Controlling the Scanner

Trigger the scanner

In certain scenarios it can be easier to trigger a scan using an on-screen button than using the trigger button the device. Call DeviceClient.trigger() on a connected device to trigger a scan.

// Example in Kotlin and using Anko DSL for brevity

button("Scan") {
    onClick { connectedDevice?.trigger() }
}

Enable and disable barcode types

Enabling and disabling data sources allows your application to control which barcode types the scanner is able to read. This can be useful to enable a data source that isn’t enabled by default or to prevent the user from scanning an unused barcode.

Enable or disable the relevant data source when the device first becomes available.

Warning

Unlike camera based scanning solutions, disabling data sources does not have a significant impact on read times. Do not disable data sources simply for performance reasons.

// Example in Kotlin for brevity

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
fun onCaptureDeviceStateChange(DeviceStateEvent event) : Unit {
    when(event.state.intValue()) {
        DeviceState.READY -> {
            event.device.enableDataSource(DataSource.ID_QR_CODE) {
                // OK or Error
            }
            event.device.disableDataSource(DataSource.ID_UPC_A) {
                // OK or Error
            }
        }
    }
}

Accept or reject decoded data

Using the data confirmation apis, your app can decide whether the scanner should accept or reject a particular scan. This can be used to simply validate the data in-app or to enforce a particular workflow.

Note

Until the data is acknowledged, the scanner’s trigger is locked and the user is unable to scan additional barcodes.

While it is possible to send feedback to the device without any additional configuration, the user may receive duplicate and potentially conflicting feedback unless you disable local acknowledgement first.

// Example in Kotlin for brevity

@Subscribe(threadMode = MAIN)
fun onDeviceReady(event: DeviceStateEvent) {
    when (event.state.intValue()) {
        DeviceState.READY -> {
            event.device.disableLocalAcknowledgment() {
                // OK or Error
            }
        }
    }
}

@Subscribe
fun onData(event: DataEvent) {
    scanCounter++
    when (scanCounter % 2) {
        0 -> event.device.acceptData()
        else -> event.device.rejectData()
    }
}