FudiNFC Library

1. Introduction

Target & How setup

This is a tutorial for introducing how to read and write Near-Field-Communication tags using Android Project. First at all, need add the dependency repository and library into your gradle files:

Please replace latestVersion annotation with the latest maven version

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    Implementation 'com.github.romellfudi:FudiNFC:{latestVersion}'
}

Dependencies

Knowledge Basic programming

2. Setting up

To start you need a code with interfaces classes or adapter patterns.

Download the Code

Click the following link to download all the code for this tutorial:

Download source code

What you'll learn

What you'll need

3. How to read NFC tags

Add manifest's permission:

Add the following to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.NFC" />

Paste this in the activity if you're extending our class:

override fun onNewIntent(Intent intent) {
    super.onNewIntent(intent) 
    for (String message in getNfcMessages()) { 
        // message 
    }
}
override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    val items: SparseArray<String> = NfcReadUtilityImpl()
                           .readFromTagWithSparseArray(intent)
    for (i in 0 until items.size()) {
        // items.valueAt(i) 
    }
}
override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    for (message in NfcReadUtilityImpl().readFromTagWithMap(intent).values()) {
        // message
    }
}

4. How to write into tags

Implement NFC Callback interfaces:

Implement your own business-case callbacks in order to write the byte buffer:

mOpCallback = when(condition) {
// write email
OpCallback { it.writeEmailToTagFromIntent(text, null, null, intent) }

// write sms message
OpCallback { it.writeSmsToTagFromIntent(text, null, intent) }

// write geolocation - latitude & longitude
OpCallback { it.writeGeolocationToTagFromIntent(latitude, longitude, intent) } 

// write uri
OpCallback { it.writeUriToTagFromIntent(text, intent) }

// write phone contact
OpCallback { it.writeTelToTagFromIntent(text, intent) }

// write rolling-on bluetooth device
OpCallback { it.writeBluetoothAddressToTagFromIntent(text, intent) }
}

Let your activity implement TaskCallback and invoke the WriteCallbackNfc function:

mTaskCallback = object : TaskCallback {
                override fun onReturn(result: Boolean) {
                }

                override fun onProgressUpdate(vararg values: Boolean?) {
                }

                override fun onError(e: Exception) {         
                }
}

WriteCallbackNfc(mTaskCallback, mOpCallback).executeWriteOperation()

5. How to obtain tags' MAC

Use the following function as to get MAC of the tag

fun getMAC(tag: Tag): String {
    val byteArrayToHexString = String.format("%0" + (tag.id.size * 2).toString() + "X",
                                 BigInteger(1, tag.id))
    val regex = Regex("(.{2})")
    return regex.replace(byteArrayToHexString, "$1:").dropLast(1)
}

For instance:

val mac = getMAC(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG) as Tag)
// returns 0025:96FF:FE12:3456

6. License

by Romell Domínguez

Copyright 2016 Romell D.Z.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.