Question

I have created an application which gets the NFC UID and changes a text view to the ID however, when i try to do more things with the application, i get an error message. For example, if i try to say... if the UID = "001hghg" then change another text view to "success!" Therefore i was wondering if its possible to start the application first, scan the tag and within the onCreate or onResume method, change the text view. Here is the code below:

public class MainActivity extends Activity {

    TextView tvUID;

    // list of NFC technologies detected:
    private final String[][] techList = new String[][] {
            new String[] {
                NfcA.class.getName(),
                NfcB.class.getName(),
                NfcF.class.getName(),
                NfcV.class.getName(),
                NdefFormatable.class.getName(),
                TagTechnology.class.getName(),
                IsoDep.class.getName(),
                MifareClassic.class.getName(),
                MifareUltralight.class.getName(), 
                Ndef.class.getName()
            }
    };

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

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        // creating pending intent:
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        // creating intent receiver for NFC events:
        IntentFilter filter = new IntentFilter();
        filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
        // enabling foreground dispatch for getting intent from NFC event:
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);

    } 



    @Override
    protected void onNewIntent(Intent intent) {
        if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
            tvUID = (TextView) findViewById(R.id.tvUID);
            tvUID.setText(ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
        }
    } 

    private String ByteArrayToHexString(byte [] inarray) {
        int i, j, in;
        String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
        String out= "";

        for(j = 0 ; j < inarray.length ; ++j) 
            {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
            }
        return out;
    }

}

No correct solution

OTHER TIPS

Intents are Android's means of inter-process communication. Consequently, being notified about actions external to your applications typically involves notifications through intents. A user scanning an NFC tag is an action that happens outside of your application, so you want your application to be somehow notified about that event. In general, the NFC service does this by sending an intent, and your application registers to receive that intent.

On Android < 4.4, intents were the only way of receiving a notification on NFC tag events. While your app is running, you can either receive them through onNewIntent (intent filters in manifest, foreground dispatch) or through onActivityResult (foreground dispatch).

Starting with Android 4.4, there is a new reader-mode API. THis API permits you to register for a specific callback onTagDiscovered to be triggered upon tag detection.

So yes, you can be notified about tag discovery through other callback methods than onNewIntent. But no, the onCreate method will be the first method invoked when your activity is created. onStart and onResume immediately follow that method. So there is no way to scan that tag in between starting your activity manually and invokation of these methods.

However, if you activity has been started by an NFC intent, you can get that intent with the activity's getIntent method. This also works in any of the methods onCreate, onStart and onResume.

The action you are checking (ACTION_TAG_DISCOVERED) is bypassed by ACTION_NDEF_DISCOVERED. I worked on a NFC project recently and for some reason every time I would use this action it wouldn't pass (even with the action_tag_discovered filter). So I started checking discovery with TECH and NDEF_DISCOVERED only.

I don't know if this is of any help, but it kinda solved all my problems 8-)

Maybe you could post the error you are getting?

And which environment you are using. Are you using Android Studio and a phone linked with adb?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top