Can't scan after connect dispose/clear

52 Views Asked by At

I am connecting to Bluetooth Low Energy (BLE) device using the library rxandroidble2.

After successful connection to the device, I'm testing reconnection after forcibly terminating the connection.

Although the disposible for the connection has been disposed, the device does not switch to the advertise state.

Only when turn off the screen of cell phone will the BLE device switch to advertised state and scanning will be possible.

Is there a way to make sure the code disconnects from the BLE device?

Below is my viewmodel code.

public void doOnDeviceSelectPage() throws Exception {
        if (rxBleClient == null) {
            initRxBleClient();
            return;
        }

        if (disposable.isDisposed()) {
            disposable = new CompositeDisposable();
        }


        disposable.add(
                onDeviceSelectPageBleUseCase.execute(rxBleClient)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                                rxBleConnectionPair -> {
                                    ScanResult scanResult = rxBleConnectionPair.first;
                                    RxBleConnection rxBleConnection = rxBleConnectionPair.second;
                                    String scannedDevice = new String(scanResult.getScanRecord().getManufacturerSpecificData(0xffff));

                                    // Handle each scan result
                                    Log.d("BleConnect", "Device connect: " + scannedDevice);

                                   
                                    onDeviceConnected();
                                    if (scannedDevice.equals("A")) {
                                        deviceController.setRxBleConnection(rxBleConnection);
                                        deviceController.onDeviceConnected();
                                    } 
                                },
                                throwable -> {
//                                    onDeviceDisconnected();
                                    // Handle errors
                                    Log.e("BleScan", "Error: " + throwable.getMessage());
                                    onDeviceDisconnected();
                                }
                        )
        );
    }


    public void onDeviceDisconnected() throws Exception {
        setConnectedDevice(null);

        disposable.dispose();
    }

}

I tried removing rxbleconnection and rxbleclient in the viewmodel, but it had no effect. Even after closing the app, the device did not return to the advertise state, and could only return to the advertise state after closing the screen.

0

There are 0 best solutions below