Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Delving into Mobile Hardware

Exploring USB integration on Android

 

 
Farhan Rasheed
Android Developer at Swiggy
Published: November 05, 2020
Tweet
Share
 

Photo by Alexandre Debiève on Unsplash

 

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.

 

Image for post

 

 

Image for post

 

Image for post

An android-based device with a dedicated 1D/2D scanner

 

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

 

Image for post

The device in action

 

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

 

Image for post

An MH-ET V3.0 Live barcode scanner

 

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.

 

Image for post

 

Image for post

A DIY set up to integrate the barcode scanner

 

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.

 

Changes required in the AndroidManifest.xml file

 

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

 

Image for post

Default communication parameters

 

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

 

Image for post

Command documentation of 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.

 

Voila!

I have created a simple application with a Button to trigger a scan and a TextView to populate the data that is received from the scanner.

Look at the application in action. It is almost identically to the demo application provided by the device manufacturer.

 

Image for post

The DIY barcode scanner in action

 

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.

 

References

https://developer.android.com/guide/topics/connectivity/usb/host

 

mik3y/usb-serial-for-android

This is a driver library for communication with Arduinos and other USB serial hardware on Android, using the Android…

github.com

 

 

 

Tags: Android, Hardware, USB, Point Of Sale, Barcode Scanner

 

View original article at: 


 

Originally published: October 28, 2020

Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code 🗣 With Kotlin Scopes
Noisy Code 🗣 With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
Android News
KMM QuickStart Guide
KMM QuickStart Guide

By Mayank Kharbanda

Kotlin Multiplatform (KMP) is a code-sharing technology that allows us to use the same code for different platforms whether it’s JVM, Android, iOS, watchOS, tvOS, Web, Desktop, or WebAssembly. In this article, we will use Kotlin Multiplatform Mobile (KMM) which is a subset of KMP with the focus on providing better tooling and support for sharing code on mobile platforms i.e. Android and iOS.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One