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,290
Last edited:

warwound

Expert
Licensed User
Longtime User
Great!
At last some example Mega ADK code that compiles and works.

Works perfectly on my Huawei Ascend G300 with ICS.
On my Novo ElfII Jelly Bean tablet, B4A-Bridge fails to log anything within the B4A IDE though(everything else works as expected).

And my old ZTE Blade running CyanogenMod Gingerbread fails as it has no USB Host or Accessory support.
That's a shame as i hoped the Blade could have a new lease of life used in some Arduino projects.
I'll have to research and see if a newer custom ROM is available that enables Host and Accessory support - not sure whether the Blade has the required hardware though so don't hold out much hope there.

My eagle eyes spotted an error in the sketch comments!
B4X:
digitalWrite(LED, LOW); // use the pin 23 LED as a connected indicator
(The LED is actually on pin 13).

Anyway - thanks, i at last have some working code i can use as a basis for my own code.

Martin.
 

agraham

Expert
Licensed User
Longtime User
not sure whether the Blade has the required hardware though so don't hold out much hope there.
Shouldn't need special hardware - that's part of the point of ADK, it works in slave mode. Let me know if you get anywhere with the Blade - I've got one in the drawer.
 

warwound

Expert
Licensed User
Longtime User
Shouldn't need special hardware - that's part of the point of ADK, it works in slave mode.

From what i have researched it seems as though the minimum requirement on the Android device is for linux kernel version 2.6.35+.
My Blade with CyanogenMod Gingerbread (Android version 2.3.7) has that kernel version.

However i installed AccessorySupportDetection from the Play Store and it told me the Blade had no support for USB Host or Accessory.

Martin.
 

warwound

Expert
Licensed User
Longtime User
Try to run it in Debug mode. Starting from Android 4.1 the logs cannot be read directly. In Debug mode the logs are sent to the device bridge program.

No luck with debug mode either.

The tablet is running a custom ROM 'Paranoid Android' which is android version 4.1.1.

Martin.
 

avalle

Active Member
Licensed User
Longtime User
Why app is not recognized as Arduino is plugged?

Hello, I've been able to run this sample with my Galaxy S3 running JB 4.1.2.
I've previously run an Arduino Mega ADK with the Google ADK2011 sample (written in Java) and every time I plug the Mega ADK to the phone the companion ADK app is launched automatically asking for permissions, and this is a great user experience.

I'm still not clear why with this sample we have the following behavior:

1) when the phone is plugged to the ADK the accessory is recognized but an error message claims no app working with the accessory is available, which is not correct;

2) why do we need to press the Connect button twice in the app? The first time is to grant permissions to the app, but why is the second push needed?

I'd like to understand if this behavior is due to limits in the sample or in B4A ADK support, and how to fix it in the first case.

Thanks
Andrea
 

agraham

Expert
Licensed User
Longtime User
1) If you follow this link, given in the first tutorial, USB Accessory | Android Developers, and look in the "Manifest and resource file examples" section you will see how to set up automatic recognition.

Add this to your Manifest Editor.
B4X:
AddActivityText(main, <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" />)
Put your accessory_filter.xml file in <your project>\objects\res\xml and mark it as read-only. You will have to work out the appropriate meta-data values from the properties of a UsbAccessory object when your accessory is connected. I chose not to include this in the demo mainly because I don't like automatic app launching but also because I thought it was an unnecessary complication.

2) As commented in the demo code the Android permission dialog, unlike Basic4android dialogs, is non-modal so the code in the Sub that called it runs on while the dialog is displayed. This means you can't wait then test if permission was granted so the Sub has to exit and the user try again once the dialog is dismissed.
 

avalle

Active Member
Licensed User
Longtime User
Thanks Andy, I had noticed your reference of the Android SDK page but didn't know how this should be implemented in B4A.

Now that the automatic recognition is done, I just need to press the Connect button once. Is this because the permission are granted automatically when I accept to launch the App when the Accessory is recognized?

I still face an issue with reconnection as the Exit button does not seem to kill the app on my Galaxy (JB 4.1.2). I have to kill it manually in order to avoid the write failed error ENODEV (No such device).
Is there any issue with ExitApplication and 4.1.2 ?

