Android Tutorial Arduino ADk Consummation

Introduction

The big day has dawned at last. We are now going to connect our Arduino to our Android device and get the two to talk to each other and to their own IDE on the PC.


Make the connection

Now we are connecting the Arduino to an Android device we may have some power issues to consider as the Android device sees the Arduino as a host when it is connected. Because of this if the device is capable of charging through the USB port it will try to charge itself from it. Some tablets, like my Motorola Xoom, will not attempt to charge as they have a separate charging socket that allows them to be charged faster and at a higher voltage than a USB host port will permit.

When working stand-alone and not connected to an Android device or a PC the Arduino will obviously need an external power supply which will need to have enough capacity to power the Arduino and any shields attached to it. The power supply needs to be able to supply a continuous current capacity of about 0.5 Amp or more at a voltage between 7 and 12 volts.

When working connected to a PC by its slave port the Arduino will power itself from the PC and needs no external power supply unless there is a shield connected that needs more current than a USB port can supply

When working connected to an Android device by its host port and not connected to a PC the Arduino will need an external power supply which will need to have enough capacity to charge the Android device as well as power the Arduino and any shields attached to it. . The power supply needs to be able to supply a continuous current capacity of about 1.5 Amp or more at a voltage between 7 and 12 volts.

When working connected to both a PC and an Android device and not connected to an external power supply the Arduino will power itself from the USB port of the PC to which it is connected and will also pass power through to the Android device to charge it. If the USB port cannot supply enough power then the Arduino will not operate correctly. You can check this by downloading a known good sketch, like the Blink sketch, and observing if it works properly while both the PC and the Android device are connected to it.

If there is not enough USB power available and if your device is chargeable from a source other than its USB port then connecting that charger should ease the load on the USB power enough to make things work. Otherwise an external power supply for the Arduino will be required, and will in any case be needed if the Arduino is to run disconnected from the PC


Communicating with Basic4android

Finally we have come to what this series of tutorials is all about, talking to our Arduino application from Basic4android. As the Arduino is occupying the USB port of the Android device we will need to use B4A Bridge to download and run our Basic4android application. This enables us to avoid plugging and unplugging cables during development and conveniently allows us to work on both the Arduino and Basic4ppc program simultaneously.

I have written two simple companion programs that will bring together all that we have done so far. Both programs can be found in the archive attached to this post.

There is a program to run on the Arduino that will print whatever it receives from the Android device to the Serial Monitor in the Arduino IDE and return the same data to the Android device. It will also send any messages received from the Serial Monitor to the Android device. There is also a simple program to run on the Android device that will log whatever it receives from the Arduino. The design of the GUI is down to my traditionally low standards but adequately demonstrates how to use an Arduino as an Accessory. On the Arduino Serial operations run asynchronously to avoid hanging the main loop and I have used AsyncStreams to get similar asynchronous operation on the Android device.

Note that there is a bug in the USB library version 0.96 UsbManager.GetAccessories method which throws a NullPointer Exception if there is no Accessory attached to the Android device. This will be fixed in the next version of Basic4android.

There also seems to be a bug in the Android USB Accessory handling (what’s new!) when trying to reconnect to a disconnected Accessory. I struggled with for a few hours with an input stream exception when trying to reconnect before finding this.
android - Proper way to close a USB accessory connection - Stack Overflow
From that thread it seems even the official Google demo has this problem. Please post if anyone has any suggestions to overcome this.

In order to reliably connect to an Accessory with your program it is necessary to ensure that the process of any previous instance of your program that communicated with the Arduino has been killed. This is the reason why there is an Exit button in the Basic4ppc program that calls ExitApplication that you should use to kill the program. It is also necessary before restarting the program, or downloading and running a modified version again to either press the reset button on the Arduino or disconnect and reconnect it again. If you merely reaset the Arduino your USB permission seems to be maintained. If you disconnect and reconnect you will get the permission dialog again. In either case you will be offered the choice of opening a browser and going to the Basic4android home page


Getting started

Download the archive at the bottom of this post and copy the “ADKdemo” folder and contents to your “Documents\Arduino” folder and copy the “Arduino ADK” folder to where you keep your Basic4android projects.

Open “ADKdemo” in the Arduino IDE, download it to the Arduino and open Serial Monitor and set the Serial monitor to115200 baud.
baudrate.png


Connect the Arduino to your Android device. You may get an Android dialog offering to view the Basic4android home page – press Cancel.

