Записать в тег NFC, когда он будет обнаружен (намеренно) ⇐ Android
Записать в тег NFC, когда он будет обнаружен (намеренно)
I have an issue with writing to a NFC tag on intent. I have code that works, which reads the tag information when it is nearby. I am struggling with writing code for when I want to write to the tag when it is touched to the back of the phone. Here is what I got for the read part:
private void readFromIntent(Intent intent) { String action = intent.getAction(); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage[] msgs = null; if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs = (NdefMessage) rawMsgs; } } buildTagViews(msgs); } } private void buildTagViews(NdefMessage[] msgs) { if (msgs == null || msgs.length == 0) return; String text = ""; byte[] payload = msgs[0].getRecords()[0].getPayload(); String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en" try { text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); } catch (UnsupportedEncodingException e) { Log.e("UnsupportedEncoding", e.toString()); } nfc_contents.setText(getResources().getString(R.string.tag_found)); TextView textView = nfc_contents; textView.postDelayed(new Runnable() { @Override public void run() { nfc_contents.setText(""); } }, 5000); } Can somebody help me with this?
I tried to use the same intent as for read, but the app is crashing.
Источник: https://stackoverflow.com/questions/780 ... -on-intent
I have an issue with writing to a NFC tag on intent. I have code that works, which reads the tag information when it is nearby. I am struggling with writing code for when I want to write to the tag when it is touched to the back of the phone. Here is what I got for the read part:
private void readFromIntent(Intent intent) { String action = intent.getAction(); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage[] msgs = null; if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs = (NdefMessage) rawMsgs; } } buildTagViews(msgs); } } private void buildTagViews(NdefMessage[] msgs) { if (msgs == null || msgs.length == 0) return; String text = ""; byte[] payload = msgs[0].getRecords()[0].getPayload(); String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en" try { text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); } catch (UnsupportedEncodingException e) { Log.e("UnsupportedEncoding", e.toString()); } nfc_contents.setText(getResources().getString(R.string.tag_found)); TextView textView = nfc_contents; textView.postDelayed(new Runnable() { @Override public void run() { nfc_contents.setText(""); } }, 5000); } Can somebody help me with this?
I tried to use the same intent as for read, but the app is crashing.
Источник: https://stackoverflow.com/questions/780 ... -on-intent
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение