Saturday, October 25, 2014

Android NFC

In this tutorial we will create a simple NFC app as a new project from scratch.

Step: Create and new Project "NFC" (not Module)



Set minimum version to Eclair API v7



Select FragmentActivity (most common you will use)





Name the Activity "MainActivity"



inspect settings.gradle

include ':app'


inspect local.properties (specific to locatin of your Android SDK directory)

sdk.dir=/Users/uki/Documents/Dropbox/Android/android-sdk-macosx


inspect gradle.properties

- no configuration

inspect build.gradle

note that gradle plugin is newer that in project created last week

classpath 'com.android.tools.build:gradle:0.12.2'


inspect NFC/app/build.gradle

notice the newest versions of tools API 21 - Lollipop


apply plugin: 'com.android.application'
android {
    compileSdkVersion 21
    buildToolsVersion "21.0.2"
    defaultConfig {
        applicationId "com.chicagoandroid.nfc"
        minSdkVersion 7
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
}

Step: run the app


everything should work without any changes.


Step: open NFC/app/src/main/AndroidManifest.xml


  • Add permission within <application> tag to allow use of NFC
  • Add feature if you want your app to be available only to phones with NFC


        <uses-permission android:name="android.permission.NFC" />
        <uses-feature
            android:name="android.hardware.nfc"
            android:required="true" />
    </application>


Alternatively to uses-feature you will be able to check if device supports NFC in runtime getDefaultAdapter() is null.

Step: change minSdkVersion

  • change to API 9 if you want basic NFC support
  • change to API 10 is you want full support
  • change to API 14 to take advantage of newest API and Android Beam (device-to-device)

Previously we would set AndroidManifest.xml 
        <uses-sdk android:minSdkVersion="14"/>
You can test run the app, even using version 21 and it will not have an effect.

Since we build with build.gradle we will change it there:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 21
    buildToolsVersion "21.0.2"
    defaultConfig {
        applicationId "com.chicagoandroid.nfc"
        minSdkVersion 14
...

Step: add NFC intent filter

               <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

Run project and try to scan a tag.  The app does not pick it up.

Step: tech-list.xml resource

  • add new directory src/main/res/xml
  • add new file tech_list.xml


<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.NfcF</tech>
        <tech>android.nfc.tech.NfcV</tech>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>


Run project and try to scan a tag.