如何在发布版本中使用 Zebra EMDK?

How to use Zebra EMDK in release build?

所以我有一台 Zebra MC330M 设备。

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

如果 运行 我的应用程序处于调试模式,一切正常,我可以读取条形码,但是如果我创建一个暂存或发布版本的 apk,条形码 reader 不活动,所以按下按钮红灯不亮

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

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

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

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

我试图通过实现而不是 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
        }
    }

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

<queries>
  <package android:name="com.symbol.emdk.emdkservice" />
</queries>

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

我不知道为什么,但我首先安装了我的 apk 的调试版本,之后我删除了它,然后我安装了 staged/release 版本。我没有工作。

之后删除debug版本后,重启设备,然后安装release版本,就可以了。我不知道为什么有必要,但我很高兴。