I have a Newland MT90 barcode reader android mobile. I am creating an application in which I want to scan two barcode. When I am running my code and I press any button first time then it is working fine. At the same time when I press another button then it is replacing Bar Code in both TextView. How can I resolve this ?
btnScanUid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent("nlscan.action.SCANNER_TRIG");
intent.putExtra("SCAN_TIMEOUT", 4); // SCAN_TIMEOUT value: int, 1-9; unit: second
intent.putExtra("SCAN_TYPE ", 1); // SCAN_TYPE: read one barcodes during a scan attempt
//sendBroadcast(intent);
ReadUID(intent);
}
});
btnScanPart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("nlscan.action.SCANNER_TRIG");
intent.putExtra("SCAN_TIMEOUT", 4); // SCAN_TIMEOUT value: int, 1-9; unit: second
intent.putExtra("SCAN_TYPE ", 1); // SCAN_TYPE: read one barcodes during a scan attempt
//sendBroadcast(intent);
ReadPart(intent);
}
});
public void ReadUID(Intent intent){
sendBroadcast(intent);
registerReceiver(new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
if (intent != null) {
barcode = intent.getStringExtra("SCAN_BARCODE1");
int barcodeType = intent.getIntExtra("SCAN_BARCODE_TYPE", -1);
if(barcode!=null) {
UidBarcode.setText(barcode);
UidBarcodeType.setText(String.valueOf(barcodeType));
String text=String.valueOf(barcodeType); // Whatever you need to encode in the QR code
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
UidImageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}else{
Toast.makeText(getApplicationContext(), "Barcode Not getting.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Scan Failed", Toast.LENGTH_LONG).show();
}
}
}, new IntentFilter("nlscan.action.SCANNER_RESULT"));
}
public void ReadPart(Intent intent){
sendBroadcast(intent);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
barcodePart = intent.getStringExtra("SCAN_BARCODE1");
int barcodeType = intent.getIntExtra("SCAN_BARCODE_TYPE", -1);
if(barcodePart!=null) {
PartBarcode.setText(barcodePart);
PartBarcodeType.setText(String.valueOf(barcodeType));
String text=String.valueOf(barcodeType); // Whatever you need to encode in the QR code
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
PartImageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}else{
Toast.makeText(getApplicationContext(), "Barcode Not getting.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Scan Failed", Toast.LENGTH_LONG).show();
}
}
}, new IntentFilter("nlscan.action.SCANNER_RESULT"));
}
Assuming the same Intent
is being returned, you'd just have to change the Intent
being issued:
Intent intent = new Intent("nlscan.action.SCANNER_TRIG");
intent.putExtra("SCAN_TIMEOUT", 4); // SCAN_TIMEOUT value: int, 1-9; unit: second
intent.putExtra("SCAN_TYPE ", 1); // SCAN_TYPE: read one barcodes during a scan attempt
// passing the resId:
intent.putExtra("SCAN_TARGET_FIELD", R.id.btnScanUid);
ReadUID(intent);
And then decide on .onReceive()
which field to update.