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:
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 :
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 :)
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;
}
}
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 :
- 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.
- Share the Bluetooth object instance across Activities using the singleton model as suggested here. I implemented this version 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 :)
Yadi yadi ya...ok miss PHD, I got a question for you.
ReplyDeleteI 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 :-)
@Nash
ReplyDeleteHey you're back from the dead!
:P
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.
GASP! !!! I can't believe you.... you being a coder your self asking me to crack through of someone elses hard work.....??? Shame..shame....
ReplyDeleteAnyways...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 protection......by 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 :-) ????
Haha no self respecting coder should refrain from hacking! that is, as long as they don't use it immorally.
ReplyDeleteAnyway, 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, :
http://code.google.com/p/android/issues/detail?id=5925 , 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?
Well..tr 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. :-)
ReplyDeleteSo, 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)
Exuse the typos ...touch typing is not the easiest thing in the world to do :-) .-- need some getting use too,
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYep it looks like u'd have to be in sad face mode...
ReplyDeleteYou 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.
Hey uncle Nash,
ReplyDeleteIf 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 :)
Cheers!