Tuesday, May 8, 2012

Quick Note - Android - Activity Lifecycle Notes

When you run an app it goes through the following states: onCreate, onStart and onResume.

When you press home the app goes through: onPause and onStop.

When you go back to the app it goes through: onStart and onResume.

When you press back the app goes through: onPause, onStop and onDestroy

Sunday, May 6, 2012

Arduino to Android – Basic Bluetooth Connectivity Tutorial

This tutorial will show you how to configure an Ardunio to communicate over bluetooth with an Android device.

It assumes you have a basic knowledge of the Arduino, the Arduino IDE and installing Android applications from Google Play.

For this tutorial you will need the following items:

  1. An Arduino board.
  2. An Android device with bluetooth.
  3. A bluetooth serial board capable of connecting to the Arduino. 
  4. An Android bluetooth terminal program

You will also need the Arduino IDE.  The Android SDK and a breadboard are not required but are helpful.

What I used:

  1. A breadboard Arduino with a Modern Devices USB BUB.
  2. An Acer Iconia Tab A100 running Ice Cream Sandwich 4.0.3.
  3. A Sparkfun Bluetooth Mate Silver.
  4. BlueTerm by pymasde.es available on Google Play.

Arduino Configuration

Connect GND, VCC, RX and TX from the bluetooth device to the appropriate Arduino pins.  For my setup I connected GND to GND, VCC to +5V, RX to D5 and TX to D6.

Arduino Sketch

The Arduino sketch makes use of the SoftwareSerial library to communicate via two digital pins to the bluetooth device. You will need to make sure you set the SoftwareSerial baud rate to match the baud rate of your bluetooth device.

Android Setup

You will a program on Android that can connect to the bluetooth and host an SPP connection. I used BlueTerm from Google Play but you should be able to use another bluetooth terminal.

Run your bluetooth terminal. If you are using BlueTerm follow the steps below:

Select the three vertical dots to get the menu.

This next step is optional.  To make typing on your Android device easier you may want to turn on local echo.  Press preferences

and then select Local Echo. 

Return to the menu screen by pressing previous and select Connect Device.

Select your bluetooth device.  Mine is named mymate. If you are using the Sparkfun Bluetooth Mate when you successfully connect your Android device to it the blinking red LED marked "Stat" (red arrow in the above picture of the breadboard Arduino) will turn off and the green LED marked "Connect" (green arrow in the above picture) will turn on.

Compile and run the Arduino sketch and you should see output in BlueTerm and the Arduino serial monitor.

Serial Monitor

If you type characters on the Android device they should show up in the Arduino Serial Monitor.

Notes:

  1. As the SDK Emulator doesn’t emulate bluetooth and probably wouldn't be able to make a connection even if it did this example will not run in the emulator.
  2. This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.

Saturday, May 5, 2012

Android Example - Bluetooth Discover and List Devices and Services

This example does a bluetooth device discovery followed by a service discovery for every device it finds. The devices and services are listed as they are found.

There is one mystery that has me perplexed. Two identical sets of services are returned for each device. In the Android documentation it states that fetchUuidsWithSdp() will:
If there is an error in getting the SDP records or if the process takes a long time, ACTION_UUID intent is sent with the UUIDs that is currently present in the cache.
It is conceivable that the first set of responses is from cache and the second is from the remote device.

This example also demonstrates the use of a BroadcastReceiver, Intent Filters and a scrolling TextArea.

I have included the files that have content that is key to setting up the application.

You should be able to create a new Android project in eclipse and then copy the code included below into the appropriate file. Because fetchUUidsWithSDP() is called directly this example requires ICS or higher.

Below the code attachments you will find a screen capture of what it looks like running on my Acer A100.

If you have any suggestions or questions please post them as comments.

Download Zip Archive of Android Source Files

DiscoverDevicesAndServices.java
goes in <ProjectRoot>\src\com\example\discoverdevicesandservices\
strings.xml
goes in <ProjectRoot>\res\values\
main.xml
goes in <ProjectRoot>\res\layout\
AndroidManifest.xml
goes in <ProjectRoot>
Screen Capture from Acer A100

Notes:

  1. Due to fetchUuidsWithSDP() this requires API Release 15
  2. As the SDK Emulator doesn’t emulate bluetooth this example will not run in the emulator.
  3. This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.

Android Example - Bluetooth Simple SPP Client and Server

This example has both a client and a server. The client runs on Android and in my case I ran the server on Windows 7 x64. I tested both java server and a perl server.

A useful freeware tool for listing the services / UUIDs on a device is Medieval Software's Bluetooth Network Scanner.

I have included the files that have content that is key to setting up the application.

You should be able to create a new Android project in eclipse and then copy the code included below into the appropriate file.

Below the code attachments you will find a screen capture of what it looks like running on my Acer A100.

If you have any suggestions or questions please post them as comments.


Android SPP Client

The Android application is based on this post: Serial over Bluetooth simple test client. from anddev.org.

