简体   繁体   中英

NFC Smartposter to Dial a number

i want to create a NFC SmartPoster which dial a number with Action Record type "act". Can anyone tell how to get Action Record type "act" in android from packet and check whether packet contains Action Record type "act" or not. Below is packet i have created.

/**
 * Smart Poster containing a Telephone number and Action record type.
 */

public static final byte[] SMART_POSTER_Dial_Number =
    new byte[] {
    // SP type record
    (byte) 0xd1, (byte) 0x02, (byte) 0x26, (byte) 0x53, (byte) 0x70,
 // Call type record
    (byte) 0xd1, (byte) 0x01, (byte) 0x0e, (byte) 0x55, (byte) 0x05, (byte) 0x2b,
    (byte) 0x39, (byte) 0x31, (byte) 0x38, (byte) 0x38, (byte) 0x37, (byte) 0x32,
    (byte) 0x37, (byte) 0x34, (byte) 0x33, (byte) 0x39, (byte) 0x33, (byte) 0x39, 

    // Action type record
    (byte) 0x11, (byte) 0x03, (byte) 0x01, (byte) 0x61, (byte) 0x63, (byte) 0x74,
    (byte) 0x00,
 // Text type record with 'T'
    (byte) 0x91, (byte) 0x01, (byte) 0x09, (byte) 0x54, (byte) 0x02, (byte) 'C',
    (byte) 'a', (byte) 'l', (byte) 'l', (byte) 'i', (byte) 'n', (byte) 'g', (byte) '.'


     };

Please help..

When you receive an NDEF message in your Activity via an ACTION_NDEF_DISCOVERED intent, you can parse and check the contents for a SmartPoster record with an embedded 'act' record as follows:

Intent intent = getIntent();
final Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage mesg = (NdefMessage) rawMsgs[0]; // in theory there can be more messages

// let's inspect the first record only
NdefRecord[] record = mesg.getRecords()[0];
byte[] type = record.getType();

// check if it is a SmartPoster
byte[] smartPoster = { 'S', 'p'};
if (Arrays.equals(smartPoster, type) {
  byte[] payload = record.getPayload();

  // try to parse the payload as NDEF message
  NdefMessage n;
  try {
    n = new NdefMessage(payload);
  } catch (FormatException e) {
    return; // not an NDEF message, we're done
  }

  // try to find the 'act' record
  NdefRecord[] recs = n.getRecords();
  byte[] act = { 'a', 'c', 't' };
  for (NdefRecord r : recs) {
    if (Arrays.equals(act, r.getType()) {
      ... // found it; do your thing!
      return;
    }
  }
}
return; // nothing found 

BTW: You will find that there are a couple of format errors in the example message in your question: the first byte of the Uri record should be 0x81 and the first byte of the Text record should be 0x51 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM