0

我正在使用 Zebra TC26 扫描仪,并希望创建一个可以触发扫描和读取条形码的后台 java 服务。

我有一个没有活动且只有一个服务的应用程序(该服务是我的应用程序和斑马扫描仪 api 之间的中间人)。我实现了一个似乎适用于操作结果的 BroadcastReceiver,例如软扫描触发结果和配置结果。发送 SOFT_SCAN_TRIGGER 操作将开始扫描,但我从未收到带有条形码的结果。只有命令结果。

当我在具有活动的单独应用程序中运行相同的代码时,它可以工作,并且我会收到扫描的代码。我需要做什么才能通过没有活动的服务接收扫描的代码?

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");
    }
}

<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>
4

0 回答 0