ConnectTest.java
goes in <ProjectRoot>\src\com\example\connecttest\
strings.xml goes in <ProjectRoot>\res\values\ main.xml
goes in <ProjectRoot>\res\layout\
AndroidManifest.xml
goes in <ProjectRoot>
Screen Capture from Acer A100


Java SPP Server

The java server was based on this the SimpleSPPSever example from JSR-82 Sample : SPP Server and Client. To get to run on Windows I used the bluecove-2.1.1-SNAPSHOT.jar library.

SimpleSPPServer.java
goes in <ProjectRoot>\src\
Screen Capture from PC


Perl Server

The perl server was built using the Net::Bluetooth module.

SimpleSPPServer.pl

Notes:

  1. As the SDK Emulator doesn’t emulate bluetooth this example will not run in the emulator.
  2. This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.

Wednesday, May 2, 2012

Andriod Example - Bluetooth Show Paired Devices

Here is a very simple example that I thought might be helpful to someone. I have included the four files that have content that is key to setting up the application.

The program checks to see if the device has bluetooth. If the device does and it isn’t turned on the program will prompt the user to turn it one. Once bluetooth is running it lists the paired devices.

You should be able to create a new Android project in eclipse and then copy the code included below into the appropriate file.

As the SDK Emulator doesn’t emulate bluetooth this example will not run in the emulator.

This example is built upon the information in the Bluetooth Dev Guide on the Android Developers site and the Android-Java Bluetooth on Samsung Galaxy post on stackoverflow.com.

Below the code attachments you will find a screen capture of what it looks like running on my Acer A100.

If you have any suggestions or questions please post them as comments.

ShowPaired.java
goes in <ProjectRoot>\src\com\example\showpaired\

strings.xml
goes in <ProjectRoot>\res\values\

main.xml
goes in <ProjectRoot>\res\layout\

AndroidManifest.xml
goes in <ProjectRoot>

Screen Capture from Acer A100



Notes:

  1. The code highlighting is done using SyntaxHighlighter which is maintained by Alex Gorbatchev and can be found at http://alexgorbatchev.com/SyntaxHighlighter/.
  2. This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.

Android Programming

As described in the previous post I have wanted to integrate touch screen technology into one of my projects.  A couple of weeks ago after reading some introductory information on the Android SDK I decided to install it and give it a try.

I am lucky to live in an area where our county libraries have a subscription to Safari Books Online.  After doing some general reading up based on google search hits I jumped on Safari and did a search for books on Android.

The search resulted in a number of hits and after reviewing them I settled on Learning Android by Marco Gargenta.  This lead me to the open source training company Markana which has an excellent TechTV repository.  One of the video series they have is their Android Bootcamp Training Course which was presented by Marko Garenta based on the Learning Android book.

I used the books and the online videos to work my way through chapters 1 – 8 which provided me with enough knowledge to start working on a couple of my own examples.

To move forward I have made a lot of use of other people who have gone before me.  A lot of what I have learned has been based on examples from various web sites and forums.  In an attempt to give something back I plan to post some examples that I got working in an attempt to pass on what others have shown me.

My development environment consists of:

  • Windows 7 Professional x64
  • Eclipse Indigo Release 2
  • Revision 19 of the SDK
  • Android 4.0.3 (API 15)
  • Acer Iconia Tab A100 running ICS 4.0.3

While I have a good bit of experience with IT technology and programming Java is not one of my strong points.  If you see something that I have done that is wrong or could be improved please let me know.

If you have questions that I may be able to help with please post them in the comments and I will get back to you as soon as possible.

Monday, April 30, 2012

It Worked for Me – Acer Iconia Tab A100

For a while I have wanted to integrate touch screen technology into one of my projects.  I had looked at a number of touch screens that could be integrated into a hardware project and then decided that maybe the way to go was to go with a tablet.

Based on what I had read about the ability to integrate Android and Arduino together I decided to look at an Android Tablet.  After installing the Android SDK and being impressed with what I saw and could do I was sold on going with Android.

I wanted something that was portable but had more screen real estate than a smart phone and as a result was looking for a 7” tablet.

I was looking at the recently released Samsung Galaxy Tab 2 7.0 and the Auraslate when I came across the Acer Iconia line of tablets.  I looked over the specifications and found a refurbished Tab A100 at Micro Center for $199.  I bought one last Thursday (4 days ago) and so far am very impressed.

acerTabA100
I really like that the Acer Tablets have standard ports along with the proprietary connector.  I also like that it has an HDMI connector as well as USB.

The only things to watch out for that I have found are:
  • The USB port is slave only and not slave/host. 
  • As it runs on 12 volts it cannot be charged via USB and requires a separate power supply.
  • It doesn’t seem to support hands free devices.
The A100 originally came with Honeycomb 3.2.1 but last night I was prompted to upgrade it and so it is now running Ice Cream Sandwich 4.0.3.

More to follow…

Tuesday, May 29th - It has been just about a month and I have done a good bit of programming, interfacing the Tab A100 via bluetooth to an Ardunio and via the ADK and a USB shield. So far I am still very happy with the Tab A100.