简体   繁体   中英

How to read multiple payload from nfc tags and store the payloads into an arraylist?

Hi Stackers I still new in coding and need some help. Now I have two NFC tags and both tags is storing with different Geo coordinates(longitude, latitude). Currently, I able to read the payload from the NFC tags. What I want is to store the payloads from both of the NFC tags into an arraylist. When I read the first tag it does stored the payload into arraylist, but when read second tag the payload will overwritten the previous payload.

Android Manifest:

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

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".arraylist"></activity>
    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance">
        <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:host="*"
                android:scheme="geo" />
        </intent-filter>
    </activity>
</application>

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
        Toast.makeText(this, "nfc not supported", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    if (!nfcAdapter.isEnabled()) {
        startActivity(new Intent("android.settings.NFC_SETTINGS"));
        Toast.makeText(this, "nfc not yet open", Toast.LENGTH_SHORT).show();
    }

    mPendingIntent = PendingIntent.getActivity(this,0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0 );


}

private void readIntent(Intent intent){
    list = new ArrayList<>();
    Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    for(int i=0; i<parcelables.length; i ++){
        NdefMessage message =(NdefMessage)parcelables[i];
        NdefRecord[] records = message.getRecords();
        for(int j=0; j<records.length; j++){
            NdefRecord record = records[j];
            byte[] original = record.getPayload();
            byte[] value = Arrays.copyOfRange(original,0,original.length);
            String payload = new String(value);

            if(list != null){
                list.add(payload);
                System.out.println(list);
            }

        }
    }
}



@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    readIntent(intent);

}

@Override
protected void onResume() {
    super.onResume();

    nfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}

@Override
protected void onPause() {
    super.onPause();
    nfcAdapter.disableForegroundDispatch(this);
}

Here is my output: I/System.out: [geo:1.523534,103.633690]

But my expectation output is: [geo:1.523534, 103.633690, geo:1.5364496, 103.656321]

So how can achieve that second tag payload will not overwritten the previous payload?

The problem this time is that you are not creating a new instance of a Fragment like your were in your previous similar question, you are creating a new arraylist every time a card is read and thus de-referencing the first list object for it to be deleted by the garbage collection.

This is a classic variable initialisation issue.

readIntent is called every time a card is read. So remove the first line of `readIntent where you create a new list.

eg

private void readIntent(Intent intent){
    Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

....

Then in your class definition initialise the arraylist once only.

eg

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> list = new ArrayList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM