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
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Im not trying to make an app to write data, the app i got the data from in the link already does that. Im trying to make an app to use the data.
The url is a placeholder, just to keep the code

When i scan a tag, my app does not show up in the list of apps that can receive the data
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I can't create a tag, I'm trying to read the data on an existing one (it came out for a game recently)
 

ferdztech

Member
Licensed User
Longtime User
How to write/store data in NFC card. which version of NFC library support write method. I need to store data .
 

aeropic

Active Member
Licensed User
Longtime User
I'm playing around with NFC librairy and some tags.
It's amazing how simple it is to get it working. Many thanks for this librairy !

I'm trying to use it from a locked device. I hoped that the nfc tag could be read just when switching the screen on and that the NFC intent could wake up an activity to which I would give the "setshowwhenlocked" rights with the reflection
B4X:
Sub SetShowWhenLocked
   Dim r As Reflector
   r.Target = r.GetActivity
   r.Target = r.RunMethod("getWindow")
'  r.RunMethod2("addFlags", 6815872, "java.lang.int") 'combination of: FLAG_TURN_SCREEN_ON, FLAG_DISMISS_KEYGUARD, FLAG_SHOW_WHEN_LOCKED and FLAG_KEEP_SCREEN_ON.
   r.RunMethod2("addFlags", 2621440, "java.lang.int") 'combination of: FLAG_TURN_SCREEN_ON,  FLAG_SHOW_WHEN_LOCKED (API 5)
End Sub

But this does not work :(

Does anybody know a way to do it ?

Thanks
Alain
 

aeropic

Active Member
Licensed User
Longtime User
Thanks Erel for your answer.
Trying the first combination does not solve the problem
and when adding the line in the manifest I get a compile error :
B4X:
AndroidManifest.xml:22: error: No resource identifier found for attribute 'showOnLockScreen' in package 'android'
I do not know this attribute, how does it work ?

Nevertheless, I've the feeling NFC is blocked in the android system both during sleep and when screen is locked.
see http://www.androidauthority.com/galaxy-s3-i9300-nfc-lockscreen-screen-off-mod-125744/

XDA developpers have issued a mod working only on rooted phones...

The good news is that Lollipop is supposed to give more freedom to trusted devices (bluetooth but also NFC):
http://www.androidheadlines.com/201...th-nfc-nearby-automatic-device-unlocking.html
 

vincentehsu

Member
Licensed User
Longtime User
Dear All:
I've try this code via my Samsung Galaxy Note3
B4X:
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

Why NFC.IsNdefIntent(Activity.GetStartingIntent) always false?
I use the quick pass credit card as my nfc tag,is this an NDEF tags?
 

wildfandango

Member
Licensed User
Longtime User
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

Hi Erel, one more time thx in advanc for this library...

How i can define a intent to capture the intent for reading EMPTY NFC "devices"?

i have tried

B4X:
AddActivityText(main, <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <action android:name="android.nfc.action.TAG_DISCOVERED"/>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain"/>
</intent-filter>)

but not works...
 

DonManfred

Expert
Licensed User
Longtime User
See post #1
 
Top