我们目前正在使用 Zebra 设备进行公司 Assets 管理,因此我们正在开发一个小型原型(prototype) Android 应用程序来扫描 RFID 标签。我从数据楔 API 中了解到,应用程序可以获得具有 Intent 广播的扫描输出。
但是应用程序无法接收任何 Intent 。
设备:Zebra MC33
数据楔版本:7.3
我尝试过使用以下内容
配置文件设置:
Intent Action : my.prototype.app.SCAN
Intent Delivery Type: Broadcast Intent.
Intent Category: Default.
Added to Associated Apps
AndroidManifest.xml
<receiver
android:name=".ScanIntentReceiver"
android:enabled="true"
android:exported="true" />
ScanIntentReceiver.kt
abstract class ScanIntentReceiver : BroadcastReceiver() {
abstract fun onReceiveScan(data: ScannerOutput)
override fun onReceive(p0: Context?, p1: Intent?) {
Timber.d("S1: Broadcast Scan Intent Received.")
p0?.let { context ->
p1?.let { intent ->
when (intent.action) {
BuildConfig.intentAction -> {
try {
val data = parseData(intent, context)
Timber.d("Data received: $data")
onReceiveScan(data)
} catch (ex: Exception) {
Timber.d("Parsing error")
Timber.d(ex)
}
}
else -> {
Timber.d("No Suitable Action.")
}
}
}
}
}
}
还尝试使用“通过启动 Activity 发送”
配置文件设置:
Intent Action : my.prototype.app.SCAN
Intent Delivery Type: Send via StartActivity.
Intent Category: Default.
Added to Associated Apps
AndroidManifest.xml
<activity
android:name=".activity.ScanActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="${intentAction}" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
扫描 Activity .kt
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Timber.d("Received Intent via Activity.")
intent?.let {
try {
val data = parseData(it, this)
viewModel.processOutput(data)
} catch (ex: Exception) {
Timber.e(ex)
}
}
}
感谢任何帮助。提前致谢。
更新:
private fun parseData(intent: Intent, ctx: Context): ScannerOutput {
val decodedSource =
intent.getStringExtra(ctx.getString(R.string.datawedge_intent_key_source))
val decodedData =
intent.getStringExtra(ctx.getString(R.string.datawedge_intent_key_data))
val decodedLabelType =
intent.getStringExtra(ctx.getString(R.string.datawedge_intent_key_label_type))
....
}
更新:
val filter = IntentFilter()
filter.addCategory(Intent.CATEGORY_DEFAULT)
filter.addAction(BuildConfig.intentAction)
registerReceiver(scanIntentReceiver, filter)
最佳答案
让我们澄清一下。如果您想使用 MC33R 读取 RFID 标签,则必须使用 Zebra RFID API3,而不是 Intent。 Zebra 正在考虑将 Intent 也用于 RFID,但目前最好的选择是使用 SDK,而不是 Intent 广播器/接收器。如果您打算使用条形码扫描仪,那么官方(新)的方法是通过 Intent 。要获取 Intent ,您必须在数据楔中配置配置文件,必须激活 Intent 广播并在配置文件中指定 Intent 操作,如果这样做,您将收到 Intent 。在数据楔形配置文件中查找以下设置(默认配置文件很好):
Intent Output = ON
Intent action = my.prototype.app.SCAN
Intent distribution (or delivery): Broadcast
我可以向您保证,这些设置适用于条形码扫描仪,但如果您想使用 RFID 天线,请从 Zebra 开发人员网站下载 API3 SDK 并按照示例进行操作。
***更新
val filter = IntentFilter()
filter.addCategory(Intent.CATEGORY_DEFAULT)
filter.addAction("my.prototype.app.SCAN")//here set the action (same as in DataWedge app)
this.requireContext().registerReceiver(this, filter)
实现
BroadcastReceiver
并添加:
override fun onReceive(context: Context?, intent: Intent?) {
//Receives readings from barcode scanner
val action = intent!!.action
if (action == "my.prototype.app.SCAN") {
val decodedData = intent.getStringExtra("com.symbol.datawedge.data_string")
if (decodedData != null) {
//decodedData is your barcode
}
}
}
关于android - 使用 Zebra Data wedge API 接收 Intent 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66673752/