Using the Foreground Dispatch System for NFC

Peter Simpson

Expert
Licensed User
Longtime User
Hello everybody,
I was wondering if anybody has used the Foreground Dispatch System with NFC in mind. If yes, how did you manage to do it?

If no and and 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.

Okay, thank you for reading my post :sign0085:
 

raphaelcno

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

B4X:
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.

B4X:
' 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.
 

Attachments

  • NfcForeground.zip
    3 KB · Views: 296
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
DAM, IT WORKED FIRST TIME OF ASKING :)

Wow Raphaelcno, thank you very very very much.

Erel, may I suggest that you post this library in your library section, as this has done exactly what I've been looking for. This allows me to handle NFC TAGS within my own active Android application and IT DOES NOT even start the NFC service in the background anymore.

THIS IS ABSOLUTELY AWESOME

Thank you once again, all I need now is a library for PostgreSQL database and I'm sorted. But I'm probably going to end up using a PHP script.

It worked on my GNex, Nex7 and Nex4.

Cheers...
 
Last edited:
Upvote 0
Top