-
Notifications
You must be signed in to change notification settings - Fork 0
Home
benoistlaforge edited this page May 12, 2017
·
1 revision
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.
Every communication with barcode reader can be handled using intents.
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);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);Scan result can be retrieved using 2 intents:
- fr.coppernic.intent.scansuccess
- fr.coppernic.intent.scanfailed
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);
}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);
}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();
}