ndefTag.connect()没有连接到标签 [英] ndefTag.connect() not connecting to tag

查看:181
本文介绍了ndefTag.connect()没有连接到标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测是否有电话已经justed扫描宁可一个Intent历史推出了标签。

问题是,当我扫描标签,仿佛在connect()已失败则抛出异常。

我要离开手机上的标签给它足够的时间来读取标签。

任何想法,为什么连接失败的原因?

先谢谢了。

  Log.e(TAG,只是扫描NFC标签和DB必须为空);
                标签标签= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);                TAGID = bytesToHexString(tag.getId());
                Log.e(TAGTAGID扫描NFC标签后,立即=+ TAGID);                如果(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()。的getAction())){
                    NDEF ndefTag = Ndef.get(标签);                    尝试{
                        ndefTag.connect(); //这个应该已经执行IO操作,如果没有标记,因此应该失败
                       // NdefMessage ndefMsg = ndefTag.getNdefMessage(); //这读取来自标签的当前NDEF消息并因此导致一个IO操作
                    }赶上(例外五){
                        //没有下降标签或标签与通信
                        Log.e(TAG,有使用Ndef.connect(连接到标签的问题);
                    } {最后
                        尝试{
                            ndefTag.close();
                        }赶上(例外五){
                        }
                    }
                    Log.e(TAG,一个格式化的NFC标签只是扫描);

  06-02 13:32:30.226:E / NfcscannerActivity(24683):格式化三天前时间= 30 /月/ 14日下午13:32
    06-02 13:32:30.226:E / LoginValidate(24683):scantime从DB = 2014年6月2日11:40:20.187具体时间=二零一四年五月三十日13:32:30.237
    06-02 13:32:30.236:E / NfcscannerActivity(24683):意图行动= android.nfc.action.NDEF_DISCOVERED
    06-02 13:32:30.236:E / LoginValidate(24683):去年得到感动标签
    06-02 13:32:30.236:E / NfcscannerActivity(24683):只扫描NFC标签和DB必须为空
    06-02 13:32:30.246:我/的System.out(24683):04
    06-02 13:32:30.246:我/的System.out(24683):4E
    06-02 13:32:30.246:我/的System.out(24683):0E
    06-02 13:32:30.246:我/的System.out(24683):22
    06-02 13:32:30.246:我/的System.out(24683):C2
    06-02 13:32:30.246:我/的System.out(24683):23
    06-02 13:32:30.246:我/的System.out(24683):84
    06-02 13:32:30.246:E / NfcscannerActivity(24683):TAGID扫描NFC标签后,立即= 0x044e0e22c22384
    06-02 13:32:30.256:E / NfcscannerActivity(24683):有与连接使用Ndef.connect标签问题(
    06-02 13:32:30.256:E / NfcscannerActivity(24683):一个格式化的NFC标签只是扫描
    06-02 13:32:30.256:E / NfcscannerActivity(24683):ndefrecord具有1的长度
    06-02 13:32:30.256:E / NfcscannerActivity(24683):TextRecord.text = 1,10,5,爱丽丝riswell
    06-02 13:32:30.256:E / NfcscannerActivity(24683):有效载荷有20个的长度

 如果(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()的getAction())){
                    新主题(新的Runnable接口(){                        @覆盖
                        公共无效的run(){
                            NDEF ndefTag = Ndef.get(标签);                            尝试{
                                Log.e(TAG,即将试连接()********* *******);
                                ndefTag.connect(); //这个应该已经执行IO操作,如果没有标记,因此应该失败
                                Log.e(TAGNdef.connect()连接!********** ********);
                                NdefMessage ndefMsg = ndefTag.getNdefMessage(); //这读取来自标签的当前NDEF消息并因此导致一个IO操作                            }赶上(例外五){
                                //没有下降标签或标签与通信
                                Log.e(TAG,有使用Ndef.connect(连接到标签的问题);
                            } {最后
                                尝试{
                                    ndefTag.close();
                                }赶上(例外五){
                                }
                            }                        }
                    })。开始();

  06-02 14:03:19.396:E / NfcscannerActivity(15153):我们需要插入纪录分贝空
06-02 14:03:19.396:E / NfcscannerActivity(15153):即将试连接()************ ****************
06-02 14:03:19.396:E / NfcscannerActivity(15153):formattedNowTime = 2014年6月2日14:03:19.405
06-02 14:03:19.396:E / NfcscannerActivity(15153):有与连接使用Ndef.connect标签问题(


解决方案

的connect()code需要在一个单独的线程中运行,我需要的权限

 <使用许可权的android:NAME =android.permission.NFC/>

I'm trying to detect whether there is a tag that the phone has justed scanned rather that an Intent Launched from history.

The problem is that when i scan a tag, an exception is thrown as if the connect() has failed.

I'm leaving the phone on the tag to give it enough time to read the tag.

Any ideas why the connect is failing?

Thanks in advance.

Log.e(TAG, "just scanned an nfc tag and DB must be empty");
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

                tagId = bytesToHexString(tag.getId());
                Log.e(TAG, "tagId immediately after scanning nfc tag = " + tagId);

                if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {






                    Ndef ndefTag = Ndef.get(tag);

                    try {
                        ndefTag.connect();  // this should already perform an IO operation and should therefore fail if there is no tag
                       // NdefMessage ndefMsg = ndefTag.getNdefMessage();  // this reads the current NDEF message from the tag and consequently causes an IO operation
                    } catch (Exception e) {
                        // there is no tag or communication with tag dropped
                        Log.e(TAG, "There a problem with connecting to the tag using Ndef.connect(");
                    } finally {
                        try {
                            ndefTag.close();
                        } catch (Exception e) {
                        }
                    }








                    Log.e(TAG, "A formatted NFC Tag just scanned");

.

   06-02 13:32:30.226: E/NfcscannerActivity(24683): formatted three days ago time = 30/May/14 13:32pm
    06-02 13:32:30.226: E/LoginValidate(24683): scantime from db = 2014-06-02 11:40:20.187 specific time = 2014-05-30 13:32:30.237
    06-02 13:32:30.236: E/NfcscannerActivity(24683): action of intent = android.nfc.action.NDEF_DISCOVERED
    06-02 13:32:30.236: E/LoginValidate(24683): getting last tag touched
    06-02 13:32:30.236: E/NfcscannerActivity(24683): just scanned an nfc tag and DB must be empty
    06-02 13:32:30.246: I/System.out(24683): 04
    06-02 13:32:30.246: I/System.out(24683): 4e
    06-02 13:32:30.246: I/System.out(24683): 0e
    06-02 13:32:30.246: I/System.out(24683): 22
    06-02 13:32:30.246: I/System.out(24683): c2
    06-02 13:32:30.246: I/System.out(24683): 23
    06-02 13:32:30.246: I/System.out(24683): 84
    06-02 13:32:30.246: E/NfcscannerActivity(24683): tagId immediately after scanning nfc tag = 0x044e0e22c22384
    06-02 13:32:30.256: E/NfcscannerActivity(24683): There a problem with connecting to the tag using Ndef.connect(
    06-02 13:32:30.256: E/NfcscannerActivity(24683): A formatted NFC Tag just scanned
    06-02 13:32:30.256: E/NfcscannerActivity(24683): ndefrecord has a length of 1
    06-02 13:32:30.256: E/NfcscannerActivity(24683): TextRecord.text = 1,10,5,Alice riswell
    06-02 13:32:30.256: E/NfcscannerActivity(24683): payload has a length of 20

[Edit1]

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {


                    new Thread(new Runnable() {

                        @Override
                        public void run() {


                            Ndef ndefTag = Ndef.get(tag);

                            try {
                                Log.e(TAG, "about to test connect()********************************************");
                                ndefTag.connect();  // this should already perform an IO operation and should therefore fail if there is no tag
                                Log.e(TAG, "Ndef.connect() connected!********************************************");
                                NdefMessage ndefMsg = ndefTag.getNdefMessage();  // this reads the current NDEF message from the tag and consequently causes an IO operation

                            } catch (Exception e) {
                                // there is no tag or communication with tag dropped
                                Log.e(TAG, "There a problem with connecting to the tag using Ndef.connect(");
                            } finally {
                                try {
                                    ndefTag.close();
                                } catch (Exception e) {
                                }
                            }

                        }
                    }).start();

.

06-02 14:03:19.396: E/NfcscannerActivity(15153): we need to insert record as db empty
06-02 14:03:19.396: E/NfcscannerActivity(15153): about to test connect()********************************************
06-02 14:03:19.396: E/NfcscannerActivity(15153): formattedNowTime = 2014-06-02 14:03:19.405
06-02 14:03:19.396: E/NfcscannerActivity(15153): There a problem with connecting to the tag using Ndef.connect(

解决方案

The connect() code needs to run in a seperate thread and i needed the permission

<uses-permission android:name="android.permission.NFC" />

这篇关于ndefTag.connect()没有连接到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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