1

我正在使用 Zebra TC25 设备,尝试以编程方式使用 DataWedge API 创建配置文件。配置文件已创建,我尝试扫描条形码但没有任何反应因此我检查了我的 DataWedge 应用程序(版本 7.0.4),我进入了我的配置文件并检查了没有关联的应用程序。我有一个可以正常工作的示例代码,在我的工作项目中应用的相同示例代码不起作用。如果有人可以提供帮助将节省大量时间。在这里发布我的代码,请看一下。

数据楔管理器.kt

class DatawedgeManager(val context: Context) {

private val dwInterface = DWInterface();



companion object {
    @Volatile
    private var instance: DatawedgeManager? = null
    const val PROFILE_NAME = "DataWedgePf"
    const val PROFILE_INTENT_ACTION = BuildConfig.APPLICATION_ID
    const val PROFILE_INTENT_START_ACTIVITY = "0"


    fun getInstance(context: Context): DatawedgeManager? {
        return instance ?: synchronized(DatawedgeManager::class.java) {
            if (instance == null) {
                instance = DatawedgeManager(context )
            }
            return instance
        }
    }

}




private fun createDataWedgeProfile() {
    //  Create and configure the DataWedge profile associated with this application
    //  For readability's sake, I have not defined each of the keys in the DWInterface file
    dwInterface.sendCommandString(context, DWInterface.DATAWEDGE_SEND_CREATE_PROFILE, PROFILE_NAME)
    val profileConfig = Bundle()
    profileConfig.putString("PROFILE_NAME", PROFILE_NAME)
    profileConfig.putString("PROFILE_ENABLED", "true") //  These are all strings
    profileConfig.putString("CONFIG_MODE", "UPDATE")
    val barcodeConfig = Bundle()
    barcodeConfig.putString("PLUGIN_NAME", "BARCODE")
    barcodeConfig.putString("RESET_CONFIG", "true") //  This is the default but never hurts to specify
    val barcodeProps = Bundle()
    barcodeConfig.putBundle("PARAM_LIST", barcodeProps)
    profileConfig.putBundle("PLUGIN_CONFIG", barcodeConfig)
    val appConfig = Bundle()
    appConfig.putString("PACKAGE_NAME", BuildConfig.APPLICATION_ID)      //  Associate the profile with this app
    appConfig.putStringArray("ACTIVITY_LIST", arrayOf("*"))
    profileConfig.putParcelableArray("APP_LIST", arrayOf(appConfig))
    dwInterface.sendCommandBundle(context, DWInterface.DATAWEDGE_SEND_SET_CONFIG, profileConfig)
    //  You can only configure one plugin at a time in some versions of DW, now do the intent output
    profileConfig.remove("PLUGIN_CONFIG")
    val intentConfig = Bundle()
    intentConfig.putString("PLUGIN_NAME", "INTENT")
    intentConfig.putString("RESET_CONFIG", "true")
    val intentProps = Bundle()
    intentProps.putString("intent_output_enabled", "true")
    intentProps.putString("intent_action", PROFILE_INTENT_ACTION)
    intentProps.putString("intent_delivery", PROFILE_INTENT_START_ACTIVITY)  //  "0"
    intentConfig.putBundle("PARAM_LIST", intentProps)
    profileConfig.putBundle("PLUGIN_CONFIG", intentConfig)
    dwInterface.sendCommandBundle(context, DWInterface.DATAWEDGE_SEND_SET_CONFIG, profileConfig)
    initScan()
}

fun initZebraLaserScan(){
    createDataWedgeProfile()
}

fun initScan(){

    dwInterface.sendCommandString(context, DWInterface.DATAWEDGE_SEND_SET_SCANNER_INPUT, DWInterface.DATAWEDGE_SEND_SET_SCANNER_INPUT_ENABLE)

}

fun deinitScan(){

    dwInterface.sendCommandString(context, DWInterface.DATAWEDGE_SEND_SET_SCANNER_INPUT,
        DWInterface.DATAWEDGE_SEND_SET_SCANNER_INPUT_DISABLE)

}
}

DW接口.kt

class DWInterface()
{
    companion object {

    const val DATAWEDGE_SEND_ACTION = "com.symbol.datawedge.api.ACTION"
    const val DATAWEDGE_RETURN_ACTION = "com.symbol.datawedge.api.RESULT_ACTION"
    const val DATAWEDGE_RETURN_CATEGORY = "android.intent.category.DEFAULT"
    const val DATAWEDGE_EXTRA_SEND_RESULT = "SEND_RESULT"
    const val DATAWEDGE_EXTRA_RESULT = "RESULT"
    const val DATAWEDGE_EXTRA_COMMAND = "COMMAND"
    const val DATAWEDGE_EXTRA_RESULT_INFO = "RESULT_INFO"
    const val DATAWEDGE_EXTRA_RESULT_CODE = "RESULT_CODE"

    const val DATAWEDGE_SCAN_EXTRA_DATA_STRING = "com.symbol.datawedge.data_string"
    const val DATAWEDGE_SCAN_EXTRA_LABEL_TYPE = "com.symbol.datawedge.label_type"

    const val DATAWEDGE_SEND_CREATE_PROFILE = "com.symbol.datawedge.api.CREATE_PROFILE"

    const val DATAWEDGE_SEND_GET_VERSION = "com.symbol.datawedge.api.GET_VERSION_INFO"
    const val DATAWEDGE_RETURN_VERSION = "com.symbol.datawedge.api.RESULT_GET_VERSION_INFO"
    const val DATAWEDGE_RETURN_VERSION_DATAWEDGE = "DATAWEDGE"

    const val DATAWEDGE_SEND_GET_ENUMERATE_SCANNERS = "com.symbol.datawedge.api.ENUMERATE_SCANNERS"
    const val DATAWEDGE_RETURN_ENUMERATE_SCANNERS = "com.symbol.datawedge.api.RESULT_ENUMERATE_SCANNERS"

    const val DATAWEDGE_SEND_GET_CONFIG = "com.symbol.datawedge.api.GET_CONFIG"
    const val DATAWEDGE_RETURN_GET_CONFIG = "com.symbol.datawedge.api.RESULT_GET_CONFIG"
    const val DATAWEDGE_SEND_SET_CONFIG = "com.symbol.datawedge.api.SET_CONFIG"

    const val DATAWEDGE_SEND_GET_ACTIVE_PROFILE = "com.symbol.datawedge.api.GET_ACTIVE_PROFILE"
    const val DATAWEDGE_RETURN_GET_ACTIVE_PROFILE = "com.symbol.datawedge.api.RESULT_GET_ACTIVE_PROFILE"

    const val DATAWEDGE_SEND_SWITCH_SCANNER = "com.symbol.datawedge.api.SWITCH_SCANNER"

    const val DATAWEDGE_SEND_SET_SCANNER_INPUT = "com.symbol.datawedge.api.SCANNER_INPUT_PLUGIN"
    const val DATAWEDGE_SEND_SET_SCANNER_INPUT_ENABLE = "ENABLE_PLUGIN"
    const val DATAWEDGE_SEND_SET_SCANNER_INPUT_DISABLE = "DISABLE_PLUGIN"

    const val DATAWEDGE_SEND_SET_SOFT_SCAN = "com.symbol.datawedge.api.SOFT_SCAN_TRIGGER"
}

fun sendCommandString(context: Context, command: String, parameter: String, sendResult: Boolean = false)
{
    val dwIntent = Intent()
    dwIntent.action = DATAWEDGE_SEND_ACTION
    dwIntent.putExtra(command, parameter)
    if (sendResult)
        dwIntent.putExtra(DATAWEDGE_EXTRA_SEND_RESULT, "true")
    context.sendBroadcast(dwIntent)
}

fun sendCommandBundle(context: Context, command: String, parameter: Bundle)
{
    val dwIntent = Intent()
    dwIntent.action = DATAWEDGE_SEND_ACTION
    dwIntent.putExtra(command, parameter)
    context.sendBroadcast(dwIntent)
}

fun setConfigForDecoder(context: Context, profileName: String, ean8Value: Boolean,
                        ean13Value: Boolean, code39Value: Boolean, code128Value: Boolean,
                        illuminationValue: String, picklistModeValue: String) {
    val profileConfig = Bundle()
    profileConfig.putString("PROFILE_NAME", profileName)
    profileConfig.putString("PROFILE_ENABLED", "true")
    profileConfig.putString("CONFIG_MODE", "UPDATE")
    val barcodeConfig = Bundle()
    barcodeConfig.putString("PLUGIN_NAME", "BARCODE")
    barcodeConfig.putString("RESET_CONFIG", "true")
    val barcodeProps = Bundle()
    barcodeProps.putString("scanner_selection", "auto")
    barcodeProps.putString("decoder_ean8", "" + ean8Value)
    barcodeProps.putString("decoder_ean13", "" + ean13Value)
    barcodeProps.putString("decoder_code39", "" + code39Value)
    barcodeProps.putString("decoder_code128", "" + code128Value)
    barcodeProps.putString("illumination_mode", illuminationValue)
    barcodeProps.putString("picklist", picklistModeValue)
    barcodeConfig.putBundle("PARAM_LIST", barcodeProps)
    profileConfig.putBundle("PLUGIN_CONFIG", barcodeConfig)
    sendCommandBundle(context, DATAWEDGE_SEND_SET_CONFIG, profileConfig)
}
}

