RxBluetooth icon indicating copy to clipboard operation
RxBluetooth copied to clipboard

read failed, socket might closed or timeout, read ret: -1

Open OmarBeshary opened this issue 6 years ago • 3 comments

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()); } }); }

OmarBeshary avatar Apr 15 '18 21:04 OmarBeshary

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.

RanyAlbegWein avatar Jun 03 '18 15:06 RanyAlbegWein

I didn't until now.

OmarBeshary avatar Jun 03 '18 17:06 OmarBeshary

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(...)

RanyAlbegWein avatar Jun 03 '18 17:06 RanyAlbegWein