Basic scanner app's onData function doesn't called (Kotlin)

Tags: 

Hello,

I made a basic scanner application for the TC200J Zebra in Kotlin. I followed the tutorial step by step but the onData function does not triggerd by the hardware scan button on the right and left side of the phone. I downloaded the emdk plugin for android studio too. I am sure that i do it something in wrong way but i don't know what. It scans the ean13 code and write into a input field but i cannot catch the event in the onData. My CvLoggerClass logs only when the app in debug mode (i'm trying it in debug mode). I could use some help. If everything is true i don't need to install anything on the device, because the gradle dependency do it for me above 7.0.0.

My AndroidManifest file:
<manifest>
<uses-permission android:name="com.symbol.emdk.permission.EMDK" />
<application>
<uses-library android:name="com.symbol.emdk" />
</application>
</manifest>

My gradle file:
minSdk: 21
targetSdk: 28
dependencies {
compileOnly 'com.symbol:emdk:7.0.0'
}

My activity (the initializeEmdkManager called in onCreate(savedInstanceState: Bundle?)):

// Declare a variable to store EMDKManager object
private var emdkManager: EMDKManager? = null

// Declare a variable to store Barcode Manager object
private var barcodeManager: BarcodeManager? = null

// Declare a variable to hold scanner device to scan
private var scanner: Scanner? = null

// boolean flag to start scanning after scanner initialization
// Used in OnStatus callback to ensure scanner is idle before read() method is called
private var startRead = false

// Method to initialize and enable Scanner and its listeners
@Throws(ScannerException::class)
private fun initializeScanner() {
if (scanner == null) {
// Get the Barcode Manager object
barcodeManager = this.emdkManager!!.getInstance(FEATURE_TYPE.BARCODE) as BarcodeManager
// Get default scanner defined on the device
scanner = barcodeManager!!.getDevice(BarcodeManager.DeviceIdentifier.DEFAULT)
// Add data and status listeners
scanner!!.addDataListener(this)
scanner!!.addStatusListener(this)
// Hard trigger. When this mode is set, the user has to manually
// press the trigger on the device after issuing the read call.
scanner!!.triggerType = Scanner.TriggerType.HARD
// Enable the scanner
scanner!!.enable()
//set startRead flag to true. this flag will be used in the OnStatus callback to insure
//the scanner is at an IDLE state and a read is not pending before calling scanner.read()
startRead = true
}
}

@Throws(ScannerException::class)
private fun deInitializeScanner() {
if (scanner != null) {
try {
if (scanner!!.isReadPending) {
scanner!!.cancelRead()
}
scanner!!.disable()
} catch (e: Exception) {
e.printStackTrace()
}

try {
scanner!!.removeDataListener(this)
scanner!!.removeStatusListener(this)
} catch (e: Exception) {
e.printStackTrace()
}

try {
scanner!!.release()
} catch (e: Exception) {
e.printStackTrace()
}

scanner = null
}
}

private fun initializeEmdkManager() {
val results = EMDKManager.getEMDKManager(applicationContext, this)
// Check the return status of getEMDKManager and update the status Text
// View accordingly
if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS) CvLogger.log("Scan: EMDKManager Request Failed")
}

// AsyncTask that configures the scanned data on background
// thread and updated the result on UI thread with scanned data and type of
// label
private class AsyncDataUpdate(val scanner: Scanner) : AsyncTask<ScanDataCollection, Void, String>() {

override fun doInBackground(vararg params: ScanDataCollection?): String {
// Status string that contains both barcode data and type of barcode
// that is being scanned
var statusStr = ""

try {
// Starts an asynchronous Scan. The method will NOT turn ON the
// scanner, but puts it in a state in which the scanner can be turned
// on automatically or by pressing a hardware trigger

scanner.read()
val scanDataCollection = params[0]

// The ScanDataCollection object gives scanning result and the
// collection of ScanData. So check the data and its status

if (scanDataCollection != null && scanDataCollection.result == ScannerResults.SUCCESS) {
val scanData = scanDataCollection.scanData

// Iterate through scanned data and prepare the statusStr
scanData.forEach {
// Get the scanned data
val barcodeData = it.data
// Get the type of label being scanned
val labelType = it.labelType
// Concatenate barcode data and label type
statusStr = "$barcodeData $labelType"
}
}

} catch (e: ScannerException) {
e.printStackTrace()
}

// Return result to populate on UI thread
return statusStr
}

override fun onPostExecute(result: String?) {
CvLogger.log("Scan data: $result + \n")
}

override fun onPreExecute() {
}

override fun onProgressUpdate(vararg values: Void?) {
}
}

// AsyncTask that configures the current state of scanner on background
// thread and updates the result on UI thread
@SuppressLint("StaticFieldLeak")
private inner class AsyncStatusUpdate : AsyncTask<StatusData, Void, String>() {

override fun doInBackground(vararg params: StatusData): String {
var statusStr = ""
// Get the current state of scanner in background
val statusData = params[0]
// Different states of Scanner
when (statusData.state) {
// Scanner is IDLE
StatusData.ScannerStates.IDLE -> statusStr = "The scanner enabled and its idle"
// Scanner is SCANNING
StatusData.ScannerStates.SCANNING -> statusStr = "Scanning.."
// Scanner is waiting for trigger press
StatusData.ScannerStates.WAITING -> statusStr = "Waiting for trigger press.."
// Scanner is not enabled
StatusData.ScannerStates.DISABLED -> statusStr = "Scanner is not enabled"
else -> {
}
}

// Return result to populate on UI thread
return statusStr
}

override fun onPostExecute(result: String) {
// Update the status text view on UI thread with current scanner
// state
CvLogger.log("Scan status: $result")
}

override fun onPreExecute() {}

override fun onProgressUpdate(vararg values: Void) {}
}

override fun onDestroy() {
super.onDestroy()
if (emdkManager != null) {
// Clean up the objects created by EMDK manager
emdkManager!!.release()
emdkManager = null
}
}

override fun onStop() {
super.onStop()
try {
if (scanner != null) {
// Releases the scanner hardware resources for other application
// to use. Must be called as soon as scanning is done.
scanner!!.removeDataListener(this)
scanner!!.removeStatusListener(this)
scanner!!.disable()
scanner = null
}
} catch (e: ScannerException) {
e.printStackTrace()
}
}

//some lines of code omitted for clarity
override fun onClosed() {
if (this.emdkManager != null) {
this.emdkManager!!.release()
this.emdkManager = null
}
}

override fun onOpened(emdkManager: EMDKManager) {
this.emdkManager = emdkManager
try {
// Call this method to enable Scanner and its listeners
initializeScanner()
} catch (e: ScannerException) {
e.printStackTrace()
}
}

override fun onData(scanDataCollection: ScanDataCollection) {
// Use the scanned data, process it on background thread using AsyncTask
// and update the UI thread with the scanned results
AsyncDataUpdate(scanner!!).execute(scanDataCollection)
}

override fun onStatus(statusData: StatusData) {
AsyncStatusUpdate().execute(statusData)
}

Darryn Campbell
Hi, I tried this a little

Hi, I tried this a little while back with EMDK 6.9 and it seemed to work OK, the sample app is at https://github.com/darryncampbell/samples-emdkforandroid-6_9/tree/BarcodeSample1-Kotlin/BarcodeSample1 and the original forum post is at https://developer.zebra.com/community/home/blog/2018/11/19/kotlin-and-developing-kotlin-applications-for-zebra-devices.

Can you try the sample app I listed above to see if that works?

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments