Sample Code
USB Scanner
Detect the Connected Devices List
var usbDeviceManager: DatalogicDeviceManager
usbDeviceManager.detectDevice(context) { devices ->
_deviceList.postValue(ArrayList(devices))
}
Open the connected device and setup the scan listener to get the barcode scanning data
// Open the device
val result = device.openDevice(context)
when (result) {
USBConstants.SUCCESS -> {
//Setup listener
scanEvent = object : UsbScanListener {
//The onScan() function will be called when scanner scan the barcode
override fun onScan(scanData: UsbScanData) {
setScannedData(scanData)
}
}
}
}
Bluetooth Scanner
Steps to pair the Bluetooth Scanner with SPP profile
var usbDeviceManager: DatalogicDeviceManager
// Create the barcode image to pair scanner
val bitmap = usbDeviceManager.qrCodeGenerator(context, BluetoothProfile.SPP)
// Start to pair the Bluetooth scanner
usbDeviceManager.stopScanBluetoothDevices(context)
usbDeviceManager.scanBluetoothDevices(context) { pairingData ->
val status = pairingData.pairingStatus
val message = pairingData.message
val name = pairingData.deviceName
when (status) {
BluetoothPairingStatus.Successful -> {
if (message.contains("connected")) {
setPairingStatus(PairingStatus.Connected)
} else {
setPairingStatus(PairingStatus.Paired)
}
}
BluetoothPairingStatus.Unsuccessful -> {
if (message == "Permission denied") {
setPairingStatus(PairingStatus.PermissionDenied)
} else {
setPairingStatus(PairingStatus.Error)
}
}
BluetoothPairingStatus.Timeout -> {
setPairingStatus(PairingStatus.Timeout)
}
}
}
// We need to scan the bitmap image with Bluetooth scanner here…
// Remove Bluetooth device scanning setup
usbDeviceManager.stopScanBluetoothDevices(context)
Steps to connect the Bluetooth Scanner to receive scanning data
// Create the scanning listener
bluetoothScanEvent = object : UsbScanListener {
override fun onScan(scanData: UsbScanData) {
setScannedData(scanData)
}
}
// Connect the Bluetooth scanner
CoroutineScope(Dispatchers.IO).launch {
device.connectDevice(bluetoothScanEvent, context) { status ->
if (status == BluetoothPairingStatus.Successful) {
_status.postValue(DeviceStatus.OPENED)
_deviceStatus.postValue("Device opened")
} else {
_status.postValue(DeviceStatus.CLOSED)
_deviceStatus.postValue("No device selected")
}
}
}
// When the scanner scan barcode, the scanning data should be returned on bluetoothScanEvent
//…
// Disconnect the Bluetooth scanner
device .clearConnection(context)