You are viewing an older revision! See the latest version
mbed with Android ADK
mbed with Android ADK¶
Introduction¶
With the growth of android as the mobile OS of choice for many producers, Google recently released a accessory development library/kit (ADK) for developing hardware addons for devices. Although these libraries only work on version 2.3.4 or 3.1 or later, the ADK provides a robust USB serial link from platform to app. This cookbook page is about integrating it with mbed, and how to create apps with it.
The mbed side¶
The mbed side is composed of the USB host library, wrapped by the Android Accessory Class, which provides a stable connection to and from the system. In using the library one needs to develop a custom implementation, a few examples are shared later. The implementation includes the USB visible details of the device, such as name, manufacturer and version. It also provides a set of virtual functions which are used for call-backs when data is received or the sending is completing and start-up functions. USB is best suited to sending packets, rather than a serial stream, as it has a packet system, so a printf function is not implemented, instead there are char array outputs, which can be written to simply using a sprint function or direct char manipulation. The AndroidAccessory library was written primarily by mbed user: Makoto Abe, and was then modified by yours truly (p07gbar) into a library which works pretty stably in read and write.
The android side¶
As an embedded developer mostly I find android development a complete pain, so I have tried to abstract all of the USB part of the operating system clutter away into an easy to use library, providing byte array and string writing and a function call on new data. However android does not let it be quite that easy, one has to add some lines to the AndroidManifest.xml file. This really isn’t that tricky but is really essential, or it the whole app just crashes on boot. Details are available here (http://developer.android.com/guide/topics/usb/accessory.html) down the page. Although this library will only work with 2.3.4 targets, these can be run on any later android systems and the library would not be too hard at all to be modified for 3.1 (a few lines) to use the different libraries. The AdkPort library is very simple to use, and requires only a few lines to be added to the program:
private AdkPort mbed;
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mbed = new AdkPort(this);
Thread thread = new Thread(mbed);
thread.start();
mbed.attachOnNew(new AdkPort.MessageNotifier(){
@Override
public void onNew()
{
byte[] in = mbed.readB();
// Do something with the data
}
});
...
}