ViewpagerActivity.kt

class ViewPagerActivity : AppCompatActivity(), onPageChangeListener, SubscribeScanner {

private lateinit var mainViewModel: MainViewModel
private var listOfScreenObj = arrayListOf<String>()
lateinit var barcodeScannedEvent: BarcodeScannedEvent


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.view_pager_layout)
    setSupportActionBar(toolbar)

    val dataViewModelFactory = DataViewModelFactory(this)
    mainViewModel = ViewModelProviders.of(this, dataViewModelFactory).get(MainViewModel::class.java)
    listOfScreenObj = mainViewModel.getScreenIds()
    setViewPager()

}


private fun setViewPager() {
    viewPager2.adapter = ViewPagerAdapter(this,listOfScreenObj)
    viewPager2.registerOnPageChangeCallback(pageChangeCallback)
}

private var pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
    override fun onPageSelected(position: Int) {
       Toast.makeText(this@ViewPagerActivity , "Selected position: $position", Toast.LENGTH_SHORT).show()
    }
}

override fun onPageChange(position: Int) {
    viewPager2.currentItem = position
}


override fun subscribeListener(barcodeScannedEvent: BarcodeScannedEvent) {
    this.barcodeScannedEvent = barcodeScannedEvent
}

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    if (intent.hasExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_DATA_STRING)) {
        val scanData = intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_DATA_STRING)
        val symbology = intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_LABEL_TYPE)
        val date = Calendar.getInstance().time
        val df = SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
        val dateTimeString = df.format(date)
        barcodeScannedEvent.onBarcodeScanEvent(scanData,symbology,dateTimeString)

    }
}
}

在我的 ManiFest 文件中;

  <activity
        android:name=".view.activities.ViewPagerActivity"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden">

        <intent-filter>
            <action android:name="app.example.dataWedge" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

由于我的个人资料中没有关联的应用程序,因此如果我扫描任何内容都不会影响。所以我尝试手动添加我的包,然后调用 onNewIntent。这段代码 DataWedge 功能来自这个 github ref:: DataWedgeKotlin。附上屏幕截图,我正在使用 Zebra TC25 设备。

在此处输入图像描述

4

1 回答 1

0

这看起来像我的样本?:)

这一行:

appConfig.putString("PACKAGE_NAME", BuildConfig.APPLICATION_ID)

应该:

appConfig.putString("PACKAGE_NAME", context.packageName)

于 2020-07-23T13:59:03.237 回答