Skip to content
benoistlaforge edited this page May 12, 2017 · 1 revision

Barcode reader

Every Coppernic devices (C-One, C-five, C-eight, C-izi...) have barcode reader as an option. You can control every barcode reader the same way for every device.

The basics

Every communication with barcode reader can be handled using intents.

Trig a barcode scan

Intent scanIntent = new Intent();
scanIntent.setPackage(CpcOs.getSystemServicePackage(context));
scanIntent.setAction("fr.coppernic.intent.action.SCAN");
scanIntent.putExtra("package", context.getPackageName());
ComponentName info = context.startService(scanIntent);

Abort a barcode scan

Intent scanIntent = new Intent();
scanIntent.setPackage(CpcOs.getSystemServicePackage(context));
scanIntent.setAction("fr.coppernic.intent.action.scan.STOP");
scanIntent.putExtra("package", context.getPackageName());
ComponentName info = context.startService(scanIntent);

Get scan result

Scan result can be retrieved using 2 intents:

  • fr.coppernic.intent.scansuccess
  • fr.coppernic.intent.scanfailed

Register to intents

Scan success intent

First create a ScanSuccess BroadcastReceiver, it will handle data read:

private class ScanSuccess extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
             // Data read as String
            String data = extras.getString("BarcodeData");
            // Data read as byte array
            byte[] dataBytes = extras.getByteArray( "BarcodeDataBytes");
        }
    }
}

Then create a method to register to this intent:

private void registerBarcodeSuccess(){
    IntentFilter filter = new IntentFilter("fr.coppernic.intent.scansuccess");
    ScanSuccess receiverBarcode = new ScanSuccess();
    registerReceiver(receiverBarcode, filter);
}

Scan failed intent

First create ScanFailed BroadcastReceiver:

private class ScanFailed extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        // Handle failure here
    }
}

Then create method to register to this intent:

private void registerBarcodeFailed(){
    IntentFilter filter = new IntentFilter("fr.coppernic.intent.scanfailed");
    ScanFailed receiverBarcodeFailed = new ScanFailed();
    registerReceiver(receiverBarcodeFailed, filter);
}

Register intents

You can then register calling the 2 methods created, in the onStart callback of your Activity / Fragment for example:

@Override
protected void onStart() {
    registerBarcodeSuccess();
    registerBarcodeFailed();
}

And don't forget to unregister in the onStop:

@Override
protected void onStop() {
    
    super.onStop();
}