private void readTag(Tag t) { // get NDEF tag details Ndef ndefTag = Ndef.get(t); // 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()); if (url.startsWith(ATTENDEE_URL_PREFIX)) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("https://" + url.substring(1))); startActivity(i); return; } } } }
@Override protected String doInBackground(Tag... params) { Tag tag = params[0]; 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 && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) { try { return readText(ndefRecord); } catch (UnsupportedEncodingException e) { Log.e(TAG, "Unsupported Encoding", e); } } } return null; }
/** Parse an well known URI record */ private static UriRecord parseWellKnown(NdefRecord record) { Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI)); byte[] payload = record.getPayload(); String prefix = URI_PREFIX_MAP.get(payload[0]); byte[] fullUri = Bytes.concat( prefix.getBytes(Charset.forName("UTF-8")), Arrays.copyOfRange(payload, 1, payload.length)); Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8"))); return new UriRecord(uri); }
private ScanableTag messageToScanable(NdefMessage message) { NdefRecord record = message.getRecords()[0]; // XXX Ignoring the first byte because it contains a flag we are not // paying attention to yet byte[] rawData = new byte[record.getPayload().length - 1]; System.arraycopy(record.getPayload(), 1, rawData, 0, rawData.length); String text = decodePayload(rawData); String mimeType = decodePayload(record.getType()); return new TagParser().parse(mimeType, text); }
@Override public boolean supportsRequest(NdefRecord handoverRequest) { if (handoverRequest.getTnf() != NdefRecord.TNF_ABSOLUTE_URI || !Arrays.equals(handoverRequest.getType(), NdefRecord.RTD_URI)) { return false; } String uriString = new String(handoverRequest.getPayload()); if (uriString.startsWith(BT_SOCKET_SCHEMA)) { return true; } return false; }
/** * Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is an NFC * intent that the app recognizes. If it is, then parse the NFC message and set the activity's * intent (using {@link android.app.Activity#setIntent(android.content.Intent)}) to something the * app can recognize (i.e. a normal {@link android.content.Intent#ACTION_VIEW} intent). */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public static void tryUpdateIntentFromBeam(Activity activity) { if (UIUtils.hasICS()) { Intent originalIntent = activity.getIntent(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) { Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msg = (NdefMessage) rawMsgs[0]; // Record 0 contains the MIME type, record 1 is the AAR, if present. // In iosched, AARs are not present. NdefRecord mimeRecord = msg.getRecords()[0]; if (ScheduleContract.Sessions.CONTENT_ITEM_TYPE.equals(new String(mimeRecord.getType()))) { // Re-set the activity's intent to one that represents session details. Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(new String(mimeRecord.getPayload()))); activity.setIntent(sessionDetailIntent); } } } }
@Override protected void onResume() { super.onResume(); Intent intent = getIntent(); String action = intent.getAction(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if (rawMsgs != null) { NdefMessage[] msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; for (NdefRecord record : msgs[i].getRecords()) { String type = new String(record.getType()); byte[] payload = record.getPayload(); if (PROFILE_MIME_TYPE.equals(type) && payload != null && payload.length == 16) { handleProfileMimeType(payload); } } } } } finish(); }