0

我正在开发我想在其中使用NFC功能的小型 android 应用程序。我通过以下方式做到了这一点

在作家方面:

 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);
}
}
4

0 回答 0