0
votes

I would like to use RxBluetooth to transfer some text between two Android devices. I'm able to scan and see nearby devices, though while trying to connect to one of them I face the following failure:


D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null

W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback

D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[65]}

java.io.IOException: read failed, socket might closed or timeout, read ret: -1


This is the code I run in order to establish a connection with a selected BluetoothDevice:

mRxBluetooth.observeConnectDevice(bluetoothDevice, 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 {
                    // Unable to reach here.
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    // I reach this point with the message:
                    // java.io.IOException: read failed, socket might closed or timeout, read ret: -1
                }
            })

Bluetooth is enabled on both devices, as I'm able to discover one from the other.

Permissions I use are:

<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Thank you.

1
FWIW, this sample app transfers text between two devices using RxBluetooth.CommonsWare

1 Answers

0
votes

Problem is solved. I forgot to observeBluetoothSocket():

        mRxBluetooth
            .observeBluetoothSocket("MyConnection", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
            .subscribeOn(mSchedulerProvider.io())
            .observeOn(mSchedulerProvider.ui())
            .subscribe(this::observeIncomingMessages,
                    throwable -> {/*Handle error*/});

Commonsguy's RxEcho ( the suggested sample app in CommonsWare's comment ) helped me realize this fact. RxEcho also helped me discover RxJava2 Extras by David Moten which made reading data from the socket very convenient:

        Strings.from(btSocket.getInputStream())
           .subscribeOn(mSchedulerProvider.io())
           .observeOn(mSchedulerProvider.ui())
           .subscribe(data -> {/* Post data to LiveData*/},
                   throwable -> {/* Handle error*/});

Thank you for taking the time and reading my question.