Android NFC Smartposter可拨打电话号码

Android NFC Smartposter可拨打电话号码,android,nfc,Android,Nfc,我想创建一个NFC SmartPoster,它可以拨打动作记录类型为“act”的号码。 有人能告诉我们如何从数据包中获取android中的动作记录类型“act”,并检查数据包是否包含动作记录类型“act”。 下面是我创建的数据包 /** * Smart Poster containing a Telephone number and Action record type. */ public static final byte[] SMART_POSTER_Dial_Number =

我想创建一个NFC SmartPoster,它可以拨打动作记录类型为“act”的号码。 有人能告诉我们如何从数据包中获取android中的动作记录类型“act”,并检查数据包是否包含动作记录类型“act”。 下面是我创建的数据包

/**
 * 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) '.'


     };

请提供帮助。

当您在
活动中通过
ACTION\u NDEF\u DISCOVERED
intent收到NDEF消息时,您可以解析并检查带有嵌入“act”记录的SmartPoster记录的内容,如下所示:

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 

顺便说一句:您会发现问题中的示例消息中存在两个格式错误:当您在
活动
中通过
操作接收NDEF消息时,Uri记录的第一个字节应该是
0x81
,文本记录的第一个字节应该是
0x51
因此,您可以使用嵌入的“act”记录解析和检查SmartPoster记录的内容,如下所示:

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 

顺便说一句:您会发现问题中的示例消息中存在一些格式错误:在这种情况下,Uri记录的第一个字节应该是
0x81
,文本记录的第一个字节应该是
0x51

答案旁边应该出现一个绿色复选标记。我还没看到。谢谢你的接受!我更喜欢在这里讨论NFC编程相关的东西,这样每个人都可以获利。当然,你可以发邮件,我也可以联系你_pb@yahoo.com电话号码:8872743939在这种情况下,答案旁边应显示绿色复选标记。我还没看到。谢谢你的接受!我更喜欢在这里讨论NFC编程相关的东西,这样每个人都可以获利。当然,你可以发邮件,我也可以联系你_pb@yahoo.com电话号码:8872743939