Android Question Zebra Datawedge usage

fabton1963

Member
Licensed User
Longtime User
Good morning everyone.
I have developed an application that uses DataWedge and broadcast intents to read barcodes.
I used the BroadcastReceiver library to intercept the intent sent by DataWedge, and everything works fine on devices running Android 11.
However, on new devices with Android 13, I no longer receive the broadcast intents.
I also tried what Erel suggested regarding RegisterReceiver, but without success.
The following code regards to ZebraSVC


ZebraSVC:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    Dim Broadcast As BroadCastReceiver
End Sub
Sub Service_Create
    Broadcast.Initialize("BroadcastReceiver" )
End Sub
Sub Service_Start (StartingIntent As Intent)
    Broadcast.addAction("com.miocoso.DATAWEDGE")
    Broadcast.addCategory("android.intent.category.DEFAULT")
    Broadcast.SetPriority(2147483647)
    RegisterReceiver(Broadcast,False)
    
    'Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
    CallSubDelayed2(Main,"show_barcode_icon",True)
End Sub
Sub Service_Destroy
    unRegisterReceiver (Broadcast)
End Sub

Sub BroadcastReceiver_OnReceive (context As Object, i As Object )
    Dim action As String = context
    Log($"BroadcastReceiver_OnReceive(${action})"$)

    Log(i)

    Dim intent As Intent = i
    
    If intent.HasExtra("com.motorolasolutions.emdk.datawedge.data_string") Then
        Log("emdkDataString = "&intent.GetExtra("com.motorolasolutions.emdk.datawedge.data_string"))
        CallSubDelayed2(Main, "read_item_barcode", intent.GetExtra("com.motorolasolutions.emdk.datawedge.data_string"))
    else if intent.HasExtra("com.symbol.datawedge.data_string") Then
        Log("emdkDataString = "&intent.GetExtra("com.symbol.datawedge.data_string"))
        CallSubDelayed2(Main, "read_item_barcode", intent.GetExtra("com.symbol.datawedge.data_string"))
        
    End If

End Sub

Private Sub RegisterReceiver(br As BroadCastReceiver, Exported As Boolean)
    Dim p As Phone
    If p.SdkVersion < 33 Then
        br.registerReceiver("")
    Else
        Dim r As Reflector
        r.Target = br
        Dim filter As Object = r.GetField("filter")
        Dim receiver As Object = r.GetField("receiver")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethod("registerReceiver", Array(receiver, filter, IIf(Exported, 2, 4)))
        r.SetField("isRegistered", "true", "java.lang.boolean")
    End If
End Sub



Sub unRegisterReceiver(pBR As BroadCastReceiver)

    Dim p As Phone
    If p.SdkVersion < 33 Then
        pBR.unregisterReceiver
    Else
        Dim r As Reflector
        r.Target = pBR
        Dim receiver As Object = r.GetField("receiver")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethod("unregisterReceiver", Array(receiver))
        r.SetField("isRegistered", "false", "java.lang.boolean")
    End If
End Sub
Manifest:

Manifest:
AddReceiverText(ZebraSVC,
<intent-filter>
    <action android:name="com.miocoso.DATAWEDGE" />
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
)

Any idea how to solve?
 

fabton1963

Member
Licensed User
Longtime User
No, doesn't work.
I rebuild my app using receiver, I modify datawedge demo profile to send intent to my application.
When I start my application the zebra scanner is enabled but my receiver does not Receive anything.
I also try to change the four option that datawedge use to publish intent [startAtivity, startService, startForegroundSevice, broadcast]

Manifest:
AddManifestText(
<uses-permission android:name="com.symbol.emdk.permission.EMDK" />
)
AddApplicationText(
<uses-library android:name="com.symbol.emdk" />
)
' ZEBRA
AddReceiverText(ZebraReceiver,
<intent-filter>
    <action android:name="com.symbol.datawedge.DWELMAC"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
)


ZebraReceiver:
Sub Process_Globals
    
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    Log("Ricevuto: " & StartingIntent.Action)
'    If StartingIntent.HasExtra("com.motorolasolutions.emdk.datawedge.data_string") Then
'        Log("emdkDataString = "&StartingIntent.GetExtra("com.motorolasolutions.emdk.datawedge.data_string"))
'        CallSubDelayed2(B4XPages.MainPage, "read_item_barcode", StartingIntent.GetExtra("com.motorolasolutions.emdk.datawedge.data_string"))
'    End If
End Sub
 
Upvote 0

calloatti

Member
Licensed User
Did you solve this? I have it working fine in Zebra TC21 with Android 13, wrote the app years ago and upgraded Android a couple of times, everything works fine.

Can post my code if you still need it.
 
Upvote 0

calloatti

Member
Licensed User
I just noticed you replied an hour ago, so I guess you still need a solution.

In datawedge, I modified the default profile, (in Spanish):

Salida de Intent
Activada (checked)
Acción de intent = scanner (use whatever you want as a string here)
Envio de Intent: Emitir intent

In the B4xpage I have this code:

B4X:
Sub Class_Globals

Private bcr As BroadCastReceiver

End Sub

Private Sub B4XPage_Created (Root1 As B4XView)

    bcr.Initialize("bcr")
        
End Sub

private Sub B4XPage_Appear
    
    bcr.registerReceiver("scanner") 'CONFIGURE ACTION IN DATAWEDGE

End Sub

private Sub B4XPage_Disappear
        
    bcr.unregisterReceiver

End Sub

private Sub bcr_OnReceive (action As String, o As Object)

    Private Intent As Intent = o
    
    Private Barcode As String

    Log(Intent.ExtrasToString)
    
    Barcode = Intent.GetExtra("com.symbol.datawedge.data_string")
    
    Log(Barcode)
            
    barcode_event(Barcode)
    
End Sub
 
Upvote 0

fabton1963

Member
Licensed User
Longtime User
Thank you for the suggestion. In the previous version of my app running on the TC26, I was using a BroadcastReceiver registered within a service, but once I installed it on the TC27, it stopped receiving the intents.
From what I see in the code you proposed, the BroadcastReceiver seems to be registered within a b4xpage — I’ll try this approach now and let you know how it goes.
 
Upvote 0

fabton1963

Member
Licensed User
Longtime User
Did you solve this? I have it working fine in Zebra TC21 with Android 13, wrote the app years ago and upgraded Android a couple of times, everything works fine.

Can post my code if you still need it.
Thank you, I solved .

It works also with service module.
I removed from manifest any reference to the scanner intent ( I'm using broadcast )
I use a custom datawedge profile
Associated applications ( i choose my app not needed if I use deafault profile and just enable intent)
Send Intent ENABLED
Intent action com.mycompany.scanner
distribution Broadcast

my previous wrong code

B4X:
Sub Service_Start (StartingIntent As Intent)

    Broadcast.addAction("com.mycompany.scanner")
    Broadcast.addAction("com.symbol.datawedge.api.ACTION")
    Broadcast.addAction("com.symbol.datawedge.api.NOTIFICATION_ACTION")
    Broadcast.addAction("com.symbol.datawedge.api.RESULT_ACTION")

    Broadcast.addCategory("android.intent.category.DEFAULT")
    Broadcast.SetPriority(2147483647)
    Broadcast.registerReceiver("")
    
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
    CallSubDelayed2(Main,"show_barcode_icon",True)
End Sub
the working code
B4X:
Sub Service_Start (StartingIntent As Intent)

    Broadcast.registerReceiver("com.mycompany.scanner")
    
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
    CallSubDelayed2(Main,"show_barcode_icon",True)
End Sub
 
Upvote 0
Top