Reputation: 5070
I have made a simple application listening to the Bluetooth
serial communication with my Bluetooth module. Bluetooth module is in this case acting as a Server and my Android phone as a Client.
I was able to pair the devices and also to connect to it. However, I am not getting anything to the InputStream
. Any idea why? Might it be related to the fact that the module has PIN 1234 and I have never hard coded the PIN (but I was able to connect)
Code of the listener Thread:
public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
//private final OutputStream mmOutStream;
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) {
System.out.println("IO EXCEPTION: "+ e.toString());
}
mmInStream = tmpIn;
// mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // 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
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
processBytes(bytes);
} catch (IOException e) {
System.out.println("IO EXCEPTION: "+ e.toString());
break;
}
}
}
EDIT:
I have just noticed this error: android bluetooth connection fail (isSocketAllowedBySecurityPolicy start : device null) But I have tried to get UUIDs, and I have the correct one:
00001101-0000-1000-8000-00805f9b34fb
EDIT2:
This might be my issue as well, since I am on SG3 and I use SPP BT module. Android Bluetooth SPP with Galaxy S3
EDIT3: I have also tried to change the:
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
with:
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
as they suggest here: Bluetooth pairing without user confirmation
EDIT4: I have tried to use Android official sample for BT chatting. It had the same issue. I am sure my BT module works, because I have tried it on the PC, and also on the same SG3 with another BT terminal app. However, couple of BT terminals didn't work, I had to find the right one. Can there be any difference?
You should also know that I am sending IR signal to the IR receiver, which passes it to the BT module which then passes it to the mobile app. Baud rate of the IR receiver is 9600, same as IR LED.
Upvotes: 3
Views: 1492
Reputation:
Your code in the connected thread looks ok until your get to this point:
// Send the obtained bytes to the UI activity
processBytes(bytes);
So without seeing the rest of your code, I am assuming the problem is here. It's not getting to the catch, because there is no exception being thrown. But you are not handling how the bytes are being passed back to the UI correctly.
I notice you are using the code from the api here:
developer.android.com/guide/topics/connectivity/bluetooth
And the thing you are missing is this:
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
I too, had exactly this problem when I wrote my first bluetooth app. I was trying to simplify the code sample and attempted to exclude the handler.
/**
* The Handler that gets information back from the BTManager
*/
private final Handler mHandler = new Handler() {
@Override public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothManager.STATE_CONNECTED:
// TODO
break;
// todo .../
}
break;
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in
// the buffer
String readMessage =
new String(readBuf, 0, msg.arg1);
// TODO display your string message
break;
// todo .../
}
}
};
If need be you can download the sample developer.android.com/samples, it is important to manage the threads, I use sychroninized methods. Beyond seeing more of your code, this is the most likely guess.
If this doesn't answer your question, let me know.
Upvotes: 2