Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

bundle

Changes to the run() method of BluetoothChatService's ConnectedThread thread
Saturday, 04 September 2010 13:54
// 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;
}
}

}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/dtsog3EgLjU/12175

 


Taxonomy by Zaragoza Online