Code Monkey home page Code Monkey logo

Comments (8)

dariuszseweryn avatar dariuszseweryn commented on July 30, 2024

Hello,
Thank you for your report.
I will check tomorrow this scenario. I think that it may be a bug in the library because it is listening for BluetoothAdapter's state changes and maybe when the connection is not yet established it's misbehaving.

from rxandroidble.

klemzy avatar klemzy commented on July 30, 2024

I see. Also some more information. The weird thing is that in RxBleGattCallback onConnectionStateChange gets called but not onServicesDiscovered. Also another thing. When I debugged with breakpoints on bluetoothGatt.discoverServices() and onComplete inside RxBleRadioOperation getSubscriber then it worked.

from rxandroidble.

dariuszseweryn avatar dariuszseweryn commented on July 30, 2024

I have tried to reproduce the issue today on Motorola XT1030 but apparently switching off the BluetoothAdapter while establishConnection(_, true) is making the connection never being established without any exceptions. I have waited for around 10 minutes.

Could you paste the code you are using and which mobile phone model?

Apparently it would be best to make the library fail any connections that are in the process of establishing if the BluetoothAdapter will go off.

from rxandroidble.

klemzy avatar klemzy commented on July 30, 2024

I see, but did you try to connect again after turn off/on with device being discoverable? Because my problem is that when Bluetooth turns on I call establishConnection and then it hangs.

I am using Nexus 6P, Android N Preview 4

device.establishConnection(context, true)              
            .flatMap(rxBleConnection ->
                    rxBleConnection.getCharacteristic(CHARACTERISTIC)
                            .flatMap(characteristic -> 
                                    rxBleConnection.writeCharacteristic(setValue(characteristic))
                                            .cache()
                                            .flatMap(characteristic1 -> readData(rxBleConnection))
                            )
            );

This is the code.

from rxandroidble.

dariuszseweryn avatar dariuszseweryn commented on July 30, 2024

I don't see if you're unsubscribing the first connection (started before the off/on switch) and you shouldn't be able to call on the same device .establishConnection() again anyway because it would emit BleAlreadyConnectedException.

It would be great to see the logs from the library (use RxBleLog.setLogLevel(RxBleLog.VERBOSE))

You could also describe when do you unsubscribe from the first connection.

from rxandroidble.

klemzy avatar klemzy commented on July 30, 2024

I am calling unsubscribe but I did not include the whole snippet. I am listening to the adapter on/off and unsubscribing appropriately. Also I unsubscribe when I finish reading from the device.

I am using latest code commited in this repository.

When I was debugging (I included library source into my project) the code worked?!?!?, services got discovered.

I also found a way to simulate this by not turning on/off the adapter:

.subscribe(bluetoothGatt -> {
                        try {
                            Method method = bluetoothGatt.getClass().getMethod("refresh");
                            if (method != null) {
                                method.setAccessible(true);
                                boolean result = (boolean) method.invoke(bluetoothGatt);
                                RxBleLog.d("Refresh result " + result);
                            }
                        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        bluetoothGatt.close();
                    },
                    throwable -> onError(throwable),
                    () -> onCompleted()
)

Basically inside RxBleRadioOperationDisconnect I have added refresh function. This clears discovered services cache from the device (which also happens on adapter off). At least what the Google and internet says.

So at the moment I managed to fix this with the folling code inside RxBleRadioOperationServicesDiscover.

Handler handler = new Handler(Looper.myLooper());
handler.postDelayed(() -> {
    final boolean success = bluetoothGatt.discoverServices();
    RxBleLog.d("Discover services " + success);
    if (!success) {
        subscription.unsubscribe();
        subscriber.onError(new BleGattCannotStartException(BleGattOperationType.SERVICE_DISCOVERY));
    }
}, 5000);

Maybe there is a better solution to this problem.

I have attached logs.logs.txt

from rxandroidble.

dariuszseweryn avatar dariuszseweryn commented on July 30, 2024

What do you refer to when saying I am using latest code committed in this repository.? The latest SNAPSHOT or the latest release? Maybe your SNAPSHOT is not up to date. Unfortunately I couldn't reproduce your problem and refreshing the BluetoothGatt is not the best idea to reproduce the behaviour as I think it may not be the cause of the original problem.

from rxandroidble.

dariuszseweryn avatar dariuszseweryn commented on July 30, 2024

Actually last SNAPSHOT was from May since there was a styling issue which prohibited pushing to the maven repo. There is a new SNAPSHOT available since yesterday now.

from rxandroidble.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.