Android USB host-to-serial connection? - android

Apparently with Android 2.3.4 and 3.1 one can now access USB accessories.
I have a Ardupilot Mega (based on Arduino) board with a USB cable connected to my laptop, and I can connect to it using a simple serial communications program over COM7 and 115,200 baud. This allows me into the command-line interface, and I can issue commands and get logs.
Is it possible to write an Android app that will communicate over USB to my Ardupilot Mega board?
I've seen many similar threads, but most of them were pre-USB host.

usb-serial-for-android is my open source library written for exactly this need. It supports FTDI and CDC-ACM usb serial devices using Android's USB host support; no root or ADK necessary. It can talk most Arduinos.
The project is still in its early days, but the basic support has worked well enough for several projects. There is also a discussion list where you can get help.

Unfortunately the library from mik3y's answer (see here: mik3y/usb-serial-for-android) seems to be long dead. It has many problems and waiting pull requests. Also the LGPL licence prevents anybody to fork it and maintain it.
But don't lose your hope, there's a working replacement and it's under the MIT licence. felHR85/UsbSerial.
EDIT: I found a maintained fork of mik3y's library. But the licence or readme is not updated, so I suppose the owner doesn't have a connection to mik3y and simply took it and improved it. I hope he won't get into any trouble because of the licence, because we need this library. See kai-morich/usb-serial-for-android
EDIT2: It seems that all of these libraries are maintained again, mik3y joined forces with kai-morich and they're working together now.
EDIT3: mik3y/usb-serial-for-android is under MIT license now. And still maintained in 2022.

Maybe. Check out the Android Open Accessories announcement on the Android Developers Blog. The Arduino Mega ADK is compatible. Not sure about the Ardupilot Mega.
From their page:
The Android 3.1 platform (also backported to Android 2.3.4) introduces
Android Open Accessory support, which allows external USB hardware (an
Android USB accessory) to interact with an Android-powered device in a
special "accessory" mode. When an Android-powered powered device is in
accessory mode, the connected accessory acts as the USB host (powers
the bus and enumerates devices) and the Android-powered device acts as
the USB device. Android USB accessories are specifically designed to
attach to Android-powered devices and adhere to a simple protocol
(Android accessory protocol) that allows them to detect
Android-powered devices that support accessory mode. Accessories must
also provide 500mA at 5V for charging power. Many previously released
Android-powered devices are only capable of acting as a USB device and
cannot initiate connections with external USB devices. Android Open
Accessory support overcomes this limitation and allows you to build
accessories that can interact with an assortment of Android-powered
devices by allowing the accessory to initiate the connection.
Note: Accessory mode is ultimately dependent on the device's hardware
and not all devices will support accessory mode. Devices that support
accessory mode can be filtered using a "uses-feature" element in your
corresponding application's Android manifest. For more information,
see the USB Accessory Developer Guide.
The following list of distributers are currently producing Android
Open Accessory compatible development boards:
The Arduino Store provides the Arduino Mega ADK (in EU nations or
non-EU nations) that is based on the ATmega2560 and supports the ADK
firmware. DIY Drones provides an Arduino-compatible board geared
towards RC (radio controlled) and UAV (unmanned aerial vehicle)
enthusiasts. Microchip provides a PIC based USB microcontroller board.
Modern Device provides an Arduino-compatible board that supports the
ADK firmware. RT Corp provides an Arduino-compatible board based on
the Android ADK board design. Seeed Studio provides an
Arduino-compatible board that supports the ADK firmware. SparkFun's
IOIO board now has beta support for the ADK firmware.

You can check out the library described in Slick USB 2 Serial Library.
It enables common USB-to-serial adapters to work with Android devices that have USB host mode without needing to root.

http://slickdevlabs.com/
I am currently using the library in my own app, however I think you can get away with just the app that slick has on the market. There are two free apps and one is the "pro" version of one of the free ones; search slick usb in Google Play.
It does use HOST mode and you can change the baud rate to whatever you need.

Related

How to make android device as slave and read the slave data from iot hardware as Electric Imp, Arduiono or even PC?

I am looking for a solution where my Android Device is connected to IoT devices such as Electric Imp, Arduino or Windows PC via USB.
So the requirement here comes that I want to write some data(text or binary data) at my Android USB port so that the other IoT devices which are connected via USB are able to read that data.
Android Device will act as a slave and IoT devices will act as master which will read the information.
Note: There is no connection with WiFi or Bluetooth, only via USB it is needed.
This is in practical terms likely a bad idea as an architecture, but if you want to pursue it is is exactly what the Android USB Accessory Protocol was designed to enable.
You need an embedded device capable of being a USB host, the reference implementation was the Android Accessory Development Kit (ADK) thought that hardware was never a great choice and obsolete as even in it's era ARM MCU's with native USB host support were already common.
Note that this method is opposite that of "USB OTG" where the Android devices is the USB host. Although Accessory mode requires a more capable embedded hardware (with host vs only device support) Accessory mode is more widely supported by Android devices, some of which cannot function in OTG mode, and many of which rapidly run down their battery when doing so. But both suffer from the physical USB connection to a sculpted-for-a-hand phone often making an intermittent and easily damaged connection in comparison to the sorts of connectors typically chosen for interfaces which will be semi-permanently connected.
You would probably also be able access the usb data lines directly as a serial connection that carries the raw information.

Is it possible to use OTG Host Negotiation Protocol to change modes while connecting two devices?

