Android Question BroadCastReceiver: can't catch data from device

Luca Rinaldoni

Member
Licensed User
Hi!
I try to use BroadCastReceiver for a new project because I must "capture" some data that arrives from a scanner of barcode. On device's SDK, I found sample for register a broadcast receiver.

In B4A's site I had download BroadcastReceiver 2.0, but I could not make.

The SDK's sample is:

B4X:
private void registerBarcodeScannerBroadcastReceiver() { 
//Enable the Output via API mode; do not add a line feed; turn Good Read LED on 
Intent intent = new Intent ("ACTION_BAR_SCANCFG"); 
intent.putExtra("EXTRA_SCAN_MODE", 3); i
ntent.putExtra("EXTRA_SCAN_AUTOENT", 0); 
intent.putExtra("EXTRA_SCAN_NOTY_LED", 1); 

sendBroadcast(intent); registerReceiver(barcodeScannerBroadcastReceiver, new IntentFilter("nlscan.action.SCANNER_RESULT")); 
}

private void unregisterBarcodeScannerBroadcastReceiver() { unregisterReceiver(barcodeScannerBroadcastReceiver); 
}

private BroadcastReceiver barcodeScannerBroadcastReceiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
             final String scanResult_1=intent.getStringExtra("SCAN_BARCODE1"); 
             final String scanStatus = intent.getStringExtra("SCAN_STATE"); 
             if (null==scanResult_1 || null==scanStatus || scanResult_1.isEmpty() 
                             || scanStatus.isEmpty())   { 
                 return; } 
             if ("ok".equals(scanStatus)) { 
                   tvBarcode.setText(scanResult_1); 
                   int codeId = intent.getIntExtra("SCAN_BARCODE_TYPE", -1); 
                   tvCodeId.setText(""+codeId); 
              } 
      } 
};

I try to translate (like tutorial explain) in a service:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Broadcast As BroadCastReceiver
   
End Sub

Sub Service_Create
    Broadcast.Initialize("BroadcastReceiver")
End Sub

Sub Service_Start (StartingIntent As Intent)
   
'Sample from B4A   
'    Broadcast.addAction("android.provider.Telephony.SMS_RECEIVED")
'    Broadcast.addAction(Broadcast.SMS_RECEIVED)
'    Broadcast.SetPriority(2147483647)
'    Broadcast.registerReceiver("")
   
   
    Broadcast.addAction("ACTION_BAR_SCANCFG")
    Broadcast.addAction("EXTRA_SCAN_MODE=3")
    Broadcast.addAction("EXTRA_SCAN_AUTOENT=0")
    Broadcast.addAction("EXTRA_SCAN_NOTY_LED=1")
    Broadcast.registerReceiver("nlscan.action.SCANNER_RESULT")
   
   
End Sub

'Sample from B4A
'Sub BroadcastReceiver_OnReceive (Action As String)
'    ToastMessageShow(Action, False)
'    'can only abort when sendOrderedbroadcast is called.
'    Broadcast.AbortBroadcast
'   
'End Sub

Sub BroadcastReceiver_OnReceive(context As Object, intent1 As Intent)

    If (intent1.HasExtra("SCAN_BARCODE1")) Then
        Log("Found BARCODE1")
    End If
   
    Broadcast.AbortBroadcast
End Sub

Sub Service_Destroy

End Sub

I don't understand how use a "putExtra" method and how read data, using "getStringExtra" or similar.
The OnReceive event raised.

Have any idea?


Best regards!
 

Erel

Administrator
Staff member
Licensed User
What is the output of Log(intent1) and Log(intent1.ExtrasToString) ?
 

Luca Rinaldoni

Member
Licensed User
Hi Erel,

B4X:
Sub BroadcastReceiver_OnReceive(context As Object, intent1 As Intent)

    Log(intent1)
    Log(intent1.ExtrasToString)
   
    Broadcast.AbortBroadcast
End Sub
On log's tab, I can see this.

