Delving into Mobile Hardware
Exploring USB integration on Android
Samsung, Google, OnePlus, etc would be the manufacturers that you generally would be working with because of the larger software-based consumer markets. Android, however, is a sophisticated operating system that can be used to integrate very specialized hardware to do some tasks very efficiently. There is a very niche category of devices that use integrated hardware to perform highly specific tasks.
I was presented with an opportunity to work with one such device. It has hardware dedicated to doing one thing consistently and efficiently, which is scanning barcodes over and over again. It is used in warehouses of online commerce giants to logistic based upstarts to do multiple hundred scans every day.
This has a 1D/2D decoding ready integrated barcode scanner that scans a barcode in under a second with a negligible impact on the battery and is very efficient compared to scanning using the device’s camera and works in all lighting situations.
This device got me intrigued. I wanted to understand a few things
- Can I find some kind of external hardware that can scan a barcode which more general-purpose in nature and have decent documentation?
- What are the specifications and APIs using which external hardware can be integrated with the Android operating system?
- What are the commands that would be needed to perform a scan and what kind of callbacks are needed to be implemented to actually read the scanned data?
- What would it cost to make a hobby-level proof of concept?
Documenting my quest to find an answer to each of these questions
Finding the hardware
Being based out of India, I found the best hardware that suits my needs here
It has the following features
- Integrated image decoder
- Small and power-efficient form factor
- USB to Serial interface based on the CH340 chip
- Detailed documentation with multiple programmable settings
- Affordable price point
I had a Xiaomi Redmi 4 lying around, so using that to demonstrate that even low-cost old Android phones are quite capable when it comes to performing dedicated tasks.
This phone is capable of acting as a USB host and can be programmed to work with the hardware scanner.
Below is the setup I’ve created using a micro-USB OTG cable and some double-sided tape to stick the scanner on the back of the phone.
Writing the Android Driver
Registering interest for the USB device
To have your application discover a particular USB device, you can specify an intent filter to filter for theandroid.hardware.usb.action.USB_DEVICE_ATTACHED
intent.
Along with this intent filter, you need to specify a resource file that specifies properties of the USB device, such as product and vendor ID.
Since the scanner exposes a communication interface using the CH340, the vendor id and product id of that chip are used in the device filter file.
Querying for the device
Requesting permission to access the device
Setting up the communication channel
Sending/Receiving data to the device
I will not go into more detail regarding the I2C serial communication bus
The following are the default settings of the barcode scanner. USB communication has been set up to use these via the controlTransfer method of the USBDeviceConnection class
This is a well documented open-source project that supports all the popular USB to serial chips(FTDI, Prolific, Silabs, Qinheng). I have used this as a guide to program the barcode scanner which is a Qinheng CH340 chip
Commands and Callbacks for the Scanner
It is pretty straight forward to trigger the scanner and read the data returned by the scanner.
We have to send “~T.” as a byte array to the USB device.
We would receive a “T” as an acknowledgment if the trigger was successful and we would need to wait for the data post that with a timeout.
How much did it cost?
Being a mobile developer, I had most of the things used at home but still listing down the cost of individual items used in this build
- Xiaomi Redmi Note 4 — $90
- MH-ET V3.0 Live barcode scanner — $30
- Micro USB OTG connector — $1
- USB to micro USB cable — $1
Having spent most of my professional career writing code for purely software-oriented products, this was a good detour and a very welcome weekend project to explore the lesser-known aspects of the Android operating system. This also is the first half of the larger Android-based point-of-sale system project that I’m working on.