2012-03-31 118 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