Skip to content

skjolber/ndef-tools-for-android

Repository files navigation

Build Status Maven Central

Overview

This site hosts a library for Near Field Communication on Android using the NDEF format.

The current (version <= 14.0) Android SDK only comes with a low-level NDEF API which does not expose developers to the full potential of the NDEF format. Rather than sweating over byte arrays, developers should have access to high-level representations.

Features:

  • NDEF object representation library (no more byte arrays!)
    • Simple conversion to and from Android SDK low-level equivalent
  • JSE module with the corresponding Android classes for use in regular Java

In short, this projects helps you to handle dynamic NDEF content at runtime.

License

Apache 2.0

Obtain

The project is built with Gradle and is available on the central Maven repository. For Gradle, configure the property

ext {
  ndefToolsForAndroidVersion = '2.0.1'
}

and add the dependency

api("com.github.skjolber.ndef-tools-for-android:ndeftools:${ndefToolsForAndroidVersion}")

Usage

So a Message consists of a list of Records in the NDEF standard. Browse the source for an overview of supported record types.

Creating new NDEF records

Compose an Android Application Record:

AndroidApplicationRecord aar = new AndroidApplicationRecord();
aar.setPackageName("org.ndeftools.boilerplate");

Compose a Mime Record

MimeRecord mimeRecord = new MimeRecord();
mimeRecord.setMimeType("text/plain");
mimeRecord.setData("This is my data".getBytes("UTF-8"));

Create new NDEF message

From above, simply

Message message = new Message(); //  com.github.skjolber.ndef.Message
message.add(androidApplicationRecord);
message.add(mimeRecord);

or from bytes

byte[] messageBytes = ...; // get your bytes
Message message = Message.parseNdefMessage(messageBytes);

Converting to and from native Android NdefMessage

Use

NdefMessage lowLevel = ...; // get from existing code
Message highLevel = new Message(lowLevel);
// read from high-level records

or

Message highLevel = ...// compose high-level records
NdefMessage lowLevel = highLevel.getNdefMessage();
// .. pass low-level NdefMessage to existing code

JSE module

A few NFC classes copied from the Android open source project, so that the NDEF library can be used on regular Java (i.e. Java 8 or 11).

Example

For a working example see android-nfc-lifecycle-wrapper.

See also

For a graphical NDEF editor, try NFC Eclipse plugin. It creates static NDEF content, and so is good for getting to know the NDEF format. Recommended for developers new to NFC.

Acknowledgements

This project springs out the NFC Tools for Java and NFC Eclipse plugin projects.

History

April 2024: Version 2.0.1 maintenance release.

August 2020: Version 2.0.0 maintenance release:

  • Maven coordinates updated; group is now com.github.skjolber.ndef-tools-for-android
  • Packages renamed to com.github.skjolber.ndef
  • Added Gradle build (now dual builds with Maven)
  • Moved utilities and examples to seperate project
  • Minor improvements

March 28th 2013: Version 1.2.3 released.
February 5th 2013: Version 1.2.2 released.
January 1st 2013: Version 1.2.1 released.
October 18th 2012: Version 1.2 released.
September 15th 2012: Initial release.