how to know when the RFID gun button trigger is pressed on the RFD40 + TC21

95 views Asked by At

How can I know when the gun trigger is pressed programmatically using Zebra DataWedge 11.3.28 using an intent broadcaster on a RFD40 + TC21?

I am using one of the examples from Zebra

object DataWedgeUtility {
    private const val PROFILE_NAME = "DW_my_app"
    private const val ACTION_DATAWEDGE = "com.symbol.datawedge.api.ACTION"
    private const val EXTRA_CREATE_PROFILE = "com.symbol.datawedge.api.CREATE_PROFILE"
    private const val EXTRA_SET_CONFIG = "com.symbol.datawedge.api.SET_CONFIG"

    fun createDWProfile(context: Context) {
        sendDataWedgeIntentWithExtra(
            context,
            ACTION_DATAWEDGE,
            EXTRA_CREATE_PROFILE,
            PROFILE_NAME
        )

        val profileConfig = Bundle()
        profileConfig.putString("PROFILE_NAME", PROFILE_NAME)
        profileConfig.putString("PROFILE_ENABLED", "true") //  Seems these are all strings
        profileConfig.putString("CONFIG_MODE", "UPDATE")
        val rfidConfig = Bundle()
        rfidConfig.putString("PLUGIN_NAME", "RFID")
        rfidConfig.putString("RESET_CONFIG", "true")

        val rfidProps = Bundle()
        /* values true o false as string */
        rfidProps.putString("rfid_input_enabled", "true")
        /* values true o false as string */
        rfidProps.putString("rfid_beeper_enable", "true")
        /* Integer from 5 to 30 */
        rfidProps.putString("rfid_antenna_transmit_power", "25")

        rfidProps.putString("rfid_memory_bank", "0")

        rfidProps.putString("rfid_session", "1")
        rfidProps.putString("rfid_filter_duplicate_tags", "true")
        rfidProps.putString("rfid_hardware_trigger_enabled", "true")

        rfidProps.putString("rfid_trigger_mode", "1")
        rfidProps.putString("rfid_tag_read_duration", "100")

        rfidConfig.putBundle("PARAM_LIST", rfidProps)
        profileConfig.putBundle("PLUGIN_CONFIG", rfidConfig)
        val appConfig = Bundle()
        appConfig.putString(
            "PACKAGE_NAME",
            context.packageName
        ) //  Associate the profile with this app
        appConfig.putStringArray("ACTIVITY_LIST", arrayOf("*"))
        profileConfig.putParcelableArray("APP_LIST", arrayOf(appConfig))
        sendDataWedgeIntentWithExtra(context, ACTION_DATAWEDGE, EXTRA_SET_CONFIG, profileConfig)

        //  You can only configure one plugin at a time, we have done the RFID input, 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",
            context.resources.getString(R.string.activity_intent_filter_action)
        )

        intentProps.putString("intent_delivery", "0")
        intentConfig.putBundle("PARAM_LIST", intentProps)
        profileConfig.putBundle("PLUGIN_CONFIG", intentConfig)
        sendDataWedgeIntentWithExtra(context, ACTION_DATAWEDGE, EXTRA_SET_CONFIG, profileConfig)

        //  Disable keyboard output
        profileConfig.remove("PLUGIN_CONFIG")
        val keystrokeConfig = Bundle()
        keystrokeConfig.putString("PLUGIN_NAME", "KEYSTROKE")
        keystrokeConfig.putString("RESET_CONFIG", "true")
        val keystrokeProps = Bundle()
        keystrokeProps.putString("keystroke_output_enabled", "false")
        keystrokeConfig.putBundle("PARAM_LIST", keystrokeProps)
        profileConfig.putBundle("PLUGIN_CONFIG", keystrokeConfig)
        sendDataWedgeIntentWithExtra(context, ACTION_DATAWEDGE, EXTRA_SET_CONFIG, profileConfig)
    }

    private fun sendDataWedgeIntentWithExtra(
        context: Context,
        action: String,
        extraKey: String,
        extraValue: String
    ) {
        val dwIntent = Intent()
        dwIntent.action = action
        dwIntent.putExtra(extraKey, extraValue)
        context.sendBroadcast(dwIntent)
    }

    private fun sendDataWedgeIntentWithExtra(
        context: Context,
        action: String,
        extraKey: String,
        extras: Bundle
    ) {
        val dwIntent = Intent()
        dwIntent.action = action
        dwIntent.putExtra(extraKey, extras)
        context.sendBroadcast(dwIntent)
    }
}

And on the Activity, I have the following code

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DataWedgeUtility.createDWProfile(this)
    ...
}

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        val action = intent.action

        if (action == resources.getString(R.string.activity_intent_filter_action)) {
            //  Received a RFID scan
            try {
                val epc = intent.getStringExtra(
                    resources.getString(R.string.datawedge_intent_key_data)
                )
                if (epc?.isNotBlank() == true){
                    val tagList = epc.split("\n")
                    tagList.forEach {
                        EventBus.getDefault().post(TagReadEvent(epc = it))
                    }
                }
            } catch (e: Exception) {
                //  Catch if the UI does not exist when we receive the broadcast
            }
        }
    }

fun softRfidTrigger(): Boolean{
        val dwIntent = Intent()
        dwIntent.action = "com.symbol.datawedge.api.ACTION"
        //  Button released, end scan
        when  {
            !readingFlag -> {
                readingFlag = true
                dwIntent.putExtra(
                    "com.symbol.datawedge.api.SOFT_RFID_TRIGGER",
                    "START_SCANNING"
                )
            }
            else -> {
                readingFlag = false
                dwIntent.putExtra(
                    "com.symbol.datawedge.api.SOFT_RFID_TRIGGER",
                    "STOP_SCANNING"
                )
            }
        }
        sendBroadcast(dwIntent)
        return readingFlag
    }
..
}

I am using a single Activity approach on the app, and all communication between the Activity and Fragments is using EventBus events.

0

There are 0 answers