Open “AduinoADKdemo.b4a” in Basic4android. Compile, download and run it, preferably via B4A Bridge as we are using the device USB port. Press the “Connect” button and you may get a dialog asking for permission. Accept it and press “Connect” again. Each press on “Send” will send a message to the Arduino which it will print to the Serial Monitor and send back to Basic4android which will log it to the Log pane in the Basic4android IDE.

serialmonitor.png


Any message sent from the Serial Monitor will also be logged.

b4aide.png


Exit the Basic4android application by pressing the Exit button to kill the process.


The next tutorial

The next tutorial will be the last and for some it will be superfluous as they will be able to take the two sets of source code and learn enough from them to be able to progress on their own. In it I will comment on both sets of source code and maybe add some appropriate homilies, should any actually come to mind!


Tutorials

In addition to this tutorial there are other tutorials. They are:


1. Introduction to the Arduino Mega ADK and Android Accessories. Introduction

2. Installing the Arduino IDE and compiling your first program. Installation

3. Connecting the Arduino Mega ADK to your computer and running programs on it. Programming

4. This one!

5. Some commentary on the two programs in tutorial 4. Coda
 

Attachments

  • AdruinoADK.zip
    8.6 KB · Views: 1,513
Last edited:

FrancescoS

Member
Licensed User
Longtime User
Great Job agraham !!
I'm new, my goal is to create a standalone alarm system with arduino ADK and my old samsung i9000 android 4.0.3.

First I try to autostart the agraham "Arduino ADK Example" while plug the usb cable from android ADK to i9000.
I followed the instructuions by agraham in this post:

- I have created the readonly "accessory_filter.xml" with this code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-accessory manufacturer="Arduino ADK Example" model="Arduino ADK Example" version="1.0" />
</resources>

- I have inserted the following code in the manifest editor:
?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="b4a.example"
android:versionCode="1"
android:versionName="1"
android:installLocation="internalOnly">

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="10"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
<application
android:icon="@drawable/icon"
android:label="Arduino ADK Example">
<activity
android:windowSoftInputMode="stateHidden"
android:launchMode="singleTop"
android:name=".main"
android:label="Arduino ADK Example"
android:screenOrientation="unspecified">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>
</application>
</manifest>

Compile ok
Transfer to i9000 ok
app works fine starting in manual mode

But if I plug the usb cable I receive the following message:


then...

Theese messages each time i plug the usb cable.
May You help me ?
Thanks
Francesco
 

Attachments

  • Foto-16-09-13-22-56-50.jpg
    Foto-16-09-13-22-56-50.jpg
    47.2 KB · Views: 538
  • Foto-16-09-13-22-57-25.jpg
    Foto-16-09-13-22-57-25.jpg
    41.2 KB · Views: 542

FrancescoS

Member
Licensed User
Longtime User
Hi Erel, no. It seems that it doesn't remember the association of USB accesory with the agraham app.
For Your info, before using Basic4Android, I have tested it with an example compiled in eclipse that i have founded in tha ADK tutorial in android, and it works.
Same so 4.0.3
Same i9000
Same ADK board

Thanks
Francesco
 

agraham

Expert
Licensed User
Longtime User
If you have not changed the Arduino program code this looks wrong.
<resources>
<usb-accessory manufacturer="Arduino ADK Example" model="Arduino ADK Example" version="1.0" />
< /resources>
For the accessory details specified in the demo sketch
B4X:
AndroidAccessory acc = AndroidAccessory("Anywhere Software",
"ADKDemo",
"Arduino ADK Board demo.",
"1.0",
"http://www.b4x.com",
"1");
I would expect to use
<resources>
<usb-accessory manufacturer="Anywhere Software" model=ADKDemo" version="1.0" />
< /resources>
 

FrancescoS

Member
Licensed User
Longtime User
Hi agraham,
now I need to receive data from arduino ADK board.
In Your example you send data to arduino, I need to implement a two-way data transfer.
ie: if i close an arduino pin, I need to know it in Basic4android.
Also what is Your advice to poll data from arduino in Basic4android ? (timer, interrupt, event ?)
Thanks
Francesco
 

akosije

Member
Licensed User
Longtime User
How's the connection of this ?

Android tablet to Arduino Mega ? Then whats next sir ? I cant open port COM 7 on Arduino IDE on my laptop coz i connect my arduino to my android tablet.
 
Top