Hello, I am having such a frustrating time with this, I'm learning
about handlers and have hit a dead end. I apologize if I'm in the
wrong forum
I have a bluetooth device that is sending the temperature to the
android phone in a constant stream. I want to display the string on
the screen. I have connected the bluetooth and I can read the
results, similar to bluetooth chat:
To summarize before I give you the code the message that I am suppose
to receive is 37. So I have two problems:
1) sometimes I get 3, 7, or 37 in the debug (not on the screen). I
can't figure out where it is getting out of sync
2) Handler isn't being called after a message is received, but after
the program crashes. What am I doing wrong?
************Temperature Program*****
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onStart() {
super.onStart();
dialog = ProgressDialog.show(this, "Connecting",
"Searching for a Bluetooth serial port...");
thread = new CommThread(this, mHandler);
thread.run(); //Runs CommThread Connection and I/
O
}
//...Other code here...
private final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "Calling Handler");
}
};
}
*************************
*********************CommThread********************
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
istream = tmpIn;
ostream = tmpOut;
StringBuffer sb = new StringBuffer(); //Used to read what
message says
String message; //Used to read what message says
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
while (true) {
try {
// Read from the InputStream
bytes = istream.read(buffer);
//Determine what message says before handler
sb.setLength(0);
sb.append(new String(buffer, 0, bytes));
message = sb.toString();
Log.d(TAG, "The message is: " + message);
//Call Handler
mHandler.obtainMessage(Temperature.MESSAGE_READ,bytes,
0,buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write!",e);
break;
}
}
}
*******************
***********Debug
04-23 13:42:04.552: DEBUG/BluetoothChatService(28755): The message is:
3
04-23 13:42:04.552: DEBUG/BluetoothChatService(28755): The message is:
7
04-23 13:42:05.552: DEBUG/BluetoothChatService(28755): The message is:
37
04-23 13:42:06.545: DEBUG/BluetoothChatService(28755): The message is:
37
04-23 13:42:07.547: DEBUG/BluetoothChatService(28755): The message is:
3
04-23 13:42:07.571: DEBUG/BluetoothChatService(28755): The message is:
7
04-23 13:42:08.546: DEBUG/BluetoothChatService(28755): The message is:
3
04-23 13:42:08.572: DEBUG/BluetoothChatService(28755): The message is:
7
04-23 13:42:28.949: ERROR/BluetoothChatService(28755): Exception
during write!
04-23 13:42:28.949: ERROR/BluetoothChatService(28755):
java.io.IOException: Software caused connection abort
..........
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
***********
So every time I receive a message I want to send it to the Handler,
like BluetoothChat program does.
The Handler isn't being called until the program crashes in the end.
Thanks for your help,
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en