ndefTag.connect() not connecting to tag

ぃ、小莉子 提交于 2019-12-24 23:03:01

问题


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(

回答1:


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

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


来源:https://stackoverflow.com/questions/23994776/ndeftag-connect-not-connecting-to-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!