2.IsoDep类

本文详细介绍了IsoDep类的功能,包括如何获取IsoDep对象、主要I/O操作transceive(byte[])的使用方法及其注意事项。此外,还介绍了与IsoDep相关的NfcA或NfcB技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IsoDep

类概述:

Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations on a Tag.

Acquire an IsoDep object using get(Tag).

The primary ISO-DEP I/O operation is transceive(byte[]). Applications must implement their own protocol stack on top of transceive(byte[]).

Tags that enumerate the IsoDep technology in getTechList() will also enumerate NfcA or NfcB (since IsoDep builds on top of either of these).

Note: Methods that perform I/O operations require the NFC permission.

该类提供了访问ISO-DEP(ISO 14443 - 4)标记属性和I / O操作。

使用 get(Tag) 函数可以获得一个IsoDep对象。

主要的ISO-DEP I / O操作是 transceive(byte[]) 函数(核心函数)。应用程序必须他们自己的实现协议栈之上使用 transceive(byte[]) 函数。

使用 getTechList() 函数会列举IsoDep还将列举NfcA或NfcB(因为IsoDep构建在这两种技术其中一种之上的)。

注意:执行I / O操作的方法需要获取NFC权限。

核心函数:public byte[] transceive (byte[] data)

Send raw ISO-DEP data to the tag and receive the response.

Applications must only send the INF payload, and not the start of frame and end of frame indicators. Applications do not need to fragment the payload, it will be automatically fragmented and defragmented by transceive(byte[]) if it exceeds FSD/FSC limits.

Use getMaxTransceiveLength() to retrieve the maximum number of bytes that can be sent with transceive(byte[]).

This is an I/O operation and will block until complete. It must not be called from the main application thread. A blocked call will be canceled with IOException if close() is called from another thread.

向标签 tag发送原始ISO-DEP数据和接收响应。

应用程序必须只发送the INF payload,而不是payload的开始碎片和payload的结束碎片。应用程序不需要拆分payload,使用transceive(byte[])函数将自动分散和整理payload,如果它超过FSD / FSC的限制。

使用getMaxTransceiveLength()函数来检索transceive(byte[])能发送的最大字节数。

这是一个I / O操作,将阻塞,直到操作完成。在主应用程序线程,它不能被调用。如果在另一个线程调用close()函数,该操作调用将被取消并抛出IOException异常。


 

转载于:https://www.cnblogs.com/H-BolinBlog/p/5362431.html

