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