Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Reading NDEF data from NFC tags

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-12-2012, 02:13 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default 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:
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/addit...c-library.html
Reply With Quote
  #2 (permalink)  
Old 02-12-2012, 04:21 PM
Basic4ppc Veteran
 
Join Date: Sep 2007
Posts: 480
Default Nfc

How can I check if it has read an URL or a text? How do I know when to use GetAsTextType or GetAsUriType?
Reply With Quote
  #3 (permalink)  
Old 02-12-2012, 04:35 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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.
Reply With Quote
  #4 (permalink)  
Old 02-12-2012, 04:44 PM
Basic4ppc Veteran
 
Join Date: Sep 2007
Posts: 480
Default 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,
Reply With Quote
  #5 (permalink)  
Old 02-12-2012, 04:51 PM
Basic4ppc Veteran
 
Join Date: Sep 2007
Posts: 480
Default

I guess one way is always to use text (even when it is an URL).
Reply With Quote
  #6 (permalink)  
Old 02-22-2012, 08:40 AM
Knows the basics
 
Join Date: Jun 2011
Location: Germany
Posts: 99
Default 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!
__________________
Manios
- ARCHOS 1o1 Internet Tablet
- SAMSUNG Nexus S (ICS)
Reply With Quote
  #7 (permalink)  
Old 02-23-2012, 06:49 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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.
Reply With Quote
  #8 (permalink)  
Old 03-01-2012, 10:27 AM
Basic4ppc Veteran
 
Join Date: Sep 2007
Posts: 480
Default 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,
Reply With Quote
  #9 (permalink)  
Old 03-01-2012, 10:29 AM
Basic4ppc Veteran
 
Join Date: Sep 2007
Posts: 480
Default

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.
Reply With Quote
  #10 (permalink)  
Old 03-01-2012, 10:36 AM
Basic4ppc Veteran
 
Join Date: Sep 2007
Posts: 480
Default

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

Need to solve it myself.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving and reading data (.ini file) cla721 Questions (Windows Mobile) 6 12-07-2011 10:28 AM
Storing reading data barx Basic4android Updates and Questions 2 06-25-2011 12:10 PM
reading data packets with AsyncStream theos Bugs & wishlist 1 04-22-2011 06:59 PM
reading http data nikdo Questions (Windows Mobile) 2 11-30-2009 12:31 PM
Reading MP3 ID3 tags, any ideas??? kronos79 Forum Discussion 2 11-10-2007 10:57 AM


All times are GMT. The time now is 10:10 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0