Skip to content Skip to sidebar Skip to footer

Set Timer To Know How Fast The Nfc Read The Tag

I'm new to android, and get the project that I need to know how fast the the NFC Tag from the phone read the tag with timer. It's been 4 days and I still try to figure it out. So

Solution 1:

With that method of reading you don't need to time how long your App spends actually reading the NFC tags as the time will always be exactly zero.

This is because the Android OS has fully read the data from the tag before the result is passed to your App.

There are ways to actually read the tag yourself if you really want to time reading the tag.

Update: As you have not told me the Type of card you are using it is not possible to write the code to measure the time you want.

Some Background:

All NFC card operations at the low level will do 1 to N number of transceive operations, with each transceive operation sends a byte Array of 1 to N bytes and the returns a byte Array of 0 to N bytes.

For the raw read time you would time the time taken to run the right number of transeive command to read the data.

Timing of higher level operations would also include the time taken to parse the N number of byte Arrays in to a NdefMessage as well as the transceive commands

The code:

So as I don't know the card type the best I can do is get a time it takes to connect to the card and read the data and parse it to an NdefMessage

This is the timing of any operation that can cause RF activity and block further execution of the code.

The code ignores the fact the Android OS has already read the card and has passed you the card data with zero time in your app spent reading the card, it re-reads the card to get the timings. Note that if you take the card away to fast it can generate exceptions and fail to time reading the card.

I could have written the code to use enableReaderMode which is more reliable especial when writing to the card and has many other benefits. But instead I used ForegroundDispatch as the example code was using ForegroundDispatch, so I followed suit.

PS I would not recommend using ForegroundDispatch

package com.test.foregrounddispatch;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.util.Log;

publicclassMainActivityextendsAppCompatActivity {

    NfcAdapter mAdapter;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = NfcAdapter.getDefaultAdapter(this);
    }

    @OverrideprotectedvoidonPause() {
        // TODO Auto-generated method stubsuper.onPause();
        mAdapter.disableForegroundDispatch(this);

    }


    @OverrideprotectedvoidonResume() {
        // TODO Auto-generated method stubsuper.onResume();
        IntentFiltertagDetected=newIntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilterndefDetected=newIntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndefDetected.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {}
        IntentFiltertechDetected=newIntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        IntentFilter[] nfcIntentFilter = newIntentFilter[]{ndefDetected,techDetected,tagDetected};

        PendingIntentpendingIntent= PendingIntent.getActivity(
                this, 0, newIntent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        if(mAdapter!= null)
            mAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);

    }

    @OverrideprotectedvoidonNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            // While with ForegroundDispatch the NDEF message has already been read// And passed to us in the intent and thus the time the App spends "read" the NFC card is Zero// We want to time time the read, so now we have been notified that a NDEF card is in range// Try and read from it// Get the Tag from the intentTagtag= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

            longstartTime=0;
            longendTime=0;

            try {
                // Create an NDEF tag objectNdefndefTag= Ndef.get(tag);

                // This is the I/O operation to read the card and format the result to an NDEF message// Nothing is done with the result to not add any time to it, as we are timing this
                startTime = System.currentTimeMillis();
                ndefTag.connect();
                ndefTag.getNdefMessage();
                endTime = System.currentTimeMillis();
                ndefTag.close();
            } catch (Exception e) {
                Log.e("NFC", e.toString());
            }
            Log.v("NFC", "Time to read in milliseconds is: " + (endTime - startTime));
        }

    }
}

For a short "Hello" plain text NDEF record on my phone it produces the log:-

V/NFC: Time to read in milliseconds is: 18

Post a Comment for "Set Timer To Know How Fast The Nfc Read The Tag"