2

所以我有一个 Zebra MC330M 设备。

我之前创建了一个应用程序,我想使用 PDA 内置的条码扫描仪。

如果在调试模式下运行我的应用程序,一切正常,我可以读取条形码,但如果我创建分阶段或发布版本的 apk,条形码阅读器处于非活动状态,因此如果我按下按钮,红灯不会亮起。

我创建了一个简单的来演示问题,但如果您的设备具有 EMDK,您可以重现该问题,否则您会遇到异常。 项目

我通过本教程实现了所有东西:https ://techdocs.zebra.com/emdk-for-android/11-0/tutorial/tutBasicScanningAPI

所以我将它添加到 gradle 中:compileOnly 'com.symbol:emdk:7.6.+' 我在 manfest 中添加了这两行:

<uses-permission android:name="com.symbol.emdk.permission.EMDK" />
<uses-library android:name="com.symbol.emdk" android:required="false"/>

我尝试使用 implementation 而不是 compileOnly 添加依赖项,但之后我在这一行得到了一个异常:EMDKManager.getEMDKManager(context,object : EMDKManager.EMDKListener...

[更新] 当我按下扫描仪按钮时,我收到此错误:

E/ActivityManager: Sending non-protected broadcast com.symbol.button.R1 from system 1208:system/1000 pkg android
    java.lang.Throwable
        at com.android.server.am.ActivityManagerService.checkBroadcastFromSystem(ActivityManagerService.java:18150)
        at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18728)
        at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18819)
        at android.app.ContextImpl.sendBroadcastAsUser(ContextImpl.java:1040)
        at com.android.server.RemoteKeyEventService.broadcastPublicKeyIntent(RemoteKeyEventService.java:672)
        at com.android.server.RemoteKeyEventService.handleKeyEvent(RemoteKeyEventService.java:526)
        at com.android.server.wm.InputMonitor.interceptKeyBeforeQueueing(InputMonitor.java:464)
        at com.android.server.input.InputManagerService.interceptKeyBeforeQueueing(InputManagerService.java:1874)

我的应用程序的目标是 API 级别 30,我将权限放在清单中。


I also tried to log the error, but there is no error, it seems the two listeners (status, and data) not triggered, or can not enable the scanner, but in this case I should get an error message. 

Barcode Reader Class:
class ZebraBarcodeReader(
    private val context: Context
){
    private var emdkManager: EMDKManager? = null
    private var barcodeManager: BarcodeManager? = null
    private val barcodeLiveData = MutableLiveData<EventWrapper<String?>>()

    init {
        val result = EMDKManager.getEMDKManager(context,object : EMDKManager.EMDKListener{

            override fun onOpened(p0: EMDKManager?) {
                opened(p0)
            }

            override fun onClosed() {
                closed()
            }
        })

        if(result.statusCode != EMDKResults.STATUS_CODE.SUCCESS) {
            Timber.d("$TIMBER_TAG ZebraBarcodeReader failed to connect")
        }else{
            Timber.d("$TIMBER_TAG EMDKManager object initialization in progress...")
        }
    }

    fun getData(): LiveData<EventWrapper<String?>> = barcodeLiveData

    private fun opened(manager: EMDKManager?){
        emdkManager = manager

        initBarcodeManager()
        initScanner()
    }

    private var scanner: Scanner? = null

    private fun initScanner() {
        scanner = barcodeManager?.getDevice(BarcodeManager.DeviceIdentifier.DEFAULT)

        if(scanner == null){
            Timber.d("$TIMBER_TAG Scanner not found")
        }else{
            scanner?.addDataListener {scanDataCollection->
                if(scanDataCollection != null && (scanDataCollection.result) == ScannerResults.SUCCESS){
                    val scanData = scanDataCollection.scanData

                    scanData.forEach {
                        val barcode = it.data

                        barcodeLiveData.postValue(EventWrapper(barcode))
                    }
                }
            }

            scanner?.addStatusListener { statusData->
                val state = statusData.state
                var statusString = ""

                when(state){
                    StatusData.ScannerStates.IDLE-> {
                        statusString = statusData.friendlyName + "is enabled and IDLE"
                        setConfig()

                        try {
                            scanner?.read()
                        }catch (e: ScannerException){
                            Timber.d(e)
                        }
                    }

                    StatusData.ScannerStates.WAITING-> {
                        statusString = "Scanner is waiting for trigger press"
                    }

                    StatusData.ScannerStates.SCANNING-> {
                        statusString = "Scanning"
                    }

                    StatusData.ScannerStates.DISABLED-> {
                        statusString = "Scanner is disabled"
                    }

                    StatusData.ScannerStates.ERROR-> {
                        statusString="An error occured"
                    }

                    else -> {
                        statusString="Another thing in else branch"
                    }
                }

                Timber.d("$TIMBER_TAG $statusString")
            }

            try {
                scanner?.enable()
            } catch (e: ScannerException) {
                Timber.d("$TIMBER_TAG Scanner can not be enabled")
            }
        }
    }

    private fun setConfig() {
        scanner?.let { s->
            try {
                val config = s.config

                if(config.isParamSupported("config.scanParams.decodeHapticFeedback"))
                    config.scanParams.decodeHapticFeedback = true

                s.config = config
            }catch (e: Exception){
                Timber.d(e)
            }
        }
    }

    private fun initBarcodeManager() {
        barcodeManager = emdkManager?.getInstance(EMDKManager.FEATURE_TYPE.BARCODE) as BarcodeManager?

        if(barcodeManager == null){
            Timber.d("$TIMBER_TAG Barcode scanning not supported")
        }
    }

    private fun closed(){
        emdkManager?.let { m->
            m.release()
            emdkManager = null
        }

        Timber.d("$TIMBER_TAG : closed unexpectedly!")
    }

    fun destroy(){
        emdkManager?.let { m->
            m.release()
            emdkManager = null
        }
    }

4

2 回答 2

1

请将以下内容添加到您的清单中的 uses-permission 标签下:

<queries>
  <package android:name="com.symbol.emdk.emdkservice" />
</queries>
于 2022-01-31T07:47:44.057 回答
0

对我来说有点奇怪,但我解决了这个问题。

我不知道为什么,但首先我安装了我的 apk 的调试版本,然后我将其删除,然后我安装了暂存/发布版本。我没有工作。

之后当我删除调试版本时,我重新启动设备,然后安装发布版本,它可以工作。我不知道为什么这是必要的,但我很高兴。

于 2022-01-31T15:48:37.567 回答