RxBluetooth
RxBluetooth copied to clipboard
read failed, socket might closed or timeout, read ret: -1
Hi when i try to make a connection with a device it gives me this error
read failed, socket might closed or timeout, read ret: -1
After making a connection with the device , the phone send a connection request with a pair number and the other phone accepts it , but it still gives me the error.
this is my code
rxBluetooth.observeDevices() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Consumer<BluetoothDevice>() { @Override public void accept(BluetoothDevice bluetoothDevice) throws Exception { if(bluetoothDevice.getName() != null && bluetoothDevice.getName().equals("Test")) { Log.e("RxBt",bluetoothDevice.getName()); rxBluetooth.cancelDiscovery(); connectToDevice(bluetoothDevice); } if(bluetoothDevice.getAddress() != null) Log.e("RxBt",bluetoothDevice.getAddress()); //addDevice(bluetoothDevice); } });
Connecting to device method
private void connectToDevice(BluetoothDevice bluetoothDevice) { Log.e("connectToDevice()","connectToDevice invoked."); UUID uuid = UUID.fromString("f77d314e-9730-4a0b-a3a8-0d283eff53ea"); rxBluetooth.observeConnectDevice(bluetoothDevice, uuid) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Consumer<BluetoothSocket>() { @Override public void accept(BluetoothSocket socket) throws Exception { // Connected to the device, do anything with the socket Log.e("connectToDevice()","Start of Connection."); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { Log.e("connectToDevice()",throwable.getMessage()); } }); }
Did you manage to solve this issue? I'm stuck on the same thing.
Discovery works fine, but this library is useless when you can't actually connect.
I didn't until now.
Seems like you need to observeBluetoothSocket
to open a BluetoothServerSocket
and listen for a connection request :
mRxBluetooth.observeBluetoothSocket("MyConnection", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
.subscribeOn(mSchedulerProvider.io())
.observeOn(mSchedulerProvider.ui())
.subscribe(new Consumer<BluetoothSocket>() {
@Override
public void accept(BluetoothSocket bluetoothSocket) throws Exception {
// ...
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
// ...
}
})`
then mRxBluetooth.observeConnectDevice(...)