I am using a Zebra TC26 scanner and want to create a background java service that can trigger scans and read barcodes.
I have a app that has no activity and only one service (this service is a middleman between my app and the zebra scanner api). I implemented a BroadcastReceiver that seems to be working for action results such as the soft scan trigger result and the config result. Send a SOFT_SCAN_TRIGGER action will start scanning but I never receive a result with the barcode. Only the command results.
When I run the same code in a separate app with an activity it works and i reveive the scanned code. What do I need to do so I can receive the scanned code with this service that has no activity?
public class MyService extends Service {
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String decodedSource = intent.getStringExtra(SOURCE);
String decodedData = intent.getStringExtra(DATA_STRING);
// ...
}
};
// ...
private void startListening() {
IntentFilter filter = new IntentFilter();
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addAction(MY_ACTION);
registerReceiver(broadcastReceiver, filter);
}
public void triggerScan() {
Intent i = new Intent();
i.setAction(ACTION);
i.putExtra(SWITCH_PROFILE, MY_PROFILE);
i.putExtra("SEND_RESULT","true");
i.putExtra(SOFT_SCAN_TRIGGER, "START_SCANNING");
this.sendBroadcast(i);
}
// ...
private void initConfiguration(){
Bundle bundleMain = new Bundle();
// profile name and state
bundleMain.putString("PROFILE_NAME", MY_PROFILE);
bundleMain.putString("PROFILE_ENABLED","true");
bundleMain.putString("CONFIG_MODE", "UPDATE");
// Associate profile with this app
Bundle appConfig = new Bundle();
appConfig.putString("PACKAGE_NAME", getPackageName());
appConfig.putStringArray("ACTIVITY_LIST", new String[]{"*"});
bundleMain.putParcelableArray("APP_LIST", new Bundle[]{appConfig});
// Configure intent output for captured data to be sent to this app
Bundle bundleIntentOutConfig = new Bundle();
bundleIntentOutConfig.putString("PLUGIN_NAME", "INTENT");
bundleIntentOutConfig.putString("RESET_CONFIG", "false");
// Param list properties
Bundle bundleIntentParams = new Bundle();
bundleIntentParams.putString("intent_output_enabled", "true");
bundleIntentParams.putString("intent_action", MY_ACTION);
bundleIntentParams.putString("intent_category", Intent.CATEGORY_DEFAULT);
bundleIntentParams.putString("intent_delivery", "2"); //0 - activity, 1 - service, 2 - Broadcast
bundleIntentOutConfig.putBundle("PARAM_LIST", bundleIntentParams);
ArrayList<Bundle> bundlePluginConfig = new ArrayList<>();
bundlePluginConfig.add(bundleIntentOutConfig);
bundleMain.putParcelableArrayList("PLUGIN_CONFIG", bundlePluginConfig);
Intent bundleSetConfig = new Intent();
bundleSetConfig.setAction(ACTION);
bundleSetConfig.putExtra(SET_CONFIG, bundleMain);
bundleSetConfig.putExtra("SEND_RESULT", "COMPLETE_RESULT"); //Supported values: NONE, LAST_RESULT, COMPLETE_RESULT
bundleSetConfig.putExtra("COMMAND_IDENTIFIER", "INTENT_API");
}
}
and
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
</application>