Note that there are some explanatory texts on larger screens.

plurals
  1. PONFC tag with text data and package name not working properly
    primarykey
    data
    text
    <p>I am developing small android application in which i wanted to use <strong>NFC</strong> functionality. I did this in following ways </p> <p>On writer side :</p> <pre><code> boolean addAAR = true; String uniqueId = "qrfid://nilkash"; byte[] uriField = uniqueId.getBytes(Charset.forName("US-ASCII")); System.arraycopy(uriField, 0, payload, 0, uriField.length); NdefRecord rtdUriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], uriField); if(addAAR) { return new NdefMessage(new NdefRecord[] { rtdUriRecord, NdefRecord.createApplicationRecord("com.example.androidnfcurlreader") }); </code></pre> <p>And On receiver side I am using this in manifest file </p> <pre><code> &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;intent-filter&gt; &lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;data android:mimeType="application/com.example.androidnfcurlreader" /&gt; &lt;/intent-filter&gt; </code></pre> <p>According to my few things are working fine like when i read tag it launch my application but few things are going wrong when i read tag it launch my application but not reading data from it. once app is opened I have to again read tag for getting data. I check for this so when application launch it shows intent action as main instead of NDEF_DISCOVERED. while application is open and if I read tag it shows me data and at that time action is NDEF_DISCOVERED. But what I want when i read tag it opens my application and mush show me data as well. </p> <p>What to do? How to solve this problem? Am I doing something wrong? Please help. Thank you .</p> <pre><code> //Reading of data from tag is like this // inside oncreate activity handleIntent(intent); // inside onresume setupForegroundDispatch(activity, mNfcAdapter); // inside on pause stopForegroundDispatch(activity, mNfcAdapter); // on new intent handleIntent(intent); private void handleIntent(Intent intent) { String action = intent.getAction(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { String type = intent.getType(); if (MIME_TEXT_PLAIN.equals(type)) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); sendData(ndefmessage(tag)); } else { Log.d(TAG, "Wrong mime type: " + type); } } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { // In case we would still use the Tech Discovered Intent Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] techList = tag.getTechList(); String searchedTech = Ndef.class.getName(); for (String tech : techList) { if (searchedTech.equals(tech)) { //new NdefReaderTask().execute(tag); ndefmessage(tag); break; } } } } private void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); IntentFilter[] filters = new IntentFilter[1]; String[][] techList = new String[][]{}; // Notice that this is the same filter as in our manifest. filters[0] = new IntentFilter(); filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); filters[0].addCategory(Intent.CATEGORY_DEFAULT); try { filters[0].addDataType(MIME_TEXT_PLAIN); } catch (MalformedMimeTypeException e) { throw new RuntimeException("Check your mime type."); } adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); } private static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { adapter.disableForegroundDispatch(activity); } private String ndefmessage(Tag tag) { Tag tag1 = tag; Ndef ndef = Ndef.get(tag); if (ndef == null) { // NDEF is not supported by this Tag. return null; } NdefMessage ndefMessage = ndef.getCachedNdefMessage(); NdefRecord[] records = ndefMessage.getRecords(); for (NdefRecord ndefRecord : records) { if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN &amp;&amp; Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) { try { return readText1(ndefRecord); } catch (UnsupportedEncodingException e) { Log.e(TAG, "Unsupported Encoding", e); } } } return null; } private String readText1(NdefRecord record) throws UnsupportedEncodingException { byte[] payload = record.getPayload(); String textEncoding = ((payload[0] &amp; 128) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] &amp; 0063; return new String(payload, 0, payload.length, textEncoding); } private void sendData(String result) { eventlistsetter.getRfidListener().handleEvent(0, result); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload