Reputation: 495
So I'm trying to connect an Android app to a raspberry Pi, the devices are paired and are trusted by I receive an error identical to the one found here: IOException: read failed, socket might closed - Bluetooth on Android 4.3
However that solution seems really messy and I'm not even sure how to integrate it with my current code. So here's my current code:
@Override
protected void onHandleIntent(Intent intent) {
device = (BluetoothDevice) intent.getExtras().get("device");
if (device != null) {
Log.v("DeviceManager", "Beginning attempts to communicate to: " + device.getName());
try {
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(MY_UUID);
//Throws exception here:
socket.connect();
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
while (Utils.appIsInForeground && device != null) {
if (outputStream == null){
Log.e("DeviceManager", "Looks like the outputStream is null...");
if (inputStream == null){
Log.e("DeviceManager", "And input stream is null, are you even connected to the device?");
}else{
Log.e("DeviceManager", "Although strangely, input stream has been set.");
}
}else{
String myString = "This is a string!";
byte[] myByteArray = myString.getBytes("UTF-8");
outputStream.write(myByteArray, 0, myByteArray.length);
}
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
Log.e("DeviceManager", "Can't create a socket to the bluetooth device.");
} catch (InterruptedException e) {
e.printStackTrace();
Log.e("DeviceManager", "The sleep thread was interrupted.");
} catch (NullPointerException e) {
e.printStackTrace();
Log.e("DeviceManager", "A null pointer exception, this is probably not good.");
}
}else {
Log.e("DeviceManager", "The Device Manager has been passed a null object instead of a device.");
}
}
And this is the stack trace I'm getting:
03-26 11:44:09.525 7916-8048/com.app.name W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
03-26 11:44:09.526 7916-8048/com.app.name W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:685)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:697)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:374)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err: at com.app.name.service.DeviceManager.onHandleIntent(DeviceManager.java:60)
03-26 11:44:09.527 7916-8048/com.app.name W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:67)
03-26 11:44:09.528 7916-8048/com.app.name W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-26 11:44:09.528 7916-8048/com.app.name W/System.err: at android.os.Looper.loop(Looper.java:154)
03-26 11:44:09.528 7916-8048/com.app.name W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
03-26 11:44:09.528 7916-8048/com.app.name E/DeviceManager: Can't create a socket to the bluetooth device.
What's strange is that my Raspberry Pi comes up on BluetoothCtl saying that the phone connected but then it immediately disconnects.
Upvotes: 0
Views: 1116
Reputation: 1378
Connection would be created just for the querying the SDP database on your Raspberry board.
1) Check if the SDP on the Raspberry side really contains the requested MY_UUID record
2) It's not obvious if the Raspberry board is bonded with your Android. It's required because the you creates a secure socket in the code you posted.
Upvotes: 0