I was wondering if anybody has used the Foreground Dispatch System with NFC in mind.
Some months ago I tried to make a small library in order to use the NFC Foreground Dispatch System. I'm not a professional programmer and it took me quite a long time to make it working. I'm not sure if it's completely stable, but at least it works for me with a Nexus 7 tablet.
Please find attached NfcForeground library in ZIP file. Decompress the ZIP file and put the JAR and XML files in your B4A library folder.
The NfcForeground library contains NfcForeground type with following members:
- .EnableForeground => To be used in Sub Activity_Resume.
- .DisableForeground => To be used in Sub Activity_Pause.
- .NfcExists => Tests if NFC Adapter exists in the device.
- .NfcEnabled => Tests if NFC Adapter exists and is enabled (active).
Here is an example of code using this library.
Put a button "Btn_NFC" on your layout. The button will be automatically disabled by the code if the device does not have NFC function.
You can click on the button in order to access NFC settings. NFC cannot be activated programmatically, it has to be done by the user.
Sub Process_Globals
' Check if SDK >= 10, otherwise you get an error message
' Settings list: http://developer.android.com/reference/android/provider/Settings.Secure.html
Dim Obj_Phone2 As Phone ' Phone type from Phone library
Dim MobilAndroidSdk10 As Boolean : MobilAndroidSdk10 = False ' Valeur de contrôle de SDK >= 10
If Obj_Phone2.SdkVersion >= 10 Then
MobilAndroidSdk10 = True
Dim NFC1 As NFC
End If
End Sub
Sub Globals
If MobilAndroidSdk10 = True Then
Dim NfcF As NfcForeground ' NfcForeground type from NfcForeground library
End If
' Button on the layout
Dim Btn_NFC As Button
End Sub
Sub Activity_Resume
' Remove views from the layout in case the activity is restarted by NFC
' otherwise the new views come above the old ones
For i = Activity.NumberOfViews - 1 To 0 Step -1
Activity.RemoveViewAt(i)
Next
' (...Layout/views...)
' Button initially not activated (SDK < 10)
'Btn_NFC.Text = "NFC ikke tilgjengelig"
Btn_NFC.Text = "NFC not available"
Btn_NFC.Enabled = False
If MobilAndroidSdk10 = True Then
' Show/hide button if NFC adapter exists or not
Btn_NFC.Enabled = NfcF.NfcExists
If NfcF.NfcExists = False Then
'Btn_NFC.Text = "NFC ikke tilgjengelig"
Btn_NFC.Text = "NFC not available"
Else If NfcF.NfcEnabled Then
'Btn_NFC.Text = "NFC På - " & CRLF & "Hold mot NFC-merke"
Btn_NFC.Text = "NFC On - " & CRLF & "Tap NFC tag"
Btn_NFC.Color = Colors.Green
Else
'Btn_NFC.Text = "NFC Av - " & CRLF & "Åpne innstillinger"
Btn_NFC.Text = "NFC Off - " & CRLF & "Open settings"
Btn_NFC.Color = Colors.RGB(255, 127, 255)
End If
' Enable Foreground Dispatch System
NfcF.EnableForeground
' Read NFC tag
' http://www.b4x.com/forum/basic4android-getting-started-tutorials/14931-reading-ndef-data-nfc-tags.html
If NFC1.IsNdefIntent(Activity.GetStartingIntent) Then
Dim List_NdefRecords As List
List_NdefRecords = NFC1.GetNdefRecords(Activity.GetStartingIntent)
Dim NdefRecord2 As NdefRecord
NdefRecord2 = List_NdefRecords.Get(0)
Dim NfcText As String
If NdefRecord2.IsTextType Then
NfcText = NdefRecord2.GetAsTextType
' => Go to Sub using the data from the NFC tag
Sub_NFC_Found(NfcText)
Else
' Error message if wrong data format
Msgbox("Wrong data format in NFC tag", "xxxxx")
End If
End If ' End If NFC1.IsNdefIntent
End If ' End If MobilAndroidSdk10 = True
End Sub
Sub Activity_Pause(UserClosed As Boolean)
' Disable Foreground Dispatch System
If MobilAndroidSdk10 = True Then
NfcF.DisableForeground
End If
End Sub
' Action when clicking on button
Sub Btn_NFC_Click
If MobilAndroidSdk10 = True Then
' Open NFC Settings screen (not possible to activate/deactivate NFC programmatically)
Dim DoAction As Intent
DoAction.Initialize("android.settings.NFC_SETTINGS", "")
StartActivity(DoAction)
End If
End Sub
In addition you have to put following code in the Manifest Editor:
Edit 2013-07-20: This is only necessary if you want the application to be shown on the screen when you scan a NFC tag.
' Open application when using NFC
AddActivityText(activityxxxxxxxxxxxxx,
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>)
Edit 2013-07-20:
"activityxxxxxxxxxxxxx" is the name of the activity to be opened.
NB: You don't need this code in the Manifest Editor if you only want to use the NFC tag information when the application/activity is already active in foreground.
I hope my example is understandable and that you will be able to adapt it to your project
As mentioned earlier this example works for me, but I guess it's possible to improve the code. Your comments are welcome
If a library has to be written, then it looks like I will have to find somebody in this community to write the library for me and pay them for doing so.
If I tried to count the number of hours I have used on this library, then it would be quite expensive for you...
I have myself used libraries made by other users in this forum, so now it is my turn to come with my contribution and give a library to other users.