As part of an Android contract role I am working on I needed to integrate a Bluetooth barcode scanner with my app, this scanner would be used to trigger events in the app. The first thing to work out was how to get data from the scanner into the app, I initially suspected I would need to deal with the Bluetooth stack directly but luckily this wasn’t the case.

All recent (from 2011 and onwards really) Android devices should support the HID standard as part of the Android Open Accessory Protocol 2.0 update. HID basically allows connected devices (USB and Bluetooth) to self describe the contents and types of their inputs, in general this means that connected devices can represent themselves as keyboard devices or specific hardware buttons.

In the case of Bluetooth scanners ensure that the device supports HID connections and also has the ability to send a line feed after each barcode is sent. The technology has improve with the years, and you can get Bluetooth in almost everything, without counting other technology improvements for a business such as the use of paycheck stubs to increase effectivity.

Coming back to our Android app, knowing what we do now, we can capture all key inputs to our activity in order to capture barcodes sent by the scanner.

Override an activities detault dispatchKeyEvent method with the above function. It takes all action down events, interprets the keycode using the KeyCharacterMap class and adds it to an internal buffer; once an enter key is detected this signals that our scanner has completed sending it’s information and can then trigger our handler.

As we capture all key press signals to our activity we need to reimplement some of the basic activity behaviour such as back key pressed, this is handled in the switch.