更新记录
1.0.0(2026-07-17) 下载此版本
新版本更新
平台兼容性
LY NFC 插件
ly-nfc 是一个 uni-app 插件市场的 uni_modules UTS API 插件,用于封装 App 端 Android NFC 能力。
平台
| 平台 | 支持 |
|---|---|
| App-Android | 支持 |
| App-iOS | 暂不支持 |
| H5 / 小程序 | 暂不支持 |
Android NFC 不需要运行时权限,但需要声明 android.permission.NFC。插件已在 utssdk/app-android/AndroidManifest.xml 中声明。
安装
把插件目录放到项目:
uni_modules/ly-nfc
页面中按插件根路径导入:
import {
getNfcStatus,
openNfcSettings,
startNfcReader,
stopNfcReader,
writeNfcText,
writeNfcUri
} from '@/uni_modules/ly-nfc'
API
getNfcStatus()
同步获取 NFC 状态。
const status = getNfcStatus()
console.log(status.isSupported, status.isEnabled)
返回:
{
errMsg: string
isSupported: boolean
isEnabled: boolean
}
openNfcSettings(options)
打开系统 NFC 设置页。
openNfcSettings({
success(res) {
console.log(res.errMsg)
},
fail(err) {
console.log(err.errCode, err.errMsg)
}
})
startNfcReader(options)
开启 NFC 读卡。贴近标签后会多次触发 onDiscovered。
startNfcReader({
noPlatformSounds: false,
success(res) {
console.log('读卡已启动', res.errMsg)
},
onDiscovered(res) {
console.log(res.tag.id)
console.log(res.tag.records)
},
fail(err) {
console.log(err.errCode, err.errMsg)
}
})
stopNfcReader(options)
停止 NFC 读卡。
stopNfcReader({
success(res) {
console.log(res.errMsg)
}
})
writeNfcText(options)
等待贴卡并写入 NDEF Text 记录。默认写入成功后自动停止读卡模式。
writeNfcText({
text: 'hello uni-app',
language: 'zh',
autoStop: true,
success(res) {
console.log(res.tag.id)
},
fail(err) {
console.log(err.errCode, err.errMsg)
}
})
writeNfcUri(options)
等待贴卡并写入 NDEF URI 记录。
writeNfcUri({
uri: 'https://uniapp.dcloud.net.cn',
success(res) {
console.log(res.tag.id)
}
})
错误码
| 错误码 | 错误信息 | 含义 |
|---|---|---|
| 1001 | lyNfc:fail getAppContext is null |
无法获取 App Context |
| 1002 | lyNfc:fail not support |
当前平台不支持 |
| 1003 | lyNfc:fail nfc is not supported on this device |
当前设备不支持 NFC |
| 1004 | lyNfc:fail nfc is disabled |
NFC 未开启 |
| 1005 | lyNfc:fail getUniActivity is null |
无法获取当前 Activity |
| 1006 | lyNfc:fail reader is not started |
NFC 读卡模式未启动 |
| 1007 | lyNfc:fail invalid parameter |
参数不合法 |
| 1008 | lyNfc:fail tag is not ndef |
标签不是 NDEF 标签 |
| 1009 | lyNfc:fail tag is read only |
标签不可写 |
| 1010 | lyNfc:fail write failed |
写入失败 |
注意
- Android 系统的 NFC 读写必须在真机上测试,模拟器一般没有 NFC 硬件。
writeNfcText和writeNfcUri仅支持 NDEF 标签;非 NDEF 标签会返回错误。- iOS CoreNFC 需要额外 entitlement、系统弹窗会话和设备限制,本版本先返回不支持。

收藏人数:
下载插件并导入HBuilderX
赞赏(0)
下载 1
赞赏 0
下载 12433674
赞赏 1934
赞赏
京公网安备:11010802035340号