我试图检测是否有手机已经扫描过的标签,而不是从历史记录发出的意图。
问题在于,当我扫描标签时,会抛出异常,就像connect()失败一样。
我将手机放在标签上,以便有足够的时间阅读标签。
为什么连接失败的任何想法?
提前致谢。
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(
答案 0 :(得分:0)
connect()代码需要在一个单独的线程中运行,我需要权限
<uses-permission android:name="android.permission.NFC" />