DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Changes To The Run() Method Of BluetoothChatService's ConnectedThread Thread
// Changes to the run() method of BluetoothChatService's ConnectedThread thread
// more info on http://kr3l.wordpress.com/2010/09/04/using-android-…remote-control/
private void gets(InputStream mmInStream, byte[] buffer, int[] bytes) throws IOException {
int inputdata;
bytes[0] = 0;
while((inputdata = mmInStream.read()) > -1) {
char inputchar = (char)inputdata;
buffer[bytes[0]++] = (byte)inputchar;
if (inputchar == '\n') {
return;
}
}
}
private void sendToast(String str_msg) {
Message msg = mHandler.obtainMessage(DroidBot.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(DroidBot.TOAST, str_msg);
msg.setData(bundle);
mHandler.sendMessage(msg);
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int[] bytes = { 0 };
sendToast("TRYING TO CONNECT");
String answer = "";
while (!answer.equals("PONG\r\n")) {
try {
// Read from the InputStream
write("PING\n".getBytes());
gets(mmInStream,buffer,bytes);
answer = new String(buffer, 0, bytes[0]);
mHandler.obtainMessage(DroidBot.MESSAGE_READ, bytes[0], -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
sendToast("CONNECTED!");
// Start the loop of request sensors > reply > set motors
while (true) {
try {
// Read from the InputStream
gets(mmInStream,buffer,bytes);
mHandler.obtainMessage(DroidBot.MESSAGE_READ, bytes[0], -1, buffer).sendToTarget();
//write("GET_SENSORS\r".getBytes());
//gets(mmInStream,buffer,bytes);
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}





