uniapp-安卓NFC读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | var NfcAdapter; var NdefRecord; var NdefMessage; var _getCardNo; export default { initNFC() { if (uni.getSystemInfoSync().platform == 'android' ) { listenNFCStatus() } }, readNFC(callback) { if (uni.getSystemInfoSync().platform == 'android' ) { readData(callback); } }, closeNFC() { if (uni.getSystemInfoSync().platform == 'android' ) { closeReadAndWrite(); } } } function listenNFCStatus() { try { var main = plus.android.runtimeMainActivity(); var Intent = plus.android.importClass( 'android.content.Intent' ); var Activity = plus.android.importClass( 'android.app.Activity' ); var PendingIntent = plus.android.importClass( 'android.app.PendingIntent' ); var IntentFilter = plus.android.importClass( 'android.content.IntentFilter' ); NfcAdapter = plus.android.importClass( 'android.nfc.NfcAdapter' ); var nfcAdapter = NfcAdapter.getDefaultAdapter(main); if (nfcAdapter == null ) { uni.showToast({ title: '设备不支持NFC!' , icon: 'none' }) return ; } if (!nfcAdapter.isEnabled()) { uni.showToast({ title: '请在系统设置中先启用NFC功能!' , icon: 'none' }); return ; } var intent = new Intent(main, main.getClass()); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0); var ndef = new IntentFilter( "android.nfc.action.TECH_DISCOVERED" ); ndef.addDataType( "*/*" ); var intentFiltersArray = [ndef]; var techListsArray = [ [ "android.nfc.tech.IsoDep" ], [ "android.nfc.tech.NfcA" ], [ "android.nfc.tech.NfcB" ], [ "android.nfc.tech.NfcF" ], [ "android.nfc.tech.Nfcf" ], [ "android.nfc.tech.NfcV" ], [ "android.nfc.tech.NdefFormatable" ], [ "android.nfc.tech.MifareClassic" ], [ "android.nfc.tech.MifareUltralight" ] ]; plus.globalEvent.addEventListener( "newintent" , function () { setTimeout(handle_nfc_data1, 1000); }, false ); plus.globalEvent.addEventListener( "pause" , function (e) { if (nfcAdapter) { nfcAdapter.disableForegroundDispatch(main); } }, false ); plus.globalEvent.addEventListener( "resume" , function (e) { if (nfcAdapter) { //console.log('resume'); nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray); } }, false ); nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray); } catch (e) { console.error(e); } } function handle_nfc_data1() { NdefRecord = plus.android.importClass( "android.nfc.NdefRecord" ); NdefMessage = plus.android.importClass( "android.nfc.NdefMessage" ); var main = plus.android.runtimeMainActivity(); var intent = main.getIntent(); //console.log("action type:" + intent.getAction()); if ( "android.nfc.action.TECH_DISCOVERED" == intent.getAction()) { if (readyWriteData) { //__write(intent); readyWriteData = false ; } else if (readyRead) { __read(intent); readyRead = false ; } } } function showToast(msg) { plus.nativeUI.toast(msg); } // function __write(intent) { // try { // waiting.setTitle('请勿移开标签\n正在写入...'); // var text = document.getElementById('text').value; // console.log("text=" + text); // var textBytes = plus.android.invoke(text, "getBytes"); // var textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, // plus.android.invoke("text/plain", "getBytes"), plus.android.invoke("", "getBytes"), textBytes); // var message = new NdefMessage([textRecord]); // var Ndef = plus.android.importClass('android.nfc.tech.Ndef'); // var NdefFormatable = plus.android.importClass('android.nfc.tech.NdefFormatable'); // var tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // var ndef = Ndef.get(tag); // if (ndef != null) { // var size = message.toByteArray().length; // console.log("size=" + size); // ndef.connect(); // if (!ndef.isWritable()) { // showToast("tag不允许写入"); // waiting.close(); // return; // } // if (ndef.getMaxSize() < size) { // showToast("文件大小超出容量"); // waiting.close(); // return; // } // ndef.writeNdefMessage(message); // waiting.close(); // showToast("写入数据成功."); // return; // } else { // var format = NdefFormatable.get(tag); // if (format != null) { // try { // format.connect(); // format.format(message); // showToast("格式化tag并且写入message"); // waiting.close(); // return; // } catch (e) { // showToast("格式化tag失败."); // waiting.close(); // return; // } // } else { // showToast("Tag不支持NDEF"); // waiting.close(); // return; // } // } // } catch (e) { // console.log("error=" + e); // waiting.close(); // alert('写入失败'); // } // } function __read(intent) { try { var content = "" ; waiting.setTitle( '请勿移开标签\n正在读取数据...' ); var tag = plus.android.importClass( "android.nfc.Tag" ); tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); var bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); waiting.close(); var tagid = bytesToHexString(tag.getId()) if ( typeof _getCardNo === 'function' ) { _getCardNo(tagid); } } catch (e) { uni.showToast({ title: e, icon: 'none' }); } } function bytesToHexString(inarray) { var i, j, x; var hex = [ "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" , "E" , "F" ]; var out = "" ; for (j = 0; j < inarray.length; ++j) { x = parseInt(inarray[j]) & 0xff; i = (x >> 4) & 0x0f; out += hex[i]; i = x & 0x0f; out += hex[i]; } return out; } function reverseTwo(str) { var str1 = "" ; for ( var i = 1; i <= str.length; i++) { str1 += str[i - 1]; if (i % 2 == 0) { if (i == str.length) { break ; } str1 += ":" ; } } var str2 = "" ; for ( var i = str1.split( ":" ).length - 1; i >= 0; i--) { str2 += str1.split( ":" )[i]; } return str2; } if (uni.getSystemInfoSync().platform == 'android' ) { //plus.globalEvent.addEventListener('plusready', listenNFCStatus, false); } var waiting; var readyWriteData = false ; var readyRead = false ; function writeData() { var textEle = plus.globalEvent.getElementById( 'text' ); if (!textEle.value) { uni.showToast({ title: '请输入要写入的内容!' , icon: 'none' }); return ; } readyWriteData = true ; waiting = plus.nativeUI.showWaiting( "请将NFC标签靠近!" ); } function readData(getCardNo) { readyRead = true ; _getCardNo = getCardNo waiting = plus.nativeUI.showWaiting( "请将NFC标签靠近!" , { modal: false }); } function closeReadAndWrite() { readyWriteData = false ; readyRead = false ; if (waiting) { waiting.close(); } } |
使用
1、引用
import nfc from "@/common/nfc.js"
2、初始化
nfc.initNFC();
3、读取
nfc.readNFC(function(nfcid) {
})
4、关闭读取
nfc.closeNFC();
岁月无情催人老,请珍爱生命,远离代码!!!
分类:
html/css/js
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!