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.