使用 DataWedge API

DataWedge 7.4

概述

DataWedge API 主要通过特定于 Android Intent 的命令进行操作,其他应用程序可使用这些命令来控制数据捕获,而无需直接访问底层硬件 API。本指南描述了 DataWedge 所支持 Intent 的功能以及其对数据捕获以及 DataWedge 配置文件和设置的影响。

在 DataWedge 6.2 之前的版本中,应用程序通过广播某种 Intent 访问 DataWedge API,并使用 Intent(操作和数据)中的主要信息片段来指定要执行的 API 函数。DataWedge 6.2 及更高版本将 Intent 作为操作 Intent 的额外项进行实施,允许使用单个 Intent 操作将多个 API 调用作为额外项发送。

另请参阅:


要求

使用 DataWedge API 需要有 Java 编程经验并且熟悉 Android Intent。还需要了解 DataWedge 的用法、功能和术语。有关 DataWedge 的更多信息,请参阅 DataWedge 设置指南和体系结构概述。它还可能有助于阅读 Zebra 设备随附“集成商指南”的 DataWedge 部分。

发送 Intent

DataWedge 6.2 中定义的新语法允许多个 DataWedge API 调用作为单个 Intent 操作上的额外项。语法如下所示:

// Send multiple intents as extras Intent i = new Intent(); i.setAction("com.symbol.datawedge.api.ACTION"); String[] profiles = {"MainInventory"}; i.putExtra("com.symbol.datawedge.api.DELETE_PROFILE", profiles); i.putExtra("com.symbol.datawedge.api.GET_VERSION_INFO", "");

接收结果

对于查询 DataWedge 信息的 Intent(例如在“GET_ACTIVE_PROFILE”中),该应用程序必须进行注册,才能使用标识结果 Intent 操作和类别的过滤器来接收结果。下面的代码显示如何注册广播接收器以接收结果:

// Register broadcast receiver and filter results void registerReceivers() { IntentFilter filter = new IntentFilter(); filter.addAction("com.symbol.datawedge.api.RESULT_ACTION"); filter.addCategory("android.intent.category.DEFAULT"); registerReceiver(mybroadcastReceiver, filter); } //Receiving the result private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent){ Bundle extras = getIntent().getExtras(); if (intent.hasExtra("com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE")){ String activeProfile = extras.getString("com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE");

重要说明:DataWedge API 命令不进行排队并且可能会被忽略(如果在 DataWedge 忙于处理较早 Intent 时发送)。当发送 API 命令时,只有在不忙于执行其他操作时,DataWedge 才执行该命令。例外:

  • STOP_SCANNING - 立即中断扫描操作
  • DISABLE_PLUGIN - 立即禁用当前的扫描器输入插件

为了帮助确保正确执行,Zebra 建议在关键命令之前插入延迟代码。有关示例,请参见 SoftScanTrigger API。

嵌套捆绑包

DataWedge 6.3 实施嵌套捆绑包的概念,嵌套捆绑包允许将值的“捆绑包”作为一个值包含在另一捆绑包中。捆绑包也可以是多层深的。例如,下图阐释了 PARAM_LIST 捆绑包嵌套在 PLUGIN_CONFIG[0] 中,后者嵌套在 API 调用 SET_CONFIG 中。嵌套是通过 Intent 配置在配置文件中包含的许多参数所必需的。

该图像进一步说明 SET_CONFIG API 调用可以实施第二个嵌套捆绑包 PLUGIN_CONFIG[n],其中可以包含它自己的 PARAM_LIST。


示例

下面的 Java 代码实施嵌套捆绑包。

//Using the SET_CONFIG API and a nested bundle. Bundle bMain = new Bundle(); bMain.putString("PROFILE_NAME","Profile2"); bMain.putString("PROFILE_ENABLED", "true"); bMain.putString("CONFIG_MODE","CREATE_IF_NOT_EXIST"); bMain.putString("RESET_CONFIG", "true"); Bundle bundleApp1 = new Bundle(); bundleApp1.putString("PACKAGE_NAME","com.symbol.emdk.simulscansample1"); bundleApp1.putStringArray("ACTIVITY_LIST", new String[]{ "com.symbol.emdk.simulscansample1.DeviceControl", "com.symbol.emdk.simulscansample1.MainActivity", "com.symbol.emdk.simulscansample1.ResultsActivity.*", "com.symbol.emdk.simulscansample1.ResultsActivity2", "com.symbol.emdk.simulscansample1.SettingsFragment1"}); Bundle bundleApp2 = new Bundle(); bundleApp2.putString("PACKAGE_NAME","com.example.intents.datawedgeintent"); bundleApp2.putStringArray("ACTIVITY_LIST", new String[]{ "com.example.intents.datawedgeintent.DeviceControl", "com.example.intents.datawedgeintent.MainActivity", "com.example.intents.datawedgeintent.ResultsActivity", "com.example.intents.datawedgeintent.SettingsFragment1"}); Bundle bundleApp3 = new Bundle(); bundleApp3.putString("PACKAGE_NAME","*"); bundleApp3.putStringArray("ACTIVITY_LIST", new String[]{"*"}); Bundle bundleApp4 = new Bundle(); bundleApp4.putString("PACKAGE_NAME","com.symbol.myzebraapp"); bundleApp4.putStringArray("ACTIVITY_LIST", new String[]{"*"}); bMain.putParcelableArray("APP_LIST", new Bundle[]{ bundleApp1 ,bundleApp2 ,bundleApp3 ,bundleApp4 }); Intent i = new Intent(); i.setAction("com.symbol.datawedge.api.ACTION"); i.putExtra("com.symbol.datawedge.api.SET_CONFIG", bMain); this.sendBroadcast(i);

相关指南:

另请参阅:

Zebra 支持中心 | 集成商指南、产品手册、软件下载和支持

LaunchPad | Zebra 开发人员社区

Intent | Android 开发人员

Intent 和 Intent 过滤器 | Android 开发人员

Android Intent | 教程