开发者

Writing NFC tags using a Nexus S

开发者 https://www.devze.com 2023-03-11 18:38 出处:网络
I have a Gingerbread 2.3.4 powered Nexus S and I recently got some writable NFC tags. So far I can read them 开发者_StackOverflowas blank tags, but I couldn\'t find a way to write data to them.

I have a Gingerbread 2.3.4 powered Nexus S and I recently got some writable NFC tags. So far I can read them 开发者_StackOverflowas blank tags, but I couldn't find a way to write data to them.

All my research has lead me to this article: Writing tags with Nexus S from January (before 2.3.4 release).

How do you write NFC tags inside your application, using your Nexus S? Any pointers?


I found the Android NFC API text and dev guide a bit tricky to follow so a bit of example code might help here. This is actually a port of MIDP code I've been using in Nokia 6212 devices, so I probably haven't yet figured out everything about Android NFC API correctly, but at least this has worked for me.

First we create an NDEF record:

private NdefRecord createRecord() throws UnsupportedEncodingException {
    String text       = "Hello, World!";
    String lang       = "en";
    byte[] textBytes  = text.getBytes();
    byte[] langBytes  = lang.getBytes("US-ASCII");
    int    langLength = langBytes.length;
    int    textLength = textBytes.length;
    byte[] payload    = new byte[1 + langLength + textLength];

    // set status byte (see NDEF spec for actual bits)
    payload[0] = (byte) langLength;

    // copy langbytes and textbytes into payload
    System.arraycopy(langBytes, 0, payload, 1,              langLength);
    System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);

    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
                                       NdefRecord.RTD_TEXT, 
                                       new byte[0], 
                                       payload);

    return record;
}

Then we write the record as an NDEF message:

private void write(Tag tag) throws IOException, FormatException {
    NdefRecord[] records = { createRecord() };
    NdefMessage  message = new NdefMessage(records);

    // Get an instance of Ndef for the tag.
    Ndef ndef = Ndef.get(tag);

    // Enable I/O
    ndef.connect();

    // Write the message
    ndef.writeNdefMessage(message);

    // Close the connection
    ndef.close();
}

To write to a tag, you obviously need the Tag object, which you can get from the Intent.


Maybe I'm a little late here, but I've written a library for creating, reading and writing NDEF records you might find useful.

As you might have learned, the native Android NdefMessage and NdefRecord classes are only byte-array wrappers and so although the NDEF standard is quite well specified within the NFC forum standards, there is currently no proper high-level support in Android.

The project also includes read, write and beam template activities :-)


NXP, the maker of many NFC compatible chips, makes a free but closed source app to manipulate NFC tags. I've written quite a few tags with it.

See: NXP NFC Tagwriter app

https://market.android.com/details?id=com.nxp.nfc.tagwriter

If you want to write code to do so, inazaruk's link helps, or you could try the O'Reilly "Programming Android" online book. It's has an NFC section:

http://programming-android.labs.oreilly.com/ch16.html#ch18_id316624

It's not the greatest book -- I find it too dense and some parts are poorly written -- but its NFC section and code samples is the only one I've really seen to date apart from Android's own.


To write NDEF data you can use the Ndef.writeNdefMessage() API.

If you want to write non-NDEF data then you can use the low-level transceive API's such as NfcA.transceive(), NfcB.transceive(), or IsoDep.transceive(). You will need to have advanced knowledge of the tag you are communicating with and its command/responses. I don't recommend this.

NDEF is the standard data format and can be easily read back by Android and other NFC platforms.

http://developer.android.com/reference/android/nfc/tech/Ndef.html#writeNdefMessage(android.nfc.NdefMessage)


NXP's tagwriter is a great application for writing tags.

My company, which has an application for encoding and managing tags in the cloud, recently held a workshop in Seattle walking developers through the process of reading and writing NFC tags in Android.

The blog posts for reading and writing NFC tags walk through code and have supporting video clips from the actual workshop.

Hope this helps some developers get started with NFC!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号