Ahmad Rehan
Ahmad Rehan

Reputation: 77

Cognex Barcode SDK not scanning codes

I have implemented Cognex cmbSDK completely in my app. And i'm using trial version. Key and everything is implemented correctly, When i click on button it opens camera and also displays the features but it is not scanning any type of code. Can you please help me why it is not working?

Here is my code:

open class MainActivity : AppCompatActivity(),
ReaderDevice.OnConnectionCompletedListener, ReaderDevice.ReaderDeviceListener,ActivityCompat.OnRequestPermissionsResultCallback{

private val permissionCode = 123
private val sdkKey: String = "My KEY"
private lateinit var readerDevice: ReaderDevice

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    readerDevice = ReaderDevice.getPhoneCameraDevice(this, CameraMode.NO_AIMER,
        PreviewOption.NO_ZOOM_BUTTON, null, sdkKey)
    readerDevice.setReaderDeviceListener(this)
    readerDevice.enableImage(true)
    readerDevice.enableImageGraphics(false)
    readerDevice.connect(this)
    enableSymbologies()

    btnScanBarcode.setOnClickListener{
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), permissionCode)
        }
        readerDevice.startScanning()
    }
}

override fun onConnectionCompleted(readerDevice: ReaderDevice?, error: Throwable?) {
    Toast.makeText(this, "Connection Created", Toast.LENGTH_LONG).show()
    if (error != null) {
        // ask for Camera Permission if necessary
        if (error is CameraPermissionException) ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.CAMERA),
            permissionCode
        )
    }
}

override fun onAvailabilityChanged(readerDevice: ReaderDevice?) {
}

override fun onConnectionStateChanged(readerDevice: ReaderDevice?) {

}

override fun onReadResultReceived(readerDevice: ReaderDevice, results: ReadResults) {
    readerDevice.stopScanning()
    Toast.makeText(this, "Result Received. ${results.getResultAt(0).symbology}", Toast.LENGTH_LONG).show()
    Log.d("barcode" ,"MY BARCODE RESULT: ")

    if (results.subResults != null && results.subResults.size > 0) {
        for (subResult in results.subResults) {
            createResultItem(subResult)
        }
    } else if (results.count > 0) {
        createResultItem(results.getResultAt(0))
    }
}

private fun createResultItem(result: ReadResult) {
    val item =
        HashMap<String, String>()
    if (result.isGoodRead) {
        item["resultText"] = result.readString
        val sym = result.symbology
        if (sym != null) item["resultType"] =
            result.symbology.getName() else item["resultType"] =
            "UNKNOWN SYMBOLOGY"
    } else {
        item["resultText"] = "NO READ"
        item["resultType"] = ""
    }
    Toast.makeText(this, item["resultText"], Toast.LENGTH_SHORT).show()
}

override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    if(requestCode == permissionCode){
        readerDevice.startScanning()
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

private fun enableSymbologies(){
    readerDevice.setSymbologyEnabled(Symbology.DATAMATRIX, true, object : OnSymbologyListener {
        override fun onSymbologyEnabled(
            reader: ReaderDevice,
            symbology: Symbology,
            enabled: Boolean,
            error: Throwable
        ) {
            Log.e(
                this.javaClass.simpleName,
                "Failed to enable Symbology.DATAMATRIX"
            )
        }
    })
    readerDevice.setSymbologyEnabled(Symbology.C128, true, object : OnSymbologyListener {
        override fun onSymbologyEnabled(
            reader: ReaderDevice,
            symbology: Symbology,
            enabled: Boolean,
            error: Throwable
        ) {
            Log.e(
                this.javaClass.simpleName,
                "Failed to enable Symbology.C128"
            )
        }
    })
    readerDevice.setSymbologyEnabled(Symbology.UPC_EAN, true, object : OnSymbologyListener {
        override fun onSymbologyEnabled(
            reader: ReaderDevice,
            symbology: Symbology,
            enabled: Boolean,
            error: Throwable
        ) {
            Log.e(
                this.javaClass.simpleName,
                "Failed to enable Symbology.UPC_EAN"
            )
        }
    })
    readerDevice.setSymbologyEnabled(Symbology.CODABAR, false, object : OnSymbologyListener {
        override fun onSymbologyEnabled(
            reader: ReaderDevice,
            symbology: Symbology,
            enabled: Boolean,
            error: Throwable
        ) {
            Log.e(
                this.javaClass.simpleName,
                "Failed to disable Symbology.CODABAR"
            )
        }
    })
    readerDevice.setSymbologyEnabled(Symbology.C93, false, object : OnSymbologyListener {
        override fun onSymbologyEnabled(
            reader: ReaderDevice,
            symbology: Symbology,
            enabled: Boolean,
            error: Throwable
        ) {
            Log.e(
                this.javaClass.simpleName,
                "Failed to disable Symbology.C93"
            )
        }
    })
    readerDevice.dataManSystem
        .sendCommand(
            "GET DEVICE.TYPE"
        ) { _, response ->
            if (response.error == null) {
                Log.d("Device type", response.payLoad)
            }
        }
    readerDevice.dataManSystem
        .sendCommand(
            "GET DEVICE.FIRMWARE-VER"
        ) { _, response ->
            if (response.error == null) {
                Log.d("Firmware version", response.payLoad)
            }
        }
}}

Manifest File Code:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:hardwareAccelerated="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="MX_MOBILE_LICENSE"
        android:value="HERE I PUT MY KEY"/>
</application>

Upvotes: 1

Views: 745

Answers (2)

Nazgul
Nazgul

Reputation: 86

Also if you see in our SDK we put this in configureReaderDevice ( here you put your symbology ) and then you call it from onConnectionStateChanged

But please open Support Ticket on this link and our Technical Support will resolved your issue - https://cmbdn.cognex.com/tickets

Enable the symbology have to be added after you connect to the readerDevice

Regards,

Upvotes: 2

Nazgul
Nazgul

Reputation: 86

  1. Can you please be more specific about the code that you like to scan because from the code I can see that you selected only UPC_EAN, DATAMATRIX, and C128 ( also see if this symbology is enabled in your license key )
  2. All symbologies used for the symbology parameter in this method can be found in ReaderDevice.java.

Examples readerDevice.setSymbologyEnabled(Symbology.QR, true, null);

  1. Here is our Knowledge Base for more information - https://cmbdn.cognex.com/v2.3.x/knowledge/cognex-mobile-barcode-sdk-for-android/using-cmbsdk

  2. You can always open a ticket on the Cognex webpage.

Best Regards, Cognex Mobile Solutions Team

Upvotes: 1

Related Questions