更新记录
1.0.0(2026-08-13)
- 首版:Android / iOS / 鸿蒙轻量 NDEF(微信小程序不支持)
- 能力检测与系统开关:
getNfcCapabilities/getNfcStatus - 前台会话:
startNfcSession/stopNfcSession;标签发现含 UID 与 tech 列表 - NDEF 读:Text / URI / MIME;Smart Poster 展平为 text+uri
- NDEF 写:
buildTextRecord/buildUriRecord/writeNdef - Android / 鸿蒙:空白格式化写入、Ultralight/NTAG 页读;iOS 对应能力返回明确错误码
getNdefStatus可写性与最大长度;scanTagOnce一次性扫描makeNdefReadOnly须显式确认(不可逆);统一错误码 910xxxxx- 支持 App-nvue / uni-app x(x 下鸿蒙不支持)
平台兼容性
uni-app(4.11)
| Vue2 | Vue3 | Vue3插件版本 | Chrome | Safari | app-vue | app-vue插件版本 | app-nvue | app-nvue插件版本 | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 鸿蒙插件版本 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| × | √ | 1.0.0 | × | × | √ | 1.0.0 | √ | 1.0.0 | 5.0 | 1.0.0 | 12 | 1.0.0 | 12 | 1.0.0 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × | × |
uni-app x(4.11)
| Chrome | Safari | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|---|---|
| × | × | 5.0 | 1.0.0 | 12 | 1.0.0 | × | × |
breao-nfcndef 使用说明
轻量 NFC NDEF 读写插件,适用于门禁、资产标签、点检等贴靠读写场景。
当前版本:1.0.0
- Android / iOS / 鸿蒙:能力检测、前台会话、UID、Text / URI 读写
- Android / 鸿蒙:空白标签格式化写入、Ultralight / NTAG 页读
- 不做微信小程序、不做 HCE 卡模拟、不做完整 ISO-DEP 透传工具
建议调用顺序:getNfcCapabilities / getNfcStatus → startNfcSession → 贴靠标签 → readNdef / writeNdef → stopNfcSession。各异步方法均支持 success / fail / complete;fail 含 errCode / errMsg。
1. 环境要求
- HBuilderX 4.11 及以上
- uni-app Vue3(App-vue / App-nvue)或 uni-app x(App-Android / App-iOS)
- 支持:App-Android、App-iOS、App-鸿蒙(仅 uni-app)
- 不支持:H5、微信及其它小程序;uni-app x 下鸿蒙不支持
- Android 最低 API:21;iOS 最低 12;鸿蒙最低 API:12
- App 真机调试须制作自定义基座;正式发版须购买授权后走云端传统打包
- 授权绑定唯一 appid + 包名
- 真机须具备 NFC 硬件并开启系统 NFC
2. 安装与引入
将插件目录放入工程的 uni_modules/breao-nfcndef,或从插件市场导入后同步。
import {
getNfcCapabilities,
getNfcStatus,
startNfcSession,
stopNfcSession,
onTagDiscovered,
offTagDiscovered,
readNdef,
buildTextRecord,
buildUriRecord,
writeNdef,
formatAndWriteNdef,
getNdefStatus,
scanTagOnce,
makeNdefReadOnly,
readUltralightPages,
} from '@/uni_modules/breao-nfcndef'
3. API
3.1 能力与状态
getNfcCapabilities({
success(res) {
// hasHardware / canRead / canWrite / canFormat / canReadUltralightPages / platform
},
})
getNfcStatus({
success(res) {
// enabled / hasHardware / sessionActive
},
})
3.2 会话与标签发现
startNfcSession({
alertMessage: '请将标签贴近手机',
onTagDiscovered(tag) {
// tag.uid / techList / atqa? / sak?
},
success() {},
fail(err) { console.error(err.errCode, err.errMsg) },
})
onTagDiscovered({ callback(tag) { console.log(tag.uid) } })
offTagDiscovered()
stopNfcSession({ success() {} })
会话参数见 §4。
3.3 NDEF 读 / 写
readNdef({
success(res) {
// res.message.records;Smart Poster 可展平为 text / uri
},
})
buildTextRecord({ text: 'hello', language: 'zh', success(res) { /* res.record */ } })
buildUriRecord({ uri: 'https://example.com', success(res) { /* res.record */ } })
writeNdef({ records: [/* build* 返回的 record */], success() {} })
formatAndWriteNdef({
records: [/* … */],
success() {}, // 空白格式化写入;iOS 可能返回明确不支持码
})
3.4 一次性扫描 / 只读 / 页读
scanTagOnce({
timeoutMs: 15000,
autoReadNdef: true,
success(res) {
// 含 tag 信息;autoReadNdef 时含 message
},
})
makeNdefReadOnly({
confirm: true, // 必须为 true,否则 9100012;操作不可逆
success() {},
})
readUltralightPages({
pageOffset: 0,
pageCount: 4,
success(res) {
// 页数据;仅 Android / 鸿蒙部分标签
},
})
4. 可配置项
| 字段 | 位置 | 默认 | 说明 |
|---|---|---|---|
| alertMessage | startNfcSession / scanTagOnce | 无 | iOS 系统弹层提示文案 |
| invalidateAfterFirstRead | startNfcSession | true | 是否首次读后结束会话;读写场景可设 false |
| onTagDiscovered | startNfcSession | 无 | 会话内标签发现回调 |
| debug | startNfcSession | false | 调试日志 |
| text | buildTextRecord | 必填 | NDEF Text 记录正文 |
| language | buildTextRecord | 端默认 | 语言代码,如 zh、en |
| uri | buildUriRecord | 必填 | NDEF URI 记录地址 |
| records | writeNdef / formatAndWriteNdef | 必填 | build* 返回的记录数组 |
| timeoutMs | scanTagOnce | 15000 | 一次性扫描超时毫秒 |
| autoReadNdef | scanTagOnce | true | 发现后是否自动 readNdef |
| confirm | makeNdefReadOnly | 无 | 必须显式 true,否则 9100012 |
| pageOffset | readUltralightPages | 0 | 起始页号 |
| pageCount | readUltralightPages | 4 | 读取页数(每页 4 字节) |
不传参即用默认值。
5. 完整示例
import {
getNfcStatus,
startNfcSession,
readNdef,
buildTextRecord,
writeNdef,
stopNfcSession,
} from '@/uni_modules/breao-nfcndef'
getNfcStatus({
success(st) {
if (!st.enabled) {
console.warn('请开启系统 NFC')
return
}
startNfcSession({
alertMessage: '请将标签贴近手机',
invalidateAfterFirstRead: false,
onTagDiscovered() {
readNdef({
success(msg) {
console.log(msg)
buildTextRecord({
text: 'hello-nfc',
success(built) {
writeNdef({
records: [built.record],
success() { stopNfcSession({}) },
})
},
})
},
})
},
fail(err) { console.error(err.errCode, err.errMsg) },
})
},
})
6. 权限
请在应用 manifest / 隐私弹窗中按需声明,并说明用于本机与用户贴靠的 NFC 标签之间进行 NDEF 读写。
| 平台 | 权限 | 说明 |
|---|---|---|
| Android | android.permission.NFC | 访问 NFC 硬件读写标签 |
| iOS | NFCReaderUsageDescription | NFC 读取用途说明 |
| iOS | Near Field Communication Tag Reading(Capability) | Xcode 能力开关,启用标签读取 |
| 鸿蒙 | ohos.permission.NFC_TAG | 访问 NFC 标签 |
7. 错误码(910)
| 码 | 含义 |
|---|---|
| 9100001 | 成功 |
| 9100002 | 失败 |
| 9100003 | 会话未开始 |
| 9100004 | 系统 NFC 未开启 |
| 9100005 | 当前无标签 |
| 9100006 | 参数非法 |
| 9100007 | 当前平台不支持 |
| 9100008 | 格式化不支持(如部分 iOS 场景) |
| 9100009 | Ultralight / NTAG 页读不支持 |
| 9100010 | 写入失败 |
| 9100011 | 超时 |
| 9100012 | 只读锁定未显式确认(须 confirm: true) |
| 9100013 | IO 错误 |
| 9100014 | 无 NFC 硬件 |
8. 平台注意
- 真机须具备 NFC 并开启;模拟器通常不可用
- iOS 依赖系统 NFC 弹层;空白格式化与 Ultralight 页读可能返回明确不支持码
- Android / 鸿蒙支持空白格式化写入与部分 Ultralight / NTAG 页读
makeNdefReadOnly不可逆,标签写入后将无法再次修改,调用前须confirm: true并提示用户- 读写交替场景建议
invalidateAfterFirstRead: false,避免首次读后会话被系统关闭

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 3
赞赏 0
下载 12504411
赞赏 1941
赞赏
京公网安备:11010802035340号