Skip to main content

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


  1. 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 :-)

  2. @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:

    The link above explains how to do it to Android 2.2. For how to root a Honeycomb, take a look here:
    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.

  3. GASP! !!! I can't believe you.... you being a coder your self asking me to crack through of someone elses hard work.....??? Shame..shame....

    Anyways...can't be done. This Xoom is a google, motorola and au kddi custom built. Even in the instruction manual it clearly says that this device is protected from all sort of unathorized root level custoization via software and custom built hardware the sound of it, I am pretty sure they might be having some rotwillers and rattle snakes tied to the motherboard as well.

    So know any legal way without giving me instructions on how to brick my Xoom :-) ????

  4. Haha no self respecting coder should refrain from hacking! that is, as long as they don't use it immorally.

    Anyway, I don't think rooting your Android is illegal. It would however, make your warranty void. Does it say in your manual that its illegal to root the device?
    That doesn't make sense...I mean its your phone. (BTW you read instruction manuals?? :P)

    Unfortunately there does not seem to be a way to have Sinhala without rooting. People are complaining about it, : , and according to the Google person's reply, they are aware of the situ.

    Anyway if this whole process is too geeky for you there should be lots of geeky Japs there for you to get it done?

  5. says both... that its protected I software and hardware level against any this like that and also voice my warranty if i do. This is not a phone, its a tablet pc like iPad2, rather better than iPad. :-)

    So, there is lot more to it than a phone I guess...and as for japanese nerds....effing stupid bastads will not touch anything under warranty. Its so crazy that for a joke I told them if all girls have a warranty badge pasted on their pu--ies, no one will be raped in Japan :-)

    Anyways, I also saw the link you sent me...but, well yet no solution for it right? ( sad face mode loaded)

  6. Exuse the typos ...touch typing is not the easiest thing in the world to do :-) .-- need some getting use too,

  7. This comment has been removed by the author.

  8. 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.

  9. Hey uncle Nash,
    If you just need to access Sinhala content online, there's a solution:

    SETT browser

    Works on Nexus S. Should work on Xoom as well.

    Just download and install :)



Post a Comment

Popular posts from this blog

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:
Download Androidx86 from This is an .iso file, so you'd need something like VMWare or VirtualBox to run it. Me, I use VirtualBox.
When creating the virtual machine, you need to set the type of guest OS as Linux instead of Other.
After creating the virtual machine, set the network adapter to 'Bridged'.Start the VM and select 'Live CD VESA' at boot.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.Now you need open a command prompt and go to …

පර්යේෂණ සහ ආචාර ධර්ම අවසරය

පසුගිය සති කීපයකම නිරත වෙලා හිටිය වැඩක ප්‍රතිපල ඊයේ ලැබුණා. ඒ මගේ පර්යේෂණය සඳහා ඕනෑ උන 'ආචාර ධර්ම අවසරය' නැත්නම් ethics clearence එක ලැබීමයි.

මගේ පර්යේෂණය මුලික වශයෙන් පරිගණක විද්‍යාව සහ මෘදුකාංග ඉංජිනේරු විෂයන් වලින් කෙරුනත්, මිනිසුන් ද සහභාගී කරවාගෙන කෙරෙන කොටසක් ඇති නිසා තමා මෙහෙම අවසරයක් ගන්ඩ උනේ.

ටිකක් පැහැදිලි කරොත්, මේ පර්යේෂණයේ ප්‍රථිපලයක් වන මෘදුකාංග පද්ධතිය නිවැරදිව වැඩ කරනවාද නැද්ද කියන එක තහවුරු කරන්න එක විද්‍යාගාරයක කරලා බලල මදි (සහ කරන්න අමාරුයි). 'හැබෑ ලෝකයේ' එය වැඩ කරන හැටි බලන්ඩ ඕනේ.

මගේ පර්යේෂණය 'ස්මාර්ට් තාක්ෂනය' නැත්නම්, කෘතිම බුද්ධිය සහිත ගෘහස්ත පද්ධතියක් (smart home) සම්බන්ධයෙන්. වෙනත් මෘදුකාංග වලට වඩා මේ වගේ පද්ධතියක් හදන කොට ගොඩක්ම සැලකිලිමත් වෙන්න ඕනේ මිනිස්සු මෙහෙම තාක්ෂනෙකට කොහොම ප්‍රතිචාර දක්වයිද කියලා. 
හිතන්න, ඔයාලගේ ගෙදරත් කෘතිම බුද්ධියක් නැත්නම් 'ස්මාර්ට්' පද්ධතියක් හයි කරලා කියලා. මේ වගේ පද්ධති වල අරමුණ මිනිසුන්ගේ ජීවිතය පහසු කරන එක. උදාහරණයකට ඔන්න ඔයාගේ 'බුද්ධිමත් ගෙදර' ලව්වා ඔයාට පුළුවන් වෙලාවට ලයිට් දාන්න/නිවන්න, …

පරණ අවුරුද්දේ අන්තිම POST එක

පරණ අවුරුද්දේ අන්තිම පැය ටිකනේ දැන් මේ ගෙවෙන්නේ. ඉතින් අන්තිම පොස්ට් එක සිංහලෙන් ලියන්න හිතුනා..
හ්ම්ම්..අද නම් මං අපරාදෙ කියන්නැතිව බහු ජන හිතාය වැඩ ගොඩක් කළා.. ඒ කිව්වේ අවුරුදු කෑමක් හැදුවා - සාලෙ මොප් කළා - පහන පොලිෂ් කළා ...ආ තව අසල්වාසීන්ට කැවිලි පිඟං අරන් ගියා...මගේ කාමරේ අස් කරා (- ඒක ඉතින් බහු ජන නෙවෙයි නෙ - මට විතරයි නෙ :D )

ඉස්සෙල්ලම කියන්නම් හදපු කෑම ගැන - හැදුවෙ බිබික්කම් හෙවත් පොල් කේක් හෙවත් පෝරං ආප්ප (කැමති එකක් කියාගන්න. අම්බලංගොඩ පැත්තෙ බිබික්කං කියනවා, පානදුරේ පෝරං ආප්ප, කොළඹ පොල් කේක් --- හ්ම්ම් පැටලුනාද මන්ද කොහොම හරි මං ඔය පළාත් තුනේම ඒව කියනව. අසන්නෝ සිහි බුද්ධියෙන් අසත්වා )

ඒක හදන්න හරී ලේසියි. අමාරු පොල් ගාන එක විතරයි :) පැණි සීනි ලිප තියලා ඒක පත් පැණි වෙන කොට පොල් සහ කැන්ඩීඩ් පීල් දාන්න යි තියෙන්නෙ. හැබැයි පත් පැණි සීන් එක නම් ටිකක් සංකීර්ණ්යි. ඒක බලන්න ගිහින් ඇඟිල්ලත් පිච්චුනා :(
මට මේ පාර කැන්ඩීඩ් පීල් හොයාගන්න බැරිඋනා :( අපරාදෙ කියන්න බෑ පුනර්ජීවනයේ නගරයේ නම් කේක් බඩු ගන්න නියම තැනක් තියෙනවා. මැරවින් ගේ අඩවියේ නැද්ද මන්ද.
ඉතින් මං මෝඩය වගේ ඉස්සෙල්ලම පොල් පැණි මි…