阅读的Andr​​oid NfcV标签时,连接错误 [英] Connection error when reading Android NfcV tags

查看:245
本文介绍了阅读的Andr​​oid NfcV标签时,连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序读取NFC标签。一切工作正常,在LG的Nexus 4,但在三星Galaxy S5我只得到I / O异常(在多个手机进行测试)。

所以,标签类型NfcV和调用连接()在NfcV(当我得到的I / O异常再往下是错误code - 5 ERROR_CONNECT)。

NFC TagInfo由恩智浦可以读取的在SG5S标签的存储器内容 - 在那里阅读NfcV标签比连接()收发()

有什么区别NFC芯片之间会引起我的应用程序的连接失败的一个电话而不是其他的(而其他应用程序阅读罚款)?是否有超时我需要调整可能?

code片断:

  NfcV nfcvTag = NfcV.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
如果(nfcvTag!= NULL){
    尝试 {
        nfcvTag.connect();
        // {标志:为0x00,读取多个块命令:0x23,开始块0:0×00,阅读9块(0-8):0×08}
        响应= nfcvTag.transceive(新的byte [] {(字节)为0x00,(字节)0x23(字节)为0x00,字节)0×08});
    }赶上(IOException异常E){
        Log.d(NFCService,nfcvTag.toString());
    } 最后 {
        尝试 {
            nfcvTag.close();
        }赶上(IOException异常E){
        }
    }
}
 

解决方案

所以,总结一下,我们发现在上述意见在讨论中:

  

看来你不能使用 IntentService 来处理访问一个NFC标签(通过接收到的NFC发现的意图)在一个单独的线程。在这种情况下,标签技术对象的连接()方法将失败。

这并不意味着你不能处理访问标记在一个单独的(工人)线程。事实上,你应该的不可以访问标记的主(UI)线程,因为这将阻止应用程序(和它的用户界面)为标签的访问时间,并可能导致您的应用程序上由杀害系统是反应迟钝。手动产卵处理访问标记的工作就好了一个工作线程。

我个人的和未经证实的想法/乱撞为什么 IntentService 玩不转标签:

  1. 这本来是路过NFC发现意图的情况下, IntentService 推出的标签发现和实际进入标签之间的显著的延迟。在与标签这种延迟通信可能会有所下降(例如,由于用户不对齐的阅读器和标签天线使通信不可能的,等等)
  2. (我没有对Android的NFC系统服务的内部足够的知识知道这是否会甚至有可能:)这可能是在标签对象不知何故绑定到活动。由于 IntentService 是一个不同的组件(尽管在相同的应用程序上下文中执行),它可能不会被允许访问标签对象。
  

看来,在银河S5使用了NFC控制器仅支持针对ISO / IEC 15693读取命令。使用未解决的命令(即命令没有被寻址标志设置,不包含标签的UID)导致 {0X02} 被返回的收发()方法。

有很难说,如果这是通常与NFC控制器的情况下(PN547?从NXP)在S5或如果这是一些特定于如何在标签对待处理对非寻址的命令和响应。

I have an Android app that reads NFC tags. Everything works fine on the LG Nexus 4 but on Samsung Galaxy S5 I only get I/O exceptions (tested on multiple phones).

So, tags are of type NfcV and I get the I/O exception when calling connect() on the NfcV (further down it is error code -5 ERROR_CONNECT).

NFC TagInfo by NXP can read the the memory content of the tag on the SG5S - are there other ways of reading NfcV tags than with connect() and transceive()?

What differences between NFC chips will cause my app's connection to fail on one phone but not the other (while other apps read it fine)? Are there timeouts I need to adjust maybe?

Code snippet:

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) {
        }
    }
}

解决方案

So, to summarize what we found out during the discussion in the above comments:

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.

This does not mean that you cannot handle access to a tag in a separate (worker) thread. In fact you should not access a tag on the main (UI) thread as this will block the app (and its UI) for the duration of the tag access and may cause your app to be killed by the system for being unresponsive. Manually spawning a worker thread that handles access to a tag works just fine.

My personal and unverified ideas/wild guesses why the IntentService could not handle the tag:

  1. It could have been the case that passing the NFC discovery intent to the IntentService introduced a significant delay between the tag discovery and the actual access to the tag. During this delay communication with the tag could have dropped (e.g. due to the user misaligning the reader and tag antennas making communication impossible, etc)
  2. (I do not have enough knowledge about the internals of the Android NFC system service to know if this would even be possible:) It might be that the Tag object is somehow bound to the Activity. As the IntentService is a different component (though being executed in the same app context), it might not be permitted to access the Tag object.

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.

It is difficult to say if this is generally the case with the NFC controller (PN547? from NXP) in the S5 or if that's something that is specific to how the tag treats addressed versus unaddressed commands and responses.

这篇关于阅读的Andr​​oid NfcV标签时,连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