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 🙂

6 thoughts on “Android Adventures – Keeping alive a Bluetooth connection

  1. Nash_Node

    Yadi yadi ya…ok miss PHD, I got a question for you.

    I need to install sinhala unicode on a motorola xoom…running android 3.1 honeycomb

    I guess I got the last part correct.. I am no techie geek like you chapes 🙂

    I guess you already know what xoom is, but just in case, its the iPad ass kicking version from Motorola 🙂

    Reply
  2. niroshini

    @Nash
    Hey you’re back from the dead!
    😛

    I haven’t actually tried this, but it looks as if someone else has already tried with Nexus one. Take a look here:
    http://sinhala.kalingasblog.com/2010/07/25/sinhala-unicode-on-android-google-nexus-one/

    The link above explains how to do it to Android 2.2. For how to root a Honeycomb, take a look here:
    http://androidadvices.com/root-motorola-xoom-honeycomb/
    I guess the other parts (installing the font) holds the same for Honeycomb.

    Whatever you do, do backup all your contacts and data. Rooting could backfire and erase everything!

    Anyway let me know how it goes..I might try to do this on my Androids too.

    Reply
  3. niroshini

    Yep it looks like u’d have to be in sad face mode…
    You should make a complaint to Google/Motorola. There should be a forum somewhere. The more people voice their concerns, the better. It’ll hurry them into providing the upgrades.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *