Category Archives: research

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 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
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()));
} catch (IOException connectException) {



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 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:

Happy coding!

Presenting my first paper at IITC

Last Wednesday I got to present my paper titled ‘Morpholocal Parser for Sinhala verbs’ at IITC, BMICH. It was based on my undergrad thesis… which I realized I’d almost forgotton about on the morning of the presentation!

Anyway I was originally planning to go to office and leave for bmich by 12 but I had this panic attack that I didnt remember anything I was gonna talk about so I decided to take the whole day off 🙁 And spent till like 11.20 poring over the slides at home.

So to cut a long stressful story short I went there by 12.15 but spent 15 more minutes trying to find ‘Commitee Room A’. Those people at the front sent me a regular ‘Parangiya Kotte giya vage’ tour around the darned bmich. At noon in Colombo this sort of excursion can make you really annoyed. And all this in a sari!!#$??##

My presentation was under ‘Localization and Local Language Processing Applications’ and was to be the last of the session which was about at 3pm. After that Black Magic thingie they served for lunch I was pretty de-stressed and relaxed so the presentation went OK.

Here’s the abstract of my paper if you’re interested:
This paper presents a morphological parser capable of analyzing and generating Sinhala verbs. Morphological analysis and generation plays a vital role in many applications related to natural language processing, such as spell checkers, grammar checkers, intelligent information retrieval, machine translation and other complex applications. The parser consists of a lexicon of more than 400 verb stems and handles 45 inflectional rules for each stem. Analyses produces the verb stem together with its feature tags depicting verb class, person, number, tense, gender, mood, voice, etc. The parser is modelled in the framework of two level morphology model using Xerox finite state morphology tools. To our knowledge, this is the first such parser for Sinhala verbs.

Keywords: Morphology, Natural Language Processing, Parsing, Sinhala

I was dissapointed with the turnout though. Maybe because it was the last day of the conference..But it was nice to be told that your research was interesting and that people were impressed! which I was told by 2 total strangers during tea 🙂 Since your collegues would say this to you anyway coz they’re just being nice it was good to hear it from strangers 😀

But the nicest part came while I was on my way back to the office to catch my transport… Several of my friends texted me to tell me that they had watched the whole thing on internet (UCSC tv)! Too bad I didn’t know about that before..Hmm on second thought better that I didn’t know..Would’ve freaked out probably!