Warehouse barcode scanner with helpful Android tip
Using registerBroadcastReceiver within NativeScript can often achieve an effective solution for common integrations.
Warehouse barcode scanner with Android
One day a client needed an app for a rugged PDA for their warehouse.
As a growing developer who got into the software development world through NativeScript, I was only used to creating apps for normal mobile phones. So I didn't know how to develop for a rugged PDA. After some research, I realized that the device I had to develop for was an Android-powered device.
That implied I could develop the same way I do for a standard Android mobile phone. NativeScript made this possible and I just want to provide a small tip on what was handy about it.
Receive data from the scanner with NativeScript
The key solution was to use Application.android.registerBroadcastReceiver
To receive the barcode scanned by the Android device in your NativeScript app, you can call the registerBroadcastReceiver method on the Android application instance as follows:
const receiverCallback = (
context: android.content.Context,
intent: android.content.Intent
) => {
this.articleCode = '';
this.articleCode = intent.getStringExtra('DATA');
// could use any event bus
// just using a simple system setup I created
this.$emit('barcodeScanned', { barcode: this.articleCode });
};
Application.android.registerBroadcastReceiver(
'com.android.serial.BARCODEPORT_RECEIVEDDATA_ACTION',
receiverCallback
);
- The first argument of
registerBroadcastReceiveris the intent filter provided by the device manufacturer. The device I developed with was from Seypos and they provided thecom.android.serial.BARCODEPORT_RECEIVEDDATA_ACTIONintent filter. - The second argument is a callback function to receive the scanned barcode for use in the rest of your app.
Never did I think I would encounter a Rugged PDA for development in the field, but this simple and elegant yet extremely effective NativeScript snippet helped get the job done.
Conclusion
Sometimes short but sweet provides just the clarity you might be after in a crunch. Happy NativeScripting! 💙