문제

I have developped 2 Android applications. The first one, to write into an NFC tag, and the second to read the contents I have written .

This is what i did in the first application (WriteNFC)

private NdefRecord createRecord1(String data)
{ 
byte[] payload = data.getBytes(Charset.forName("UTF-8"));
byte[] empty = new byte[] {};
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, empty, empty, payload);
}
private NdefRecord createRecord2(String data)
{ 
byte[] payload = data.getBytes(Charset.forName("UTF-8"));
byte[] empty = new byte[] {};
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, payload, empty, empty);
}

And in the second application (ReadNFC)

NdefRecord cardRecord = msg.getRecords()[1];//Extract the second Record
String url_data = new String(cardRecord.getType());//Read data type

When I read with my own application (ReadNFC), of course I had on screen only the payload of the second Record, which I stored through "Record Type". But with a third-party application, especially that natively installed ("tag") -shown in photo-, It display correctly the first record, and for the second it's an empty field. How can I hide this field. Otherwise, how can I force the other third-party applications to not read the second record?

enter image description here

도움이 되었습니까?

해결책

You simply cannot do that. Android will read the complete NDEF messages (i.e. all records) and pass it on in the Intent to an app.

다른 팁

Upps, it is no wonder this is happening, look at your code. First

return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, empty, empty, payload);

then

return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, payload, empty, empty);

So the 3rd party apps are really showing the correct data, you have a bug in your record creation.

The native NDEF support in android is somewhat crude (byte-array based), so I've written a library which helps in creating records - which you might find interesting. The above issue might be simple to resolve, but there are many other much more complex record types, so some help might come in handy ;-)

Edit: So if this is the preferred result, rather create an Unknown Record and put your 'secret' data as the payload - the will not be any good way for any 3rd party app to display that data - whereas the ID of the absolute URI Record certainly can be displayed by any NDEF-reading app (like mine?)

This third party app was bothersome to me, so I had to use foregroundDispatch to read the tag contents manually, there you have the freedom to read or not read anything you want. This snippet is from the OnResume(). `

mNfcAdapter.enableForegroundDispatch(this, pendingIntent,
                intentFiltersArray, techListsArray);

            Toast.makeText(getApplicationContext(), "TAG disscovered",
                    Toast.LENGTH_LONG).show();
            Parcelable[] rawMsgs = getIntent()
            .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

            if (rawMsgs != null) {
                NdefMessage[] msgs = new NdefMessage[rawMsgs.length];


                for (int i = 0; i < rawMsgs.length; i++) {

                    msgs[i] = (NdefMessage) rawMsgs[i];
                    setText=new String(msgs[i].getRecords()[0].getPayload());
                }
                mInfoText.setText(setText);
            }

        }`

Here I get payload of the 1st record.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top