NFC标签,文本数据和包名称无法正常工作

时间:2013-05-28 05:18:23

标签: android android-intent action nfc

我正在开发小型Android应用程序,其中我想使用 NFC 功能。我是通过以下方式做到的

在作家方面:

 boolean addAAR = true;
String uniqueId = "qrfid://nilkash";       
byte[] uriField = uniqueId.getBytes(Charset.forName("US-ASCII"));

System.arraycopy(uriField, 0, payload, 0, uriField.length); 

NdefRecord rtdUriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], uriField); 

if(addAAR) 
{
    return new NdefMessage(new NdefRecord[] {
    rtdUriRecord, NdefRecord.createApplicationRecord("com.example.androidnfcurlreader")
}); 

在接收方,我在清单文件中使用它

 <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/com.example.androidnfcurlreader" />
</intent-filter>

根据我的一些工作正常工作,就像我读标签它启动我的应用程序,但很少有东西出错我读标签它启动我的应用程序,但没有从中读取数据。一旦应用程序打开,我必须再次读取标记以获取数据。我检查了这一点,所以在应用程序启动时它会将意图操作显示为main而不是NDEF_DISCOVERED。应用程序打开时,如果我读取标签,它会向我显示数据,此时操作是NDEF_DISCOVERED。但是当我读取标签时我想要它打开我的应用程序并且也显示数据。

怎么办?如何解决这个问题呢?难道我做错了什么?请帮忙。谢谢。

  //Reading of data from tag is like this


// inside oncreate activity
handleIntent(intent);

// inside onresume
setupForegroundDispatch(activity, mNfcAdapter);

// inside on pause  
stopForegroundDispatch(activity, mNfcAdapter);

// on new intent
handleIntent(intent);

private void handleIntent(Intent intent) 
{
    String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {


        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {

            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            sendData(ndefmessage(tag));

        } else {
            Log.d(TAG, "Wrong mime type: " + type);
        }
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

        // In case we would still use the Tech Discovered Intent
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String[] techList = tag.getTechList();
        String searchedTech = Ndef.class.getName();

        for (String tech : techList) {
            if (searchedTech.equals(tech)) {
                //new NdefReaderTask().execute(tag);
                ndefmessage(tag);
                break;
            }
        }
    }
}

private void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Notice that this is the same filter as in our manifest.
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

private static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) 
{
    adapter.disableForegroundDispatch(activity);
}

private String ndefmessage(Tag tag)
{
    Tag tag1 = tag;

    Ndef ndef = Ndef.get(tag);
    if (ndef == null) {
        // NDEF is not supported by this Tag. 
        return null;
    }


    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                return readText1(ndefRecord);
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "Unsupported Encoding", e);
            }
        }
    }

    return null;
}

private String readText1(NdefRecord record) throws UnsupportedEncodingException 
{
    byte[] payload = record.getPayload();
    String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
    int languageCodeLength = payload[0] & 0063;

    return new String(payload, 0, payload.length, textEncoding);
}


private void sendData(String result)
{
    eventlistsetter.getRfidListener().handleEvent(0, result);
}
}

0 个答案:

没有答案