List of usage examples for android.nfc NdefRecord getPayload
public byte[] getPayload()
From source file:Main.java
static JSONObject recordToJSON(NdefRecord record) { JSONObject json = new JSONObject(); try {//from w ww. j ava2 s.c om json.put("tnf", record.getTnf()); json.put("type", byteArrayToJSON(record.getType())); json.put("id", byteArrayToJSON(record.getId())); json.put("payload", byteArrayToJSON(record.getPayload())); } catch (JSONException e) { //Not sure why this would happen, documentation is unclear. Log.e(TAG, "Failed to convert ndef record into json: " + record.toString(), e); } return json; }
From source file:net.gpii.android.listener.activity.NfcListenerActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nfc);//from w ww . j a v a2s .c o m statusTextView = (TextView) findViewById(R.id.nfcStatusText); progressView = (ProgressBar) findViewById(R.id.nfcProgressBar); Button closeButton = (Button) findViewById(R.id.nfcExitButton); closeButton.setOnClickListener(new ActivityQuitListener(this)); try { Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage message = (NdefMessage) messages[0]; NdefRecord record = message.getRecords()[0]; byte[] payload = record.getPayload(); // String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; String textEncoding = "UTF-8"; int languageCodeLength = payload[0] & 0077; String username = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); if (username.trim().length() == 0) { String errorMessage = "Empty username found on NFC tag."; statusTextView.setText(errorMessage); progressView.setVisibility(View.INVISIBLE); } else { GpiiLoginAsyncTask task = new GpiiLoginAsyncTask(); task.execute(username.trim()); } } catch (UnsupportedEncodingException e) { String errorMessage = "Error unpacking NFC text record."; statusTextView.setText(errorMessage); progressView.setVisibility(View.INVISIBLE); Log.e(Constants.TAG, errorMessage, e); } }
From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java
private void readTag(Tag t) { byte[] id = t.getId(); // get NDEF tag details Ndef ndefTag = Ndef.get(t);//from w ww. j a v a 2 s .c o m // get NDEF message details NdefMessage ndefMesg = ndefTag.getCachedNdefMessage(); if (ndefMesg == null) { return; } NdefRecord[] ndefRecords = ndefMesg.getRecords(); if (ndefRecords == null) { return; } for (NdefRecord record : ndefRecords) { short tnf = record.getTnf(); String type = new String(record.getType()); if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) { String url = new String(record.getPayload()); recordBadge(url); } } }
From source file:com.piusvelte.taplock.client.core.TapLockToggle.java
@Override public void onServiceConnected(ComponentName name, IBinder binder) { mServiceInterface = ITapLockService.Stub.asInterface(binder); if (mUIInterface != null) { try {/* w ww . j av a 2 s. c o m*/ mServiceInterface.setCallback(mUIInterface); } catch (RemoteException e) { Log.e(TAG, e.toString()); } } Intent intent = getIntent(); if (intent != null) { String action = intent.getAction(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) && intent.hasExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) { Log.d(TAG, "service connected, NDEF_DISCOVERED"); Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage message; if (rawMsgs != null) { // process the first message message = (NdefMessage) rawMsgs[0]; // process the first record NdefRecord record = message.getRecords()[0]; if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) { try { byte[] payload = record.getPayload(); /* * payload[0] contains the "Status Byte Encodings" field, per the * NFC Forum "Text Record Type Definition" section 3.2.1. * * bit7 is the Text Encoding Field. * * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1): * The text is encoded in UTF16 * * Bit_6 is reserved for future use and must be set to zero. * * Bits 5 to 0 are the length of the IANA language code. */ String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0077; String taggedDeviceName = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); manageDevice(taggedDeviceName, ACTION_TOGGLE); } catch (UnsupportedEncodingException e) { // should never happen unless we get a malformed tag. Log.e(TAG, e.toString()); finish(); } } else finish(); } else finish(); } else if (intent.getData() != null) { String taggedDeviceName = intent.getData().getHost(); if (taggedDeviceName != null) manageDevice(taggedDeviceName, ACTION_TOGGLE); else finish(); } else if (ACTION_UNLOCK.equals(action) || ACTION_LOCK.equals(action) || ACTION_TOGGLE.equals(action)) manageDevice(intent.getStringExtra(EXTRA_DEVICE_NAME), action); else finish(); } else finish(); }
From source file:de.berlin.magun.nfcmime.core.RfidDAO.java
/** * Checks all NDEF records against the CRC value contained in the last record. * @return true, if the checksum is correct. *///from w w w.ja v a2 s .com public boolean verifyMimeRecords() { CrcGenerator generator = new CrcGenerator(); NdefRecord checksumRecord = this.records[this.records.length - 1]; NdefRecord[] payloadRecords = (NdefRecord[]) ArrayUtils.remove(this.records, this.records.length - 1); if (checksumRecord.getTnf() == NdefRecord.TNF_MIME_MEDIA && EncodingUtils.getString(checksumRecord.getPayload(), "UTF-8").startsWith("crc32:")) { String checksumStr = EncodingUtils.getString(checksumRecord.getPayload(), "UTF--8"); long checksum = Long.parseLong(checksumStr.substring(checksumStr.indexOf("crc32:") + 6)); return generator.checkHash(payloadRecords, checksum); } else { return false; } }
From source file:com.example.mynsocial.BluetoothChat.java
void processIntent(Intent intent) { Log.e(TAG, "+++ processintent +++"); NdefMessage[] messages = getNdefMessages(getIntent()); for (int i = 0; i < messages.length; i++) { for (int j = 0; j < messages[0].getRecords().length; j++) { NdefRecord record = messages[i].getRecords()[j]; statusByte = record.getPayload()[0]; int languageCodeLength = statusByte & 0x3F; //mask value in order to find language code length int isUTF8 = statusByte - languageCodeLength; if (isUTF8 == 0x00) { payload = new String(record.getPayload(), 1 + languageCodeLength, record.getPayload().length - 1 - languageCodeLength, Charset.forName("UTF-8")); } else if (isUTF8 == -0x80) { payload = new String(record.getPayload(), 1 + languageCodeLength, record.getPayload().length - 1 - languageCodeLength, Charset.forName("UTF-16")); }/*w w w .j av a 2s.c o m*/ //messageText.setText("Text received: "+ payload+", "+isUTF8+""); address = payload; Intent serverIntent = new Intent(); connectDevice(serverIntent, true); // Toast.makeText(this, address, Toast.LENGTH_LONG).show(); } } }
From source file:org.sufficientlysecure.keychain.ui.PassphraseWizardActivity.java
private String readText(NdefRecord record) throws UnsupportedEncodingException { //low-level method for reading nfc byte[] payload = record.getPayload(); String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0063; return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); }
From source file:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java
@SuppressLint("NewApi") @Override// www. j a va 2s . c o m public void onNewIntent(Intent intent) { Log.d(TAG, "onNewIntent"); if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) { NdefMessage[] msgs = getNdefMessagesFromIntent(intent); NdefRecord record = msgs[0].getRecords()[0]; byte[] payload = record.getPayload(); String payloadString = new String(payload); //Toast.makeText(getActivity().getApplicationContext(), payloadString, Toast.LENGTH_SHORT).show(); this.content = payloadString; setResult("slave", "ok"); } else if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) { Toast.makeText(getActivity().getApplicationContext(), "This NFC tag has no NDEF data.", Toast.LENGTH_LONG).show(); } }
From source file:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java
@SuppressLint("NewApi") public void recvMsg() { Log.d(TAG, "recvMsg"); if (_nfcAdapter == null) { Toast.makeText(getActivity().getApplicationContext(), "NFC is not available, please upgrade your mobile", Toast.LENGTH_SHORT).show(); /*WBase.setTxtDialogParam(R.drawable.alert_dialog_icon, "NFC is not available, please update your mobile", new OnClickListener() {// w w w . j a v a 2 s .c o m @Override public void onClick(DialogInterface arg0, int arg1) { } }); getActivity().showDialog(_WBase.DIALOG_NOTIFY_MESSAGE + dialogIndex); dialogIndex++;*/ } else { if (!_nfcAdapter.isEnabled()) { Toast.makeText(getActivity().getApplicationContext(), "NFC is closed, please enable it with beam", Toast.LENGTH_SHORT).show(); /*WBase.setTxtDialogParam(R.drawable.alert_dialog_icon, "NFC is closed, please enable it with beam", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { startActivity(new Intent( Settings.ACTION_NFC_SETTINGS)); } }); getActivity().showDialog(_WBase.DIALOG_NOTIFY_MESSAGE + dialogIndex); dialogIndex++;*/ } else { // OK if (getActivity().getIntent().getAction() != null) { if (getActivity().getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) { NdefMessage[] msgs = getNdefMessagesFromIntent(getActivity().getIntent()); NdefRecord record = msgs[0].getRecords()[0]; byte[] payload = record.getPayload(); String payloadString = new String(payload); //Toast.makeText(getActivity().getApplicationContext(), payloadString, Toast.LENGTH_SHORT).show(); this.content = payloadString; setResult("slave", "ok"); } else { } } _nfcAdapter.enableForegroundDispatch(getActivity(), _nfcPendingIntent, _readTagFilters, null); } } }
From source file:com.hybris.mobile.activity.AbstractProductDetailActivity.java
/** * Handle incoming intents. Do not load a product if we already have one. *//*from w w w .j av a2 s . c o m*/ @Override protected void onResume() { super.onResume(); String[] options = { InternalConstants.PRODUCT_OPTION_BASIC, InternalConstants.PRODUCT_OPTION_CATEGORIES, InternalConstants.PRODUCT_OPTION_CLASSIFICATION, InternalConstants.PRODUCT_OPTION_DESCRIPTION, InternalConstants.PRODUCT_OPTION_GALLERY, InternalConstants.PRODUCT_OPTION_PRICE, InternalConstants.PRODUCT_OPTION_PROMOTIONS, InternalConstants.PRODUCT_OPTION_REVIEW, InternalConstants.PRODUCT_OPTION_STOCK, InternalConstants.PRODUCT_OPTION_VARIANT }; String productCode = null; Intent intent = getIntent(); // Direct Call if (intent.hasExtra(DataConstants.PRODUCT_CODE)) //direct call from search list for example { productCode = intent.getStringExtra(DataConstants.PRODUCT_CODE); } // NFC Call else if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) //NFC { Tag tag = getIntent().getExtras().getParcelable(NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get(tag); NdefMessage message = ndef.getCachedNdefMessage(); NdefRecord record = message.getRecords()[0]; if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { productCode = RegexUtil .getProductCode(new String(record.getPayload(), 1, record.getPayload().length - 1)); } } // Call from another application (QR Code) else if (StringUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) { productCode = RegexUtil.getProductCode(intent.getDataString()); } if (StringUtils.isNotEmpty(productCode)) { this.enableAndroidBeam(productCode); } // Only load if we don't have a product already if (mProduct == null) { populateProduct(productCode, options); } invalidateOptionsMenu(); }