Android Tutorial Reading NDEF data from NFC tags

Similar threads

B4A Tutorial NFC - Reading and Writing
B4A Tutorial Android Beam Tutorial
B4A Question NFC/NDEF newbie question
B4A Library NFC library
B4A Question cannot read and write to NFC card

NeoTechni

Well-Known Member
Licensed 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
 

Erel

Administrator
Staff member
Licensed User
Your app will only show if the data in the tag is in NDEF format and it holds a URL with www.basic4ppc.com as the host. This is configured in the manifest file.

Try to use the app I posted above to create such a tag and test it.
 

NeoTechni

Well-Known Member
Licensed 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)
 

Erel

Administrator
Staff member
Licensed User
You should set the correct intent filters based on the tag data. It will only work if the tag is formatted as NDEF tag.
 

ferdztech

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

Erel

Administrator
Staff member
Licensed User
Try the first combination instead of the second (the commented one).

Also add this line to the manifest editor:
B4X:
SetActivityAttribute(main, android:showOnLockScreen, "true")
 

aeropic

Active Member
Licensed 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/2014/10/android-lollipops-smart-lock-allows-bluetooth-nfc-nearby-automatic-device-unlocking.html
 

freedom2000

Well-Known Member
Licensed User

vincentehsu

Member
Licensed 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?
 

Erel

Administrator
Staff member
Licensed User
Probably no. You can use one of the tag writers apps from Google Play to find more details about the tag.
 

wildfandango

Member
Licensed 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.basic4ppc.com" />
</intent-filter>)
The above example will handle any Uri that points to a page starting with ht*p://www.basic4ppc.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.basic4ppc.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...
 
Top