android - 读取 Android NfcV 标签时出现连接错误

标签 android nfc rfid iso-15693

我有一个 Android 应用程序可以读取 NFC 标签。在 LG Nexus 4 上一切正常,但在三星 Galaxy S5 上我只遇到 I/O 异常(在多部手机上测试过)。

因此,标签是 NfcV 类型,当我在 NfcV 上调用 connect() 时出现 I/O 异常(进一步向下是错误代码 -5 ERROR_CONNECT)。

NFC TagInfo by NXP可以读取 SG5S 上标签的内存内容 - 除了使用 connect()transceive() 之外,还有其他读取 NfcV 标签的方法吗?

NFC 芯片之间有什么区别会导致我的应用程序在一部手机上连接失败,而在另一部手机上却不会(而其他应用程序可以正常读取)?是否有我需要调整的超时?

代码 fragment :

NfcV nfcvTag = NfcV.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
if(nfcvTag!=null){
    try {
        nfcvTag.connect();
        //{flags:0x00, read multiple blocks command: 0x23, start at block 0: 0x00, read 9 blocks (0 to 8): 0x08}
        response = nfcvTag.transceive(new byte[] {(byte)0x00,(byte)0x23,(byte)0x00,byte)0x08});
    } catch (IOException e) {
        Log.d("NFCService", nfcvTag.toString());
    } finally {
        try {
            nfcvTag.close();
        } catch (IOException e) {
        }
    }
}

最佳答案

因此,总结一下我们在上述评论中的讨论中发现的内容:

It seems that you cannot use an IntentService to handle access to an NFC tag (through a received NFC discovery intent) in a separate thread. In that case the tag technology object's connect() method will fail.

这并不意味着您不能在单独的(工作)线程中处理对标签的访问。事实上,您不应该访问主 (UI) 线程上的标签,因为这会在访问标签期间阻塞应用(及其 UI),并可能导致您的应用被 react 迟钝的系统。手动生成一个工作线程来处理对标签的访问就可以了。

我个人未经证实的想法/疯狂猜测为什么 IntentService 无法处理标签:

  1. 将 NFC 发现 Intent 传递给 IntentService 可能会在标签发现和实际访问标签之间引入明显的延迟。在此延迟期间,与标签的通信可能会中断(例如,由于用户未对准阅读器和标签天线,导致无法进行通信等)
  2. (我对 Android NFC 系统服务的内部结构了解不够,无法知道这是否可能:)可能是 Tag 对象以某种方式绑定(bind)到 Activity 。由于 IntentService 是一个不同的组件(尽管在相同的应用上下文中执行),它可能不允许访问 Tag 对象。

It seems that the NFC controller used in the Galaxy S5 only supports addressed ISO/IEC 15693 READ commands. Using unaddressed commands (i.e. commands that do not have the addressed flag set and do not contain the tag's UID) lead to { 0x02 } being returned from the transceive() method.

很难说这是 S5 中 NFC Controller (NXP 的 PN547?)的普遍情况,还是标签处理已寻址命令和未寻址命令和响应的方式所特有的情况。

关于android - 读取 Android NfcV 标签时出现连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827996/

相关文章:

android - 使用 Android-Tablet 作为 MQTT-Server

android - ImageButton 提升问题

ios - 使用 Apple Wallet NFC Pass 作为 NFC 标签

android - 如何使用 NFC 重定向或打开我的 Progressive Web App?

android - HF-RFID 与 NFC - 支持 NFC 的手机可以读取高频 RFID 标签吗?

java - 将 javax.smartcardio 用于 MIFARE Classic 和 Omnikey 5021 CL

android - Listview 不会匹配_parent

java - (Java)在打砖 block 游戏中使用 vector 碰撞

java - NFC 阅读器 "SELECT (by AID)"APDU 未路由到 Android 设备

linux - 在 Linux 中使用针对 Windows 制作的 SDK 来适应不同的架构