I have two android phones connected with an OTG cable. Obviously, the cable forces once phone into host mode and the other becomes the peripheral. I would like to be able to dynamically alternate the host/peripheral status of the devices without switching the cable around and the OTG HNP spec seems to indicate that is possible. I found some further information on it here but I'm not sure where to go with it - https://android.googlesource.com/kernel/msm/+/android-msm-bullhead-3.10-marshmallow-dr/Documentation/usb/msm_otg.txt
Yes, it is possible. Good find on the very technical documentation. However the Developer.android.com site and the USB.org has more information regarding implementation:
USB accessory and host modes are directly supported in Android 3.1 (API level 12) or newer platforms. USB accessory mode is also backported 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.
Link
As far as I can tell Host negotiation always favors traditional Host controllers like computers. Android devices have traditionally been USB accessories. However a very complicated situation arises when you have a hub with a host (like a PC) and another accessory plugged in. It seems like there is no way to settle the dispute about which device should be a host the Android or the PC. For instance I plugged in a host PC to a Android device with a HUB attached and a gigabit ethernet dongle. I could never establish a connection with the dongle. In fact the HNP never seemed to take place.
Finally, the documentation seems to suggest that the Host is responsible for operating the USB Stream. What seems most promising is the documentation Here which establishes the direction of a USBEndpoint.

Communicating with serial USB device over Android

I have one custom made device for measuring current. This device can be connected to PC and communication is done over USB. It is based on FTDI chip. I have application written in Java and that application is using rxtxSerial.dll library and gnu.io.rxtx_2.1.7.4.jar. Now I want to port this application on my android device and I'm litte bit confused what to do that? All I have is android tablet (os version 4.1.1), OTG cable and this measuring device.
Should my device be rooted? Is there any kind of library that I can simply add to my Java Android application and read data over USB? Where to start?
I have tried to port existing application for PC using same library, but rxtxSerial.dll is missing and I can't transfer it to my android device.
I can really recommend the USB-Serial-For-Android library, it supports multiple popular usb-to-serial chips (also FTDI) and there is no rooting required. Check out the GitHub project here.
There is a project dedicated to serial communication on Android. android-serialport-api. I think, it is a good resource to start with.
FTDI also provides Android related resources.
I think there's currently only one properly maintained library for serial USB communication and it's felHR85/UsbSerial. It supports several USB chips including FTDI (see Devices Supported).
Unfortunately mik3y/usb-serial-for-android seems to be dead.
And no, you don't need to root your device for connecting to and communicating with a USB device.
EDIT: We have a maintained fork of mik3y's library. See kai-morich/usb-serial-for-android
EDIT2: It seems that all of these libraries are maintained again, mik3y joined forces with kai-morich and they are working together.

Android ADK bluetooth device compatibility

I am trying to get started with Android ADK.
My goal is to connect Arduino UNO + blue-tooth and Android devices via blue-tooth.
I discovered that ADK needs Linux kernel 2.6.35+ which is used for Android 2.3.4 to allow communication via USB.
So it's clear that all devices should support this requirement for USB communication.
But what about blue-tooth? Does it mean that the same rules applies to blue-tooth interaction?
Is it possible to establish connection and data exchange with device for instance with Android 2.1 (API level 7).
So, any explanations are warmly appreciated
UPDATE#1
My primary goal is to connect Android 2.1 device with Arduino via blue-tooth.
My first thoughts were that I should use ADK but I discovered that at least for USB it depends on linux kernel version.
But I think it still may be possible to use blue-tooth instead of USB and possibly I may still use ADK, if not how can I connect Arduino with android via blue-tooth without ADK.
Well.. Not sure I understand your problem exactly but I try to give you the best possible answer I can.
There is no relationship between ADK and Bluetooth. You need ADK if you use a USB cable to connect your ADK compatible development board with your Android mobile. The USB compatible board or the ADK shield contains a USB peripheral controller so it can control the USB devices through cable.
In your case you don't need any physical connection between your Arduino and the android device so it is a matter of Bluetooth connectivity. You just have to use a cheap Bluetooth Module with your mobile (Not needs to be an Android anyway).
Now you have to connect the Bluetooth module with your Arduino. Now you have two Bluetooth enabled equipment so you are ready to communicate. I found some good tutorials on web that you can use as a starting point.
How to control Arduino over bluetooth
Arduino Android Bluetooth Communication
Let me know if you need further clarification.
Bluetooth functionality seems to have been a part of the Android APIs since level 5. Thus Android 2.1 should work just fine!

USB code for Android 2.3.4

I want to use usb in my application to retrieve data from my computer (present in text files) to android mobile and read it... what are the neccessities in terms of hardware and helpful links for the usb code...
Recently google launched USB accessory for android 2.3 ... it have classes UsbManager, UsbAccessory etc... i want to use that ... how can it be used.??
Usually the opposite of this is observed, where the android mobile projects itself as a usb device (client) , The USB host controller driver on the computer recognizes the device and handles future USB requests from your client. This is just the hardware layer, mind you. Since the USB protocol has become so ubiquitous, the computer actually stores multiple families of drivers both at the kernel layer and user space to be able to handle the variety of requests and their user space interpretations.
Since android mobiles are primarily linux-based devices, to achieve your end you will need to integrate these USB host-based driver families to your device's kernel / ROM. You will need to create a custom ROM (someone else asked this question here). This link provides you a list of drivers corresponding to specific hardware families.
Additionally, you will need to set up necessary programs on your computer that allows it to project itself as a USB client.
If you use linux on the computer too, this article might help you to start.

Categories

Resources