0

I have a TAG that, when scanned, opens the link of the shop where I bought it. Now, I want to write it with my own data. I have a form where I write what I want to write and, when I press the button "Start write", the following function runs:

private void startWriting() {
    //This variable is actually of class to disable the foreground dispatch when the onPause method is called
    NfcAdapter mAdapter = NfcAdapter.getDefaultAdapter(this);
    changeStatus(false);

    findViewById(R.id.bt_write).setVisibility(View.GONE);
    findViewById(R.id.bt_stop).setVisibility(View.VISIBLE);

    PendingIntent pendingIntent = PendingIntent.getService(this, 0, getServiceIntent(), 0);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

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

    String[][] techListsArray = new String[][] { new String[] { NfcF.class.getName(), MifareClassic.class.getName(), MifareUltralight.class.getName() } };

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

}

So, the objective is to start a service whenever a TAG is found. The function getServiceIntent() creates an intent and puts the data as an Extra. There's nothing special about it.

The problem is that, even after this runs, Android stills starts the browser instead of writing the TAG. Weirdest of all is that if I write something on the TAG first using, for instance, NFC Task Launcher then I can use my app to write it. My guess is that I'm missing some filter to detect the TAG.

Thank you

Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74
  • why don't you write a simple text ( may be intent information ) and while reception of that text ( containing intent ) to launch activity. – Jambaaz Jan 24 '13 at 11:04
  • @GauravJain thank you for your comment but I'm affraid I am not understanding how it would solve the problem.. – Miguel Ribeiro Jan 24 '13 at 12:04
  • Your objective is to start a service whenever you detect that tag , so write some plain text ( It may be intent information ) to the NFC tag and while reading the TAG , get the Intent information from Tag and launch the same service. – Jambaaz Jan 24 '13 at 12:07
  • Well, maybe the question is not clear but I can start the service IF I write the TAG before with NFC Task Launcher for instance. Before that, my app does not recognize the TAG; after that my application recognizes it – Miguel Ribeiro Jan 24 '13 at 12:16
  • Ohh...I got it ..so you want to detect the tag before NFC task launcher detects it ? – Jambaaz Jan 24 '13 at 12:19
  • I have replied in another thread . Might be useful to you http://stackoverflow.com/questions/14457055/android-nfc-sensing-and-read-tag-data-at-the-same-time/14462813#14462813 – Jambaaz Jan 24 '13 at 13:06
  • Sorry @GauravJain but your answer in the other posts opens the activity whenever a TAG is discovered. My intention is regist the TAG discovery listener when I press a button and not always (like it is done in manifest) – Miguel Ribeiro Jan 24 '13 at 15:40

0 Answers0