Android Tutorial Reading NDEF data from NFC tags

Discussion in 'Tutorials & Examples' started by Erel, Feb 12, 2012.

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 [solved] NFC Tag (not NDEF) reading
  1. Erel

    Erel Administrator Staff 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:
    Code:
    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:
    Code:
    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:
    Code:
    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
     
    MarcoRome and koaunglay like this.
  2. bluedude

    bluedude Well-Known Member Licensed 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?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    You can use different activities and let each activity handle a different type of data (with the intent filters).

    Though I believe that in most use cases it is not really needed. You will probably scan tags of a specific type.
     
  4. bluedude

    bluedude Well-Known Member Licensed 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,
     
  5. bluedude

    bluedude Well-Known Member Licensed User

    I guess one way is always to use text (even when it is an URL).
     
  6. manios

    manios Active Member Licensed 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!
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    The code in the first post is a small example that should work. It prints the tag value to the logs.
    If you like you can post your code (preferably in a new thread) and we will help you.
     
  8. bluedude

    bluedude Well-Known Member Licensed 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,
     
  9. bluedude

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

    bluedude Well-Known Member Licensed User

    Mm, probably my code somewhere because in the sample you provided it works fine.

    Need to solve it myself.
     
  11. maldrovandi

    maldrovandi Member Licensed 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?
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Have you formatted the tags as NDEF tags?
     
  13. maldrovandi

    maldrovandi Member Licensed User

    It's OK

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

    hdtvirl Active Member Licensed 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
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Can you give an example of missing data?

    This app probably uses a different method to read the data.
     
  16. joneden

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

    Code:
    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
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    The solution is to have a process global intent variable. You should then make sure that the StartingIntent is actually new:
    Code:
    Sub Process_Globals
     
    Private LastIntent As Intent
    End Sub

    Sub Activity_Resume
     
    If Activity.GetStartingIntent = LastIntent Then Return
     ...
     LastIntent = 
    Activity.GetStartingIntent
    End Sub
     
  18. joneden

    joneden Active Member Licensed User

    Ah yes, many thanks Erel!
     
  19. ranba790

    ranba790 Member Licensed User

    NFC Tranfer beween 2 phones

    Hi
    Any way to send data between 2 phones with NFC
    Thanks
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    This solution only reads NDEF tags, it will not allow you to send data.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice