9.5 Android and USB

Hello Readers!! Hope you are enjoying great. Today we shall study USB. USB stands for Universal Serial Bus. It is an industry standard which defines the connectors, cables and communication protocols used in a bus for connection, communication and power supply between devices and computers. It standardizes the computer peripherals. Peripherals include keyboard, digital cameras, pointing devices, etc. These are meant to standardize both communication and power supply standards. We are not concerned about the details of USB but in the relationship between USB and android respectively. Let us study about this new guy.

9.5.1 Introduction

According to various sources, we have two modes through which android supports variety of USB peripherals and USB accessories and they are:

  1. USB accessory
  2. USB Host

USB accessory and host modes are supported in Android 3.1 (API level 12) or newer platforms directly. Accessories may include robotics controllers, diagnostic and musical instruments, card readers, etc. External USB hardware acts as USB hosts when we talk about the USB accessory mode. USB host mode is supported in Android 3.1 and higher. This provides Android powered devices capability to interact with the USB hardware. Those devices which don’t have the ability to interact can do this because of this functionality. There is a protocol called Android accessory communication protocol. All the USB accessories designed for android powered devices must adhere to this protocol. When the Android-powered device is in host mode, it powers the bus and in turn acts as USB host. In host mode android powered device acts as the USB host, powers the bus, and supports connected USB devices.

There are two types of communication with USB device namely, synchronous and asynchronous. No matter which is the communication type, we will have to create a new thread on which all data transmissions are carried out. This is done so that we do not block the User Interface thread. We need to have permission before communicating with USB device. Explicitly asking for permission might be necessary in some situations such as when our application enumerates USB devices that are already connected and then wants to communicate with one. We must check for permission to access a device before trying to communicate with it. We will receive a run time error if the user denied permission to access the device otherwise.

Following diagram shows a simplified scenario of USB host mode:

Figure USB Host Mode


Android powered devices act as host in case of host mode. Examples include keyboard, game controllers, etc.   When the Android-powered device is in USB accessory mode, the connected USB hardware acts as the host and powers the bus. USB accessory mode is also back ported to Android 2.3.4 (API level 10) as an add-on library to support a broader range of devices. Device manufacturers can choose whether or not to include the add-on library on the device's system image. USB accessory mode allows users to connect USB host hardware specifically designed for Android-powered devices. When an Android-powered device is in USB accessory mode, the attached Android USB accessory acts as the host, provides power to the USB bus, and enumerates connected devices.

The add-on library is a wrapper for the framework API(s), the classes that support the USB accessory feature are similar. You can use the reference documentation for the android.hardware.usb even if you are using the add-on library. To set up communication with accessory we need to do following things and they are:

  1. Intent filter can be used. This filters accessory attached events or specifies the  connected accessories and then searching the suitable one we require to discover the attached accessories.
  2. We need to ask permission from user before communicating with accessory.
  3. Communicate with the accessory by reading and writing data on the appropriate interface endpoints

Application can discover accessories by either using an intent filter to be notified when the user connects an accessory or by enumerating accessories that are already connected. Using an intent filter is useful if you want to be able to have your application automatically detect a desired accessory. If you want to get a list of all connected accessories or if your application did not filter for intent then enumerating connected accessories is useful.

Explicit permission is required in some situations for example when our application specifies accessories that are already connected and then wants to communicate with one. We must check for permission to reach an accessory before trying to communicate with it. In otherwise scenario, we will get an error at execution time if the user aborts/denies permission to reach the accessory. For explicit permission we have to first create a broadcast receiver. This broadcast receiver listens for the intent which gets broadcast when we call requestPermission()

The UsbManager can be used to communicate with accessory to obtain a file descriptor.  This can set up input and output streams which read and write data to corresponding descriptor. The stream corresponds to input and output major endpoints of accessory. The communication between the device and accessory should be set in another thread. It is done so that we do not block the main User Interface thread. After communicating with an accessory or accessory was detached then we need to shut down the file descriptor which was opened by calling close() method.                                                                                                                      

Following diagram represents the USB accessory mode:

Figure USB Host Mode

                                                                                            Figure USB Accessory mode

Congratulations ladies and gentlemenJ!!! We are done with this section. Hope this section was informational. See you in the nest section. Till then keep practicing. Happy App Developing!!!