1

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>
sainupangad
  • 115
  • 9
Yellown
  • 426
  • 2
  • 6
  • 16
  • Is your service definitely running when you scan a barcode? You would be better off configuring DataWedge to StartService rather than sending a broadcast, then if it is not running, your service will be started. – Darryn Campbell Mar 15 '21 at 07:39
  • @DarrynCampbell thank you for the comment. The service is running. I can see the log with logcat. And as i said i can get the scan and config action results but not the scaned code. I wanted to do it this way because my service is also used by another app. But ill try it it works with start service. – Yellown Mar 15 '21 at 08:17
  • I would just be worried that Android was going to unregister my broadcast receiver since it was registered in the background or to kill the background service. One thing I notice is that you are registering a single action but typically the command result and scanned barcodes would be received with separate actions. – Darryn Campbell Mar 15 '21 at 16:10

0 Answers0