Android Question [Solved] B4Xpages NFC reader service

kgf

Member
Licensed User
Hi All

I am using B4XPages for a new app and would like the app to listen for NFC tags, read the tag id and then open another page etc.

I have the older NFC example working fine, but not sure where to put the NFC activity code in B4Xpages, as NFC needs an activity and the older example uses the Sub Activity_Resume. Where is the best place to place the NFC code when using B4xPages.

Also Ideally I would like it to read tags from anywhere in the app and then go to a specific page, is this possible with B4Xpages? or does it need to be on its own Page.

Anyone using NFC in B4xPages?

Thanks
 

kgf

Member
Licensed User
Thanks very much, B4XPage_Foreground did the trick, i had tried some other places but not there.

Picking up the tag now and also reading it from a second page from code in B4XMainPage

The sub and change in GetStartingIntent were all that was need to get it to work

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public Page2 As B4Xpage2
    ' NFC
    Private prevIntent As Intent
    Private nfc As NFC
    Private TagTech As TagTechnology
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Foreground ' instead of Activity_Resume
    'forces all nfc intents to be sent to this activity
    nfc.EnableForegroundDispatch
    Dim si As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent '' instead of   Activity.GetStartingIntent
    'check that the intent is a new intent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    If si.Action.EndsWith("TECH_DISCOVERED") Or si.Action.EndsWith("NDEF_DISCOVERED") Or si.Action.EndsWith("TAG_DISCOVERED") Then
        Dim techs As List = nfc.GetTechList(si)
        Log($"Techs: ${techs}"$)
        'in this case we are only accessing Ndef tags.
'        If techs.IndexOf("android.nfc.tech.Ndef") > -1 Then
'            TagTech.Initialize("TagTech", "android.nfc.tech.Ndef" , si)
'            'Connect to the tag
'            TagTech.Connect
'        Else
'            ToastMessageShow("Tag does not support Ndef.", True)
'        End If
    End If
End Sub
 
Upvote 0
Top