Category Archives: android

Android Adventures – Keeping alive a Bluetooth connection

Continuing my adventures into the Android domain, I wanted to have a Bluetooth connection between two nodes, going indefinitely, until one node sends a termination signal. Between the time of initializing the connection, and termination, the two nodes may exchange messages at random intervals.Now, the Bluetooth chat example provided in
http://developer.android.com/resources/samples/BluetoothChat/index.html is very helpful. I did use some of the code, but my client connection was defined in the Activity class itself, as a inner class extending Thread:

public class MyActivity extends Activity {
 
 //some code here that does the work in the activity
 
 
   private class MyThread extends Thread {
        private final BluetoothSocket socket;
private final BluetoothDevice device;
private WorkerInfo worker;
        public MyThread(WorkerInfo pInfo) {
BluetoothSocket tmp = null;
device = pInfo.getDevice();
worker = pInfo;
try {

UUID uuid = UUID.fromString(CommonConstants.WORKER_UUID);
tmp = device.createRfcommSocketToServiceRecord(uuid);

} catch (IOException e) {
}
socket = tmp;
}

public void run() {
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
socket.connect();
worker.setSocket(socket);
worker.isConnected = true;
if (D)
Log.d(TAG, “connected”);

String devName = socket.getRemoteDevice().getName();
String devAddr = socket.getRemoteDevice().getAddress();
Log.d(TAG, “connected to ” + devName + “:” + devAddr
+ “at DelegatorThread ” + this.getId());
Thread t = new Thread(new Reader(socket.getInputStream()));
t.start();
} catch (IOException connectException) {
return;
}
}

  }

}

I would call the MyThread to start() at the click of a Button, and then open an OutputStream on another Button click and write to the other node. This went fine. Then after several minutes, I wanted this to read from the other device. This is where it went wrong.
For some reason, after writing, my connection just dies. When the other device tries to write, it gets a connection reset by peer exception. However, if instead of waiting for several minutes, the other device wrote back immediately, the connection is kept alive.

there were two things I could do :

  1. Keep the connection alive by exchanging random bytes of data. I tried this out by implementing a KeepAliveClient thread and a KeepAliveServer thread. These would write and read small messages indefinitely till the actual message/s is/are ready to be transmitted. This could be done so that the actual messages are preceded by a special pattern so that the reader knows it has to switch from KeepAlive mode to actual Reading mode. After reading the actual message, it again switches back to KeepAlive mode.
  2. Share the Bluetooth object instance across Activities using the singleton model as suggested here. I implemented this version as well:

Put the MyThread into a seperate public class, and instantiate and start this thread from a singleton   class.  I call the method in singleton class from my Activity. This worked as well.

I’m not entirely sure the reason a singleton object method works though. Will continue working in the 2nd method because obviously it is more efficient (not having to keep transmitting), and hopefully things will clear out in future experiments 🙂

Bluetooth support on Android Emulator

I have been playing around with Android lately, and one thing that annoyed me is the emulator does not support Bluetooth (as opposed to J2ME emulator, which does).
So if you do not have an actual Android device (like me), and you want to do Bluetooth/WiFi programming, you run in to a problem.Fortunately, there is a way out – Install the Android image on a VM and use your own Bluetooth device. This is what I did:

  1. Download Androidx86 from http://www.android-x86.org/. This is an .iso file, so you’d need something like VMWare or VirtualBox to run it. Me, I use VirtualBox.
  2. When creating the virtual machine, you need to set the type of guest OS as Linux instead of Other.
  3. After creating the virtual machine, set the network adapter to ‘Bridged’.
  4. Start the VM and select ‘Live CD VESA’ at boot.
  5. Now you need to find out the ip of this VM. Go to terminal in VM (use Alt+F1 & Alt+F7 to toggle) and use the netcfg command to find this.
  6. Now you need open a command prompt and go to your android install folder (on host). This is usually C:Program FilesAndroidandroid-sdkplatform-tools>.
  7. Type adb connect IP_ADDRESS
  8. There done! Now you need to add Bluetooth. Plug in your USB Bluetooth dongle.
  9. In VirtualBox screen, go to Devices>USB devices. Select your dongle.
  10. Done! now your Android VM has Bluetooth. Try powering on Bluetooth and discovering/paring with other devices.
  11. Now all that remains is to go to Eclipse, and run your program. The Android AVD manager should show the VM as a device on the list.

I found these links helpful. Perhaps you would too:
http://androiddevnotes.com/2011/03/08/1299521520000.html
http://developer.android.com/guide/topics/wireless/bluetooth.html#EnablingDiscoverability

🙂
Happy coding!