Delphi Android mobile application is build with Delphi Alexandria (Delphi 11.1).
I receive broadcast from Zebra barcode scanner device.
I try to receive broadcast from Sunmi device and Unitech device, and it does not work.
Both Sunmi and Unitech are configured to output barcode using broadcast.
Application Manifest file :
<!-- Zebra - OK -->
<intent-filter>
<action android:name="fr.XXX.YYY.ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<!-- SUNMI - Ko -->
<action android:name="com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<!-- Unitech - Ko -->
<action android:name="unitech.scanservice.data" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
In my code I register the intents :
MainActivity.registerIntentAction(StringToJString(bcZebra));
MainActivity.registerIntentAction(StringToJString(bcSunmi));
MainActivity.registerIntentAction(StringToJString(bcUnitech));
Then I process broadcast this way :
function TfData.HandleIntentAction(const Data: JIntent): Boolean;
var bc : string;
begin
TOSLog.d('********* SCAN ******************');
TOSLog.d(JStringToString(Data.getAction));
Result := False;
if Data <> nil then begin
// Zebra, bcZebraData = com.symbol.datawedge.data_string - Ok
if StringToJString(bcZebra).equals(Data.getAction) then begin
bc := JStringToString(Data.getStringExtra(StringToJString(bcZebraData)));
result := true;
bcScan(bc);
end
// Sunmi, bcSunmiData = 'data' - Ko
else if StringToJString(bcSunmi).equals(Data.getAction) then begin
bc := JStringToString(Data.getStringExtra(StringToJString(bcSunmiData)));
result := true;
bcScan(bc);
end
// Unitech, bcUnitechData = 'text' - Ko
else if StringToJString(bcUnitech).equals(Data.getAction) then begin
bc := JStringToString(Data.getStringExtra(StringToJString(bcUnitechData)));
result := true;
bcScan(bc);
end;
end;
end;
When I scan a barcode with a Zebra device, I can see that in log console :
********* SCAN ******************
fr.XXX.YYY.ACTION
I don't see it with Sunmi and Unitech, so the function is not called.
However, when I scan with the Sunmi device, I get this in general log :
Sending non-protected broadcast com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED from system 1269:com.sunmi.scanner/u0a74 pkg com.sunmi.scanner
With Unitech :
10-28 18:44:30.199: E/ActivityManager(1178): Sending non-protected broadcast unitech.scanservice.data from system 2360:com.unitech.scanservice/1000 pkg com.unitech.scanservice
I can't figure out what I missed, and as Zebra do not log anything when it sends a broadcast, I'm unable to compare the 2 other devices.
CodePudding user response:
A quick glance of the docs for both Sunmi and Unitech suggests that you need to do it using a BroadcastReceiver that gets registered, with the appropriate actions. The following code is based around what is in the old Kastri Free repo.
The first is a unit that defines a class that will handle any actions:
unit MultiReceiver;
interface
uses
Androidapi.JNIBridge, Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Embarcadero;
type
TMultiReceiver = class;
TMultiReceiverListener = class(TJavaLocal, JFMXBroadcastReceiverListener)
private
FMultiReceiver: TMultiReceiver;
public
{ JFMXBroadcastReceiverListener }
procedure onReceive(context: JContext; intent: JIntent); cdecl;
public
constructor Create(const AMultiReceiver: TMultiReceiver);
end;
TIntentEvent = procedure(Sender: TObject; const Intent: JIntent) of object;
TMultiReceiver = class(TObject)
private
FBroadcastReceiver: JFMXBroadcastReceiver;
FIntentFilter: JIntentFilter;
FReceiverListener: TMultiReceiverListener;
FOnIntent: TIntentEvent;
protected
procedure Receive(context: JContext; intent: JIntent); virtual;
procedure ConfigureActions; virtual; abstract;
property IntentFilter: JIntentFilter read FIntentFilter;
public
constructor Create;
destructor Destroy; override;
property OnIntent: TIntentEvent read FOnIntent write FOnIntent;
end;
implementation
uses
Androidapi.Helpers;
{ TMultiReceiverListener }
constructor TMultiReceiverListener.Create(const AMultiReceiver: TMultiReceiver);
begin
inherited Create;
FMultiReceiver := AMultiReceiver;
end;
procedure TMultiReceiverListener.onReceive(context: JContext; intent: JIntent);
begin
FMultiReceiver.Receive(context, intent);
end;
{ TMultiReceiver }
constructor TMultiReceiver.Create;
begin
inherited Create;
FReceiverListener := TMultiReceiverListener.Create(Self);
FBroadcastReceiver := TJFMXBroadcastReceiver.JavaClass.init(FReceiverListener);
FIntentFilter := TJIntentFilter.JavaClass.init;
ConfigureActions;
TAndroidHelper.Context.registerReceiver(FBroadcastReceiver, FIntentFilter)
end;
destructor TMultiReceiver.Destroy;
begin
TAndroidHelper.Context.unregisterReceiver(FBroadcastReceiver)
end;
procedure TMultiReceiver.Receive(context: JContext; intent: JIntent);
begin
if Assigned(FOnIntent) then
FOnIntent(Self, intent);
end;
end.
Then an example of how you might use it:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes,
MultiReceiver;
type
TScannerReceiver = class(TMultiReceiver)
protected
procedure ConfigureActions; override;
end;
TForm1 = class(TForm)
private
FScannerReceiver: TScannerReceiver;
procedure ScannerReceiverIntentHandler(Sender: TObject; const AIntent: JIntent);
public
constructor Create(AOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses
Androidapi.Helpers;
const
cActionSunmi = 'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED';
cActionUnitech = 'unitech.scanservice.data';
{ TScannerReceiver }
procedure TScannerReceiver.ConfigureActions;
begin
IntentFilter.addAction(StringToJString(cActionSunmi));
IntentFilter.addAction(StringToJString(cActionUnitech));
end;
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
FScannerReceiver := TScannerReceiver.Create;
FScannerReceiver.OnIntent := ScannerReceiverIntentHandler;
end;
procedure TForm1.ScannerReceiverIntentHandler(Sender: TObject; const AIntent: JIntent);
begin
if AIntent.getAction.Equals(StringToJString(cActionSunmi)) then
begin
// Handle Sunmi
end
else if AIntent.getAction.Equals(StringToJString(cActionUnitech)) then
begin
// Handle Unitech
end;
end;
end.
NOTE: This is completely untested as I do not have either of the models handled by it