// 全局变量声明 var NfcAdapter; var NdefRecord; var NdefMessage; var nfcAdapter; var pendingIntent = null; var mainActivity = null; var isScanning = false; // 新增:扫描状态标志 var waiting = null; // 提升到全局作用域 function listenNFCStatus() { try{ mainActivity = plus.android.runtimeMainActivity(); var Intent = plus.android.importClass('android.content.Intent'); var PendingIntent = plus.android.importClass('android.app.PendingIntent'); var IntentFilter = plus.android.importClass('android.content.IntentFilter'); NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter'); nfcAdapter = NfcAdapter.getDefaultAdapter(mainActivity); if (!nfcAdapter) { console.error("设备不支持NFC"); return; } var intent = new Intent(mainActivity, mainActivity.getClass()); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent.getActivity(mainActivity, 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"] ]; // 使用具名函数以便移除监听器 document.addEventListener("newintent", handleNewIntent); document.addEventListener("pause", function(e) { if (nfcAdapter && isScanning) { nfcAdapter.disableForegroundDispatch(mainActivity); console.log('应用暂停,NFC扫描已停止'); } }, false); document.addEventListener("resume", function(e) { if (nfcAdapter && isScanning) { console.log('应用恢复,重新启用NFC扫描'); nfcAdapter.enableForegroundDispatch(mainActivity, pendingIntent, intentFiltersArray, techListsArray); } }, false); console.log("NFC监听已初始化"); } catch(e){ console.error("NFC初始化错误:", e); } } // 处理NFC意图 function handleNewIntent() { console.log('检测到NFC意图'); setTimeout(handle_nfc_data1, 1000); } // 停止NFC扫描和等待提示 function stopNFCScan() { if (isScanning) { try { // 关闭等待提示框 if (waiting) { waiting.close(); waiting = null; } // 停止NFC扫描 if (nfcAdapter) { nfcAdapter.disableForegroundDispatch(mainActivity); console.log("NFC扫描已停止"); showToast("NFC扫描已停止"); } // 重置状态 isScanning = false; readyRead = false; } catch(e) { console.error("停止NFC失败:", e); } } } // 读取NFC数据 function __read(intent){ try{ if (waiting) { waiting.setTitle('请勿移开标签\n正在读取数据...'); } var tag = plus.android.importClass("android.nfc.Tag"); tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); var bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); console.log("bytesId:" + bytesId); if (waiting) { waiting.close(); waiting = null; } var cont = bytesToHexString(tag.getId()); console.log("NFC ID:", cont); showToast("读取成功: " + cont); // 读取完成后停止扫描 stopNFCScan(); } catch(e){ console.error("读取NFC错误:", e); if (waiting) waiting.close(); waiting = null; stopNFCScan(); } } // NFC数据处理 function handle_nfc_data1() { try { if (!isScanning) return; NdefRecord = plus.android.importClass("android.nfc.NdefRecord"); NdefMessage = plus.android.importClass("android.nfc.NdefMessage"); var intent = mainActivity.getIntent(); console.log("action type:" + intent.getAction()); if("android.nfc.action.TECH_DISCOVERED" == intent.getAction()){ if(readyRead){ __read(intent); } } } catch(e) { console.error("处理NFC数据错误:", e); } } // 显示提示 function showToast(msg){ plus.nativeUI.toast(msg); } // 字节数组转十六进制字符串 function bytesToHexString(bytes) { if (!bytes) return ""; var hexArray = []; for (var i = 0; i < bytes.length; i++) { var hex = bytes[i].toString(16); hex = (hex.length === 1) ? "0" + hex : hex; hexArray.push(hex); } return hexArray.join(":").toUpperCase(); } // 初始化 document.addEventListener('plusready', listenNFCStatus, false); // 全局变量 var readyWriteData = false; var readyRead = false; // 开始扫描NFC function readData(){ // 如果已经在扫描,则先停止 if (isScanning) { stopNFCScan(); return; } // 准备读取 readyRead = true; // 启动NFC扫描 try { nfcAdapter.enableForegroundDispatch( mainActivity, pendingIntent, [new IntentFilter("android.nfc.action.TECH_DISCOVERED")], [ ["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"] ] ); isScanning = true; console.log("NFC扫描已启动"); showToast("NFC扫描已启动"); // 显示等待提示 waiting = plus.nativeUI.showWaiting("请靠近NFC标签...", { background: "rgba(0,0,0,0.7)", textColor: "#FFFFFF", padding: "20px", borderRadius: "10px" }); // 添加取消按钮 var cancelBtn = document.createElement('button'); cancelBtn.innerHTML = '取消扫描'; cancelBtn.style.cssText = 'margin-top:15px;padding:8px 20px;background:#f44336;color:white;border-radius:5px;border:none;'; cancelBtn.onclick = stopNFCScan; // 添加到等待对话框 waiting.appendChild(cancelBtn); } catch(e) { console.error("启动NFC扫描失败:", e); showToast("启动NFC扫描失败"); } } // 添加停止扫描按钮的HTML代码 function createStopButton() { var btn = document.createElement('button'); btn.innerHTML = '停止扫描'; btn.style.cssText = 'position:fixed;bottom:20px;right:20px;padding:12px 24px;background:#f44336;color:white;border-radius:8px;font-size:16px;z-index:9999;'; btn.onclick = stopNFCScan; document.body.appendChild(btn); } // 页面加载完成后创建按钮 document.addEventListener('DOMContentLoaded', function() { createStopButton(); }); 写完后代码报错启动NFC扫描失败: ReferenceError: IntentFilter is not defined
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值