I am new to android. my application is using bluetooth to talk to a embedded board. I am using android bluetooth chat example to open a bluetooth socket and start a thread.
private class ConnectedThread extends Thread
{
public ConnectedThread(BluetoothSocket socket)
{
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer ; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
Log.d("MR", "input stream :"+(new String(buffer)));
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
//a delay of 20ms occurs after each flush...
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
and I have a message handler
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
//Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
//connectedThread.write(s.getBytes());
connectedThread.write(s.getBytes());
Log.i(tag, "connected");
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
//String string = new String(readBuf, 0, msg.arg1);
Toast.makeText(getApplicationContext(), "Test", 0).show();
// Create the text view
//TextView textView = (TextView)findViewById(R.id.rcvedMsg);
//textView.setTextSize(40);
//textView.setText(string);
break;
case RECIEVE_MESSAGE:
byte[] readmsgBuf = (byte[])msg.obj;
String string = new String(readmsgBuf, 0, msg.arg1);
Toast.makeText(getApplicationContext(), "Test", 0).show();
// Create the text view
//TextView textView = (TextView)findViewById(R.id.rcvedMsg);
//textView.setTextSize(40);
//textView.setText(string);
break;
}
}
I am unable to receive any data back from the embedded device. Embedded device is running a rfcomm server and it can receive data from my android app. On connection the server is definitely sending data.