Thanks
Andrea
 

agraham

Expert
Licensed User
Longtime User
Now that the automatic recognition is done, I just need to press the Connect button once. Is this because the permission are granted automatically when I accept to launch the App when the Accessory is recognized?
Yes.

Is there any issue with ExitApplication and 4.1.2 ?
For Erel - the demo already calls ExitApplication but it doesn't seem to be working in this case. I don't know why it doesn't work for you but it works, and is necessary to reconnect, on my Motorola Xoom running 4.1.2.
 

avalle

Active Member
Licensed User
Longtime User
Actually I'm not sure if the app is killed or not. This is what I see after Exit:

- The phone returns to the Home screen
- If I long press the Home button I see two apps running (BA-Bridge and Arduino ADK Example)
- Then if I touch the pie chart icon (bottom left) only B4A-Bridge is reported as active application.

I'm wondering why is this inconsistent information.

And if select the Arduino ADK Example app from the list of apps running (long press Home button), it seems to be restarted, not to reactivate.

Also, I have unchecked the Filter checkbox in B4A Logs tab, but I don't get any information when the Exit is executed. What should I see there?

Thanks
Andrea
 

avalle

Active Member
Licensed User
Longtime User
I tried to call Activity.Finish before ExitApplication but the app is still in the list...

A new question: is it possible to force debug mode if it does not launch automatically?
The IDE goes in debug mode right after the app is Compiled & Run. But if I exit the app and launch it again, I see the "Waiting for IDE debugger to connect." toast message but the Debug is not activated.

Thanks
Andrea
 

avalle

Active Member
Licensed User
Longtime User
This is not really related to this thread. You can use Project - Run last deployment. However currently it doesn't work with B4A-Bridge only with USB debug mode.

The reason I think it's related to this thread is that I have to use B4A-Bridge because USB is busy connected to the Arduino ADK.

Good to know that "Run last deployment" does not work because I had tried it without success.
Does this mean that the only way to re-launch a Debug session when using B4A-Bridge is to redeploy the entire app with Compile & Run?

Also: the reason to run in Debug mode is that the Log function does not work with Android 4.1.2 and B4A-Bridge.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The reason I think it's related to this thread is that I have to use B4A-Bridge because USB is busy connected to the Arduino ADK.
It is still better to start a new thread. Otherwise it will be very difficult to find this post with the search feature.
Does this mean that the only way to re-launch a Debug session when using B4A-Bridge is to redeploy the entire app with Compile & Run?
Yes. Hopefully it will be fixed in the next version.
 

CharlesR

Member
Licensed User
Longtime User
Library error

1) If you follow this link, given in the first tutorial, USB Accessory | Android Developers, and look in the "Manifest and resource file examples" section you will see how to set up automatic recognition.

Add this to your Manifest Editor.
B4X:
AddActivityText(main, <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" />)
Put your accessory_filter.xml file in <your project>\objects\res\xml and mark it as read-only. You will have to work out the appropriate meta-data values from the properties of a UsbAccessory object when your accessory is connected. I chose not to include this in the demo mainly because I don't like automatic app launching but also because I thought it was an unnecessary complication.

2) As commented in the demo code the Android permission dialog, unlike Basic4android dialogs, is non-modal so the code in the Sub that called it runs on while the dialog is displayed. This means you can't wait then test if permission was granted so the Sub has to exit and the user try again once the dialog is dismissed.

Hi

I have worked my way throught the tutorials - Thanks very much, but when I try to upload the ADK demo I get the following errors. What am I doing wrong?

Thanks Charles

C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp: In static member function 'static void MAX3421E::setRST(uint8_t)':
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:58: error: 'PORTJ' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:58: error: 'PJ2' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:60: error: 'PORTJ' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:60: error: 'PJ2' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp: In static member function 'static uint8_t MAX3421E::readINT()':
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:65: error: 'PINE' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:65: error: 'PE6' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp: In static member function 'static void MAX3421E::pinInit()':
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:77: error: 'DDRE' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:77: error: 'PE6' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:78: error: 'DDRJ' was not declared in this scope
C:\Users\Charles\Documents\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:78: error: 'PJ2' was not declared in this scope
 
Top