我们目前正在使用斑马设备进行公司资产管理,因此我们正在开发一个小型原型 Android 应用程序来扫描 RFID 标签。我从数据楔形 API 中读到,该应用程序可以获得扫描输出具有意图广播。
但是该应用程序无法接收任何意图。
设备:斑马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.")
}
}
}
}
}
}
还尝试使用“通过启动活动发送”
个人资料设置:
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>
扫描活动.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)