2012-03-31 105 views
0

我想创建一个NFC SmartPoster,拨打带动作记录类型“act”的号码。 任何人都可以告诉如何从数据包中得到动作记录类型“行为”android并检查数据包是否包含动作记录类型“行为”与否。 下面是我创建的数据包。NFC智能拨号拨打号码

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


    }; 

请帮助..

回答

3

当您通过ACTION_NDEF_DISCOVERED意图在Activity接收NDEF消息,您可以分析和检查内容为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 

BTW:你会发现,有一对夫妇在你的问题示例消息格式错误:开放的记录的第一个字节应该是0x81和文字实录S中的第一个字节应该是0x51

+0

感谢它适用于我 – Karan 2012-04-02 05:41:21

+0

我已经接受 – Karan 2012-04-02 08:46:48

+0

在这种情况下,答案旁边应该显示一个绿色的复选标记。我还没有看到。 – 2012-04-02 11:47:29