weliem / blessed-android

BLESSED, a Bluetooth Low Energy (BLE) library for Android
MIT License
557 stars 119 forks source link

Notify of Characteristic Change Fails on 2.4.0 #168

Open sean7512 opened 2 years ago

sean7512 commented 2 years ago

When using version 2.3.4, everything works fine; however updating to 2.4.0 breaks notifying.

Inside of BluetoothPeripheralManager is the following:

public boolean notifyCharacteristicChanged(@NotNull final byte[] value, @NotNull final BluetoothGattCharacteristic characteristic) {
    Objects.requireNonNull(value, CHARACTERISTIC_VALUE_IS_NULL);
    Objects.requireNonNull(characteristic, CHARACTERISTIC_IS_NULL);

    if (doesNotSupportNotifying(characteristic)) return false;

    boolean result = true;
    for (BluetoothCentral device : getConnectedCentrals()) {
        if (!notifyCharacteristicChanged(value, device, characteristic)) {
            result = false;
        }
    }
    return result;
}

When using 2.4.0, the getConnectedCentrals() call is returning an empty list; however in 2.3.4, it is returning the centrals correctly.

sean7512 commented 2 years ago

I think the call in the loop may need changed to getCentralsWantingNotifications(). Unfortunately, I have run out of time today to continue testing.

weliem commented 2 years ago

I think this is related to the other issues you are reporting. In 2.4.0 the logic for getting connected devices was changed and now uses the library internal lists rather than the OS.

sean7512 commented 2 years ago

I don't think so, because it doesn't work even if I turn off advertising. There are 2 methods for notifying of characteristic changed, 1 that takes a central to notify and 1 that doesn't. On the one that doesn't, it is still querying the OS and not using the internal list, as it should. When using the debugger, getCentralsWantingNotifications(characteristic) returns the expected values, but the method is calling getConnectedCentrals() which is returning an empty list.

weliem commented 2 years ago

I tried the example (https://github.com/weliem/bluetooth-server-example) and everything seems fine, but that is of course only a peripheral.

sean7512 commented 2 years ago

I can submit a PR soon, I have a local code change that is working, its a very minor change.

Thanks for continuing to help me.

sean7512 commented 1 year ago

Another interesting finding.....so it gets removed from the internal list because of a random onConnectionStateChange callback that says it was disconnected. If you use bluetoothManager.getConnectionState(device, BluetoothProfile.GATT) in that call, it will return that it is connected, contradicting the notification. I wonder if we can only fall down and call handleDeviceDisconnected(device) if the OS tells us it was disconnected?

archie94 commented 1 year ago

Is there any update on this? We are facing this problem too.

archie94 commented 1 year ago

I had to revert to version 2.1.2 We were not getting this problem on debug variants but on release variants (2.4.0 and 2.3.5) onCharacteristicUpdate was not being called.

weliem commented 1 year ago

@archie94 are you sure this is the same issue? There is no onCharacteristicUpdate in BluetoothPeripheralManagerCallback....

archie94 commented 1 year ago

Umm .. this one

    public void onCharacteristicUpdate(@NotNull BluetoothPeripheral peripheral, @NotNull byte[] value, @NotNull BluetoothGattCharacteristic characteristic, @NotNull GattStatus status) {}

in BluetoothPeripheralCallback

Also may be related but from Nordic BLE library:

https://github.com/NordicSemiconductor/Android-BLE-Library/pull/395

weliem commented 1 year ago

@archie94 , you are referring to an issue with a BluetoothPeripheral but @sean7512 is referring to an issue when notifying to a BluetoothCentral....

Please open a new issue if you still are having this issue.

strike76 commented 1 year ago

I had to revert to version 2.1.2 We were not getting this problem on debug variants but on release variants (2.4.0 and 2.3.5) onCharacteristicUpdate was not being called.

Same problem here. No problem with debug release, i need to use 2.2.3 version