Android Tutorial Reading NDEF data from NFC tags

Using the NFC library you can read NFC tags formatted in NDEF form (NFC Data Exchange Format).
You can read more information about the internal process here: NFC Basics | Android Developers

This library requires Android version 2.3.3 or above (API level 10 or above).

Whenever an NDEF tag is read, Android sends an intent with the extracted data. You should declare that your activity wants to handle such intents.
This is done by adding "intent filters" with the manifest editor.

There are two types of data that you can handle: text data and Uri data.

When your activity receives an Intent, the activity is first created. If the activity is currently running then it will be paused and created again.

You can use code similar to the following code to test whether the starting intent is actually an Ndef intent and then extract the records (usually a single record) from the intent:
B4X:
Sub Process_Globals
   Dim NFC As NFC
End Sub

Sub Activity_Resume
   If NFC.IsNdefIntent(Activity.GetStartingIntent) Then
      Dim records As List
      records = NFC.GetNdefRecords(Activity.GetStartingIntent)
      For i = 0 To records.Size - 1
         Dim r As NdefRecord
         r = records.Get(i)
         Log(r.GetAsTextType)
      Next
   End If
End Sub

Text data
Tags with text data include a MIME field that helps the OS decide which application should handle the intent.
For example if you want to handle messages with the "text/plain" mime (with the main activity) then you should add the following code in the manifest editor:
B4X:
AddActivityText(main, <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" />
</intent-filter>)
Note that you can add any number of intent filters.

Call NdefRecord.GetAsTextType to convert the payload to a string.


Uri data
The intent filter of Uri tags should look like:
B4X:
AddActivityText(main, <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http"
        android:host="www.b4x.com" />
</intent-filter>)
The above example will handle any Uri that points to a page starting with ht*p://www.b4x.com.

Call NdefRecord.GetAsUriType to convert the payload to the Uri.

Testing NFC tags
I recommend you to use an application such as NFC TagWriter by NXP.
It allows you to store data formatted as Ndef on many types of tags.

NFC TagInfo is also useful to find the stored data and the mime type.

The library is available here: http://www.b4x.com/forum/additional-libraries-official-updates/14933-nfc-library.html
 

bluedude

Well-Known Member
Licensed User
Longtime User
Nfc

How can I check if it has read an URL or a text? How do I know when to use GetAsTextType or GetAsUriType?
 

bluedude

Well-Known Member
Licensed User
Longtime User
Nfc

Not sure, my app. supports different types and unfortunately I want to use one activity.

Other ways to check the type? Maybe reading the payload?

Cheers,
 

manios

Active Member
Licensed User
Longtime User
Demo App

Hi Erel,

I am having problems to get a small demo for the NFC functions to work. Could you provide a small demo app which would show how to use the library correctly?

Thanks in advance!
 

bluedude

Well-Known Member
Licensed User
Longtime User
NFC.IsNdefIntent(Activity.GetStartingIntent) always true

Hi after detecting an NFC tag ones the following is always true in Activity Resume even when not detecting a new tag again.

If NFC.IsNdefIntent(Activity.GetStartingIntent) Then

How can I solve this?

Cheers,
 

bluedude

Well-Known Member
Licensed User
Longtime User
One addition to it, i'm using GetPayLoad to get the data because i'm using my own protocols on top of text and uri.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Mm, probably my code somewhere because in the sample you provided it works fine.

Need to solve it myself.
 

maldrovandi

Member
Licensed User
Longtime User
Why don't work?

I tried to compile this example to a version 2.3.3 on a platform Nexus 5 but I do not get any results.
When I approached the tag for his reading, will not start my program.
Therefore will not take the resume event.
Instead appears a choice of programs already installed except the mine.
Would seem to me that the program does not react with any intent whatsoever.
I edited the manifest as suggested.
Where am I wrong?
In the compiler options?
 

maldrovandi

Member
Licensed User
Longtime User
It's OK

It 'just like you said. The transponder was not formatted right. Thank you very much. See you soon.
 

hdtvirl

Active Member
Licensed User
Longtime User
Erel, is it possible to get the same info out of a NFC as the NFC Research LAB Hagenberg that you gave the url for.

I am trying to read the unique serial number out of a Transport Card, the above app reads it out but I want to be able to get the same info using a B4A app and the NFC Library is it possible using this Library ?.

I am not very well up on how these cards are formatted and what info they transmit when they come in contact with a reader.


Regards


BOB
 

joneden

Active Member
Licensed User
Longtime User
Hi Erel,

I'm hitting the same issue as bluedude mentioned (but solution never issued).

The first NFC scan works fine but then any time the Activity_Resume is called after that it triggers with the initial NFC tag id.

I'm using practically the same code that you published as a sample. It's almost as if the data isn't clearing from the queue after being used. Any pointers?

Regards Jon

B4X:
   If objNFC.IsNdefIntent( Activity.GetStartingIntent) Then
        Dim records As List = objNFC.GetNdefRecords(Activity.GetStartingIntent)
      If records.Size>0 Then
            Dim r As NdefRecord = records.Get(0)
            Dim value As String = r.GetAsTextType
         NFCScan(value)
      End If
   End If
 
Top