Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing NFC tags using a Nexus S

I have a Gingerbread 2.3.4 powered Nexus S and I recently got some writable NFC tags. So far I can read them as 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?

like image 871
Marius Butuc Avatar asked Jun 08 '11 18:06

Marius Butuc


People also ask

How do you write data into NFC tags?

To simply write to one blank NFC tag, make sure all of these options are unchecked. Finally, select “Write” at the bottom of the screen and tap the back of your phone to a blank NFC tag as if you were going to read it. This writes the URL to the tag. Congratulations, you've just encoded your first NFC tag!

Can I program a NFC tag with my phone?

Turn On NFC on Your Android Device.To switch on your NFC, you should visit settings>Bluetooth & Connections> Connection Preferences> NFC> Toggle and click ON. In some devices, you may get the NFC settings under the “Network and Sharing” option (the location of NFC may vary from one device to another).

How do you write an NFC chip?

Take the tag you want to write and tap the back of your phone against it (ensure that you have NFC turned on). It will write the tag! You can now exit the app.


2 Answers

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.

like image 200
Pasi Välkkynen Avatar answered Oct 20 '22 01:10

Pasi Välkkynen


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 :-)

like image 26
ThomasRS Avatar answered Oct 20 '22 00:10

ThomasRS