Reputation: 951
I'm writing an app that will receive serial data from another device (not Android, it will be Bluesmirf Silver module if it matters, but now I'm only trying on my laptop Bluetooth adapter). Here is the code i'm using:
public class ListenThread extends Thread {
private BluetoothSocket socket;
private InputStream istream;
private Handler handler;
private BluetoothAdapter adapter;
public ListenThread(BluetoothAdapter adapter, Handler handler) {
Log.v(TAG, "creating ListenThread");
this.adapter = adapter;
this.handler = handler;
if (adapter.isDiscovering()) {
adapter.cancelDiscovery();
}
}
public void run() {
if (running == false) {
// Thread is cancelled
Log.v(TAG, "run(): running == false");
return;
}
if (adapter == null) {
// No BT adapter supplied
Log.v(TAG, "run(): adapter == null");
return;
}
if (btDevice == null) {
// No btDevice is paired
Log.v(TAG, "run(): btDevice == null");
return;
}
try {
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
adapter.cancelDiscovery();
socket.connect();
Log.v(TAG, "run(): socket connected");
} catch (IOException e) {
Log.v(TAG, "run(): socket not connected");
Log.v(TAG, "IOException: " + e.toString());
e.printStackTrace();
socket = null;
}
if (socket == null) {
// Connection to device failed
Log.v(TAG, "run(): socket is null");
return;
}
InputStream tmpIn = null;
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
// getInputStream always returns InputStream
// but operations will throw IOException until
// the stream is ready
}
istream = tmpIn;
StringBuffer sb = new StringBuffer();
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from the read();
String message;
int idx;
HashMap<String, String> hm;
String[] chunks;
Log.v(TAG, "run(): listening loop starting");
while (true) {
try {
// Read from the InputStream
bytes = istream.read();
sb.append(new String(buffer, 0, bytes));
while ((idx = sb.indexOf("\r\n\r\n")) > -1) {
message = sb.substring(0, idx);
sb.replace(0, idx + 4, "");
hm = new HashMap<String, String>();
for (String line : message.split("\n")) {
chunks = line.trim().split("=", 2);
if (chunks.length != 2) continue;
hm.put(chunks[0], chunks[1]);
}
handler.obtainMessage(0x2a, hm).sendToTarget();
}
} catch (IOException e) {
break;
}
}
}
I always get an IOException at the socket.connect() line. I tried several methods of making the connection, non of them worked, but each threw a different IO Exception:
method 1:
socket=BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress()).createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
method 2:
socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
method 3:
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
I also tried like this:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
I always got either Service Discovery Failed or Connection Refused. This is how I'm managing the ListenThread:
public void startListening() {
Log.v(TAG, "starting to listen");
running = true;
}
public void stopListening() {
Log.v(TAG, "stoping listening");
running = false;
}
I searched Google but all I found were the different methods of connecting the socket, but as I said, none of them worked. Any idea? Thanks very much!
EDIT:
This is what Log.v(TAG, "IOException: " + e.toString()); and e.printStackTrace(); prints:
IOException: java.io.IOException: Service discovery failed
java.io.IOException: Service discovery failed
W/System.err(8604): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:397)
W/System.err(8604): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:207)
W/System.err(8604): at com.ginger.kanarci_android.communication.BluetoothService$ListenThread.run(BluetoothService.java:133)
Upvotes: 1
Views: 2274
Reputation: 951
So I've finally been able to sole this. The only problem was the UUID. In the code I posted, I used the well known SPP UUID, but my laptop's bluetooth adapter had a different UUID, that's why the two couldn't connect I think.
When I configured the Bluesmirf module (which has the SSP UUID by default) and tried to connect from the app, it worked perfectly.
Upvotes: 2