Android Question NFC to UDP in the background

0slawek

New Member
Licensed User
Hey
I have an application that uses UDP to control lighting at home. You can run it and take advantage of more complex functions, and you can use the widget which is quite convenient. But I would like to expand it with the ability to send commands when the phone reads the NFC tag. I would not want the reading of the NFC tag to launch the main application window, but only to let the application run in the background, check the correctness of the serial number, send a record via UDP and terminate the operation. What should I do to get this action?
 

DonManfred

Expert
Licensed User
Longtime User
I would not want the reading of the NFC tag to launch the main application window
you need as the NFC needs an Activity. So it can not be done in Background.

BUT: Nobody stopps you from just read the NFC in your activity, do your work (check the correctness of the serial number, send a record via UDP and terminate the operation) and then leave your activity again.
 
Upvote 0

0slawek

New Member
Licensed User
ok, can I open other activity (not the main one)?
what should I put in the manifest so that when the NFC tag is read, the application has been launched?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I don´t know.
In the starterservice you can check the startingintent. Maybe you can check the content and - in case of an NFC - you start the activity you want.
But for this you probably need to setup a static intent filter. If this is possible with NFC i do not know.

you can try to setup it like this in your manifest
B4X:
AddApplicationText(
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
)

and in starter service

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private prevIntent As Intent

End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

End Sub

Sub Service_Start (StartingIntent As Intent)
    Dim si As Intent = StartingIntent
    '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
        StartActivity(MyNVFCActivity) ' In MyNVFCActivity you scan the NFC, get the taglst, check it and so on...
    End If

End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Based on https://stackoverflow.com/questions...o-show-my-application-when-nfc-discover-a-tag
it seems that you also need to create a XML with the tech lists...

try this (not sure if it works!)

B4X:
AddApplicationText(
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                         android:resource="@xml/nfc_tech_list" />

)

CreateResource(xml,nfc_tech_list.xml,<resources>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>

    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>

    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
</resources>)
 
Last edited:
Upvote 0

wes58

Active Member
Licensed User
Longtime User
What I did a couple of years ago is. In my application I added a module which I called nfcGet. In this module I have the following code:
The application doesn't have to be in foreground - just phone unlocked. And reading the tag doesn't start the main application.
In the widgetervice (you can have any other running service) I process the data ( CallSubDelayed2(widgetservice, "ReadNFCtag", r.GetAsTextType) ) and send udp message to turn the light on/off.

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim LastIntent As Intent

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim nfc_Tag As NFC
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
 
End Sub

Sub Activity_Resume
    If Activity.GetStartingIntent = LastIntent Then Return
   
    If nfc_Tag.IsNdefIntent(Activity.GetStartingIntent) Then
        Dim records As List
        Dim r As NdefRecord
        records = nfc_Tag.GetNdefRecords(Activity.GetStartingIntent)
        r = records.Get(0)
        CallSubDelayed2(widgetservice, "ReadNFCtag", r.GetAsTextType)
        Log("nfc " & r.GetAsTextType)
    End If
    LastIntent = Activity.GetStartingIntent
    Activity.Finish
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

In the manifest i have:
B4X:
AddActivityText(nfcGet,
    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="*/*" />
    </intent-filter>)

It has been working for a couple of years now.
 
Upvote 0

Similar Threads

Replies
14
Views
39K
  • Article
Android Code Snippet UDP Broadcast Address
Replies
0
Views
8K
Replies
64
Views
48K
Top