B4X:
Intent1:Intent { act=nlscan.action.SCANNER_RESULT flg=0x10 (has extras) }
-------------------
Error occurred on line: 47 (BroadcastService)
java.lang.RuntimeException: Method: ExtrasToString not found in: android.content.Intent
    at anywheresoftware.b4a.shell.Shell$MethodCache.getMethod(Shell.java:970)
    at anywheresoftware.b4a.shell.Shell.getMethod(Shell.java:623)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:709)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
    at com.rootsoft.broadcastreceiver.BroadCastReceiver$1.onReceive(BroadCastReceiver.java:110)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Pause, UserClosed = true **
** Service (broadcastservice) Destroy **
** Service (starter) Destroy **

OnReceive event is implemented on service.

Thanks, in advance...

Matteo
 

Luca Rinaldoni

Member
Licensed User
Erel,
In adding, I'm not be able to translate the SDK's sample where used "putExtra"; this method on library not exist and I using "addAction": I think that is not the same thing.

Thanks about your time...

Matteo
 

Erel

Administrator
Staff member
Licensed User
Try this:
B4X:
Sub BroadcastReceiver_OnReceive(context As Object, intent1 As Object)
Dim in As Intent = intent1
Log(in)
Log(in.ExtrasToString)
[code]
 

Luca Rinaldoni

Member
Licensed User
Hi Erel,
I've same error.

The code:
B4X:
    Dim in As Intent = intent1
    Try
        Log(in)
    Catch
        Log(LastException)
    End Try
       
    Try
        Log(in.ExtrasToString)
    Catch
        Log(LastException)
    End Try
produce this log:

Intent { act=nlscan.action.SCANNER_RESULT flg=0x10 (has extras) }

java.lang.RuntimeException: Method: ExtrasToString not found in: android.content.Intent

I think this: until I manage the putExtra, I will not have populated ExtrasToStrings.
I seen this post: https://www.b4x.com/android/forum/threads/intent-putextra-does-not-add-object-to-extras.57980/
but in BroadCastReceiver isn't implemented a similar method of "putExtra".

What's do you think?

Matteo
 

Luca Rinaldoni

Member
Licensed User
In Main activity:

B4X:
StartService(BroadcastService)

On Service_Start there is problem: I used "addAction" and not "putExtra".
The original code, from device's SDK, is:
B4X:
Intent intent = new Intent ("ACTION_BAR_SCANCFG");
intent.putExtra("EXTRA_SCAN_MODE", 3); i
ntent.putExtra("EXTRA_SCAN_AUTOENT", 0);
intent.putExtra("EXTRA_SCAN_NOTY_LED", 1);
but BroadCastReceiver library don't implemented a similar method of "putExtra".


Service code:

B4X:
#Region  Service Attributes
    #StartAtBoot: 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 Broadcast As BroadCastReceiver
  
End Sub

Sub Service_Create
    Broadcast.Initialize("BroadcastReceiver")
End Sub

Sub Service_Start (StartingIntent As Intent)
  
'Sample from B4A  
'    Broadcast.addAction("android.provider.Telephony.SMS_RECEIVED")
'    Broadcast.addAction(Broadcast.SMS_RECEIVED)
'    Broadcast.SetPriority(2147483647)
'    Broadcast.registerReceiver("")
  
  
    Broadcast.addAction("ACTION_BAR_SCANCFG")
    Broadcast.addAction("EXTRA_SCAN_MODE=3")
    Broadcast.addAction("EXTRA_SCAN_AUTOENT=0")
    Broadcast.addAction("EXTRA_SCAN_NOTY_LED=1")
    Broadcast.registerReceiver("nlscan.action.SCANNER_RESULT")
     
End Sub

'Sample from B4A
'Sub BroadcastReceiver_OnReceive (Action As String)
'    ToastMessageShow(Action, False)
'    'can only abort when sendOrderedbroadcast is called.
'    Broadcast.AbortBroadcast
'  
'End Sub

Sub BroadcastReceiver_OnReceive(context As Object, intent1 As Intent)

    Dim in As Intent = intent1
    Try
        Log(in)
    Catch
        Log(LastException)
    End Try
      
    Try
        Log(in.ExtrasToString)
    Catch
        Log(LastException)
    End Try
      
  
    Broadcast.AbortBroadcast
