NFC issue

joneden

Active Member
Licensed User
Longtime User
Hi All,

I've just started working with NFC tags and have got it so that users can sign into my app using their NFC contact badge. Great. Now that I am looking at some more activities that share another set of NFC tags I'm hitting an issue. Each time I scan a tag the original login activity loads. I need the NFC scan to be handled by the current activity.

Would the solution be to run a service in the app that gets the NFC scan, then depending on the current activity pass the scan to that activity?

The NFC tutorial only covers a single activity....

Regards,

Jon
 

joneden

Active Member
Licensed User
Longtime User
Can't be done in a service.

If anyone does the same thing create a dummy activity that handles things.

B4X:
'Activity module
Sub Process_Globals
   Dim CurrentActivity As String
End Sub

Sub Globals
   Dim objNFC As NFC
End Sub

Sub Activity_Create(FirstTime As Boolean)
End Sub

Sub Activity_Resume
   Log("ActivityNFCHandler Started")
   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
         Select Case CurrentActivity
         Case "Main" : CallSubDelayed2(Main,"NFCScan",value)
         Case "ActivityRoutineAdmin" : CallSubDelayed2(ActivityRoutineAdmin,"NFCScan",value)
         End Select
         CurrentActivity = ""
         Activity.Finish()
      End If
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

You'll also want to add this to the Manifest
B4X:
AddActivityText("ActivityNFCHandler", <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" />
</intent-filter>)

SetActivityAttribute("ActivityNFCHandler", android:theme, @android:style/Theme.Translucent.NoTitleBar)

Now to figure out a good way to stop the redisplay in the activity after the NFC scan!
 
Upvote 0

joneden

Active Member
Licensed User
Longtime User
Looks like it would be very handy. I haven't gone back over my code but it seemed to work. The FDS approach would add an extra level of surety that it would work and possibly eliminate the redraw issue that I'm having.
 
Upvote 0

wolvo66

Member
Licensed User
Longtime User
Did either of you succeed with this?
I'm starting to look at the Foreground Dispatch System library approach (also my 1st library)
- Ashley
 
Upvote 0
Top