2
votes

I want to scan an NFC tag without using Intent. In other words I want to force the scan. I have already read the:

http://developer.android.com/guide/topics/connectivity/nfc/index.html

https://code.google.com/p/ndef-tools-for-android/

but both use Intents.

P.S.: My case is that the NFC tag is permanently attached to the device, so I cannot use intents.

3
What bizarre use case is there for permanently attaching an NFC tag to a device so that it becomes the only NFC tag that the device can read?paulkayuk
My use case is that the nfc tag identifies the device in a network of many other devices. So i attach permanently the NFC tag to the device to distinct the device from others.mspapant
and what is wrong with using one of the existing unique device identifiers already baked into the device e.g. IMEI number (if available on your target device). Or are you just trying to come up with a case for the (Mis-)use of NFC technology?paulkayuk
Because we need flexibility. In case we want to switch/replace nf tag we should be able to do.mspapant
See this answer. Bart Friederichs's answer allows you to read the tag directly without intent.idmadj

3 Answers

2
votes

Use foreground dispatch: http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (nfcAdapter == null || !nfcAdapter.isEnabled()) {
            Log.e(TAG, "No NFC Adapter found.");
            //finish();
        }

        Intent intent = new Intent(this, getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            // Handles all MIME based dispatches. You should specify only the ones that you need.
            ndef.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("failed to add MIME type", e);
        }
        //intentFiltersArray = new IntentFilter[]{ndef,};

        //Use no intent filters to accept all MIME types
        intentFiltersArray = new IntentFilter[]{};

        // The tech list array can be set to null to accept all types of tag
        techListsArray = new String[][]{new String[]{
                IsoDep.class.getName(),
                NfcA.class.getName(),
                NdefFormatable.class.getName()
        }};
    }

    public void onPause() {
        nfcAdapter.disableForegroundDispatch(this);

        super.onPause();
    }

    public void onResume() {
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);

        super.onResume();
    }

    public void onNewIntent(Intent intent) {
        Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        //do something with tagFromIntent
}
0
votes

Easy. When you hit your "flexibility" time... and you actually change. Use INTENt, read the tag, and save the information on your sdcard, shared preferences, cloud wherever. And use this information each time you want to read the TAG. Instead read the file which was created las ttime when tag was attached. Next time when you will remove tag and add another one, your file will be recreated.

Don't read tag, read file created by tag attaching to device.

0
votes

You cannot do what you want. Reading a tag without an intent is not possible due to the way the Android NFC subsystem is built.

Also it is a very bad idea to glue a tag onto the backside of a phone. NFC will - as long as no tag has been detected - periodically check for the existence of a tag. Once a tag has been detected it will get powered over the air until the tag does not answer anymore.

If the tag is always in range of the phone it will drain battery life like crazy.