End Sub

Sub Service_Destroy
    Broadcast.unregisterReceiver
End Sub


Thanks for your time.

Matteo
 
Last edited by a moderator:

Luca Rinaldoni

Member
Licensed User
Which barcode scanner is it?
Last week thanks to a hint from DonManfred I was able to capture all the information that I needed from one of the latest Android 7.0 powered 1D/2D/NFC on the market.

So the intent to listen for is nlscan.action.SCANNER_RESULT

Hi Peter!
Thanks for you reply.
I try to capture data from a scans of EAN8 and EAN13. Device has installed Android 4.4.2.
If I configure it as "Simuate keystroke" data has capture, but is a workaround: with "intent" and broadcast receiver, returns other information, like type of barcode.

Matteo
 

Erel

Administrator
Staff member
Licensed User
Pay attention to my previous post (#5). The parameter type should be object.
 

Luca Rinaldoni

Member
Licensed User
Pay attention to my previous post (#5). The parameter type should be object.
My mistake, sorry! Now it's ok! I tested solution.
Many thanks!

Last thing: about "putExtra", do you have any idea for implementation?

This code (from device's SDK):
B4X:
Intent intent = new Intent ("ACTION_BAR_SCANCFG"); 
intent.putExtra("EXTRA_SCAN_MODE", 3); i
ntent.putExtra("EXTRA_SCAN_AUTOENT", 0); 
intent.putExtra("EXTRA_SCAN_NOTY_LED", 1);
I don't think that best translation, in B4A, is:

B4X:
    Broadcast.addAction("ACTION_BAR_SCANCFG")
    Broadcast.addAction("EXTRA_SCAN_MODE=3")
    Broadcast.addAction("EXTRA_SCAN_AUTOENT=0")
    Broadcast.addAction("EXTRA_SCAN_NOTY_LED=1")
    Broadcast.registerReceiver("nlscan.action.SCANNER_RESULT")

Thanks!

Matteo
 

Peter Simpson

Expert
Licensed User
So SCAN_BARCODE_TYPE returns the type of barcode you have scanned, EAN8, EAN13, QR etc but as a number, SCAN_STATE returns the current state of the scanner (I presume open/closed), so I'm going to presume that SCAN_BARCODE1 brings back the scanned barcode information?

1. Is that the entire SDK demo code in MainActivity
2. What exactly are SCAN_BARCODE1, SCAN_BARCODE_TYPE and SCAN_STATE returning for you in the B4A logs???

You should start with just registering the receiver "nlscan.action.SCANNER_RESULT" and in the _OnReceive sub just reading the basics responses.
 

Erel

Administrator
Staff member
Licensed User
Luca you are confusing two different things.
There are two things here:

1. The broadcast receiver.
2. A broadcasted intent.

The intent should be created with the Intent object and should be broadcasted with Phone.SendBroadcast.
 

Luca Rinaldoni

Member
Licensed User
So SCAN_BARCODE_TYPE returns the type of barcode you have scanned, EAN8, EAN13, QR etc but as a number, SCAN_STATE returns the current state of the scanner (I presume open/closed), so I'm going to presume that SCAN_BARCODE1 brings back the scanned barcode information?

1. Is that the entire SDK demo code in MainActivity
2. What exactly are SCAN_BARCODE1, SCAN_BARCODE_TYPE and SCAN_STATE returning for you in the B4A logs???

You should start with just registering the receiver "nlscan.action.SCANNER_RESULT" and in the _OnReceive sub just reading the basics responses.
Yes! Thanks! Now working fine. Thanks for you time.
 

Luca Rinaldoni

Member
Licensed User
Luca you are confusing two different things.
There are two things here:

1. The broadcast receiver.
2. A broadcasted intent.

The intent should be created with the Intent object and should be broadcasted with Phone.SendBroadcast.
Ok, I will modify code for test your suggest.
Many thanks for you time.
 
Top