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

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.

// Example in Kotlin

@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

@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()
    }
}