tidev / appcelerator.ble

A collection of API's to connect and communicate with BLE compatible devices via Bluetooth LE
https://titaniumsdk.com/api/modules/ble.html
Other
7 stars 3 forks source link

didUpdateValueForCharacteristic event failing #275

Open shanith32 opened 2 years ago

shanith32 commented 2 years ago

We are working on a BLE app that acts as a Central and connects to a chip(peripheral). The peripheral contains a service and a characteristic and the characteristic contains a notify property. After calling peripheral.subscribeToCharacteristic() and peripheral.readValueForCharacteristic() methods, it subscribes successfully with the didUpdateNotificationStateForCharacteristics event, however errors out on the didUpdateValueForCharacteristic event. The error message is: 257: failed to initiate reading characteristic for peripheral name

m1ga commented 2 years ago

searching for the error message I guess you are trying to implement it on Android, right? Run your app with --log-level debug to see some more logs.

It should show you:

Log.d(LCAT, "readValueForCharacteristic(): characteristic- " + characteristicProxy.uuid()
                        + " read initiation status- ." + isReadInitiated);

(source)

with false as isReadInitiated. According to https://stackoverflow.com/a/30720262/5193915 it could be a timing issue. Did you have a look at the examples in https://github.com/tidev/appcelerator.ble/tree/master/example how they do the flow?

shanith32 commented 2 years ago

@m1ga Thank you for the above information. I ran a build with --log-level debug and found this, [DEBUG] TiBleCentralOperationManager: readValueForCharacteristic(): characteristic- 4aa10003-d92d-48d3-b522-509b8524ac31 read initiation status- .false

We cloned the centralManager.js and peripheralChannel.js files from the example and nothing much is changed on them.

Here's where the error occurs on the peripheralChannel.js https://github.com/tidev/appcelerator.ble/blob/ff1591fd1bdaaae4df10868f82c9c44a6c15ce49/example/peripheralChannel.js#L215

The BLE chip(peripheral) that we are connecting to only has a notify property on its characteristic, so maybe calling peripheral.readValueForCharacteristic() on line https://github.com/tidev/appcelerator.ble/blob/ff1591fd1bdaaae4df10868f82c9c44a6c15ce49/example/peripheralChannel.js#L388 is the problem. If that is the case would you be able to let me know which method to call/event to listen to access data streamed with a characteristic's notify property? Appreciate your help!

m1ga commented 2 years ago

have a look at this code:

cmgr.addEventListener('didDiscoverPeripheral', function(e) {
    cmgr.connectPeripheral({
        peripheral: e.peripheral,
        options: {
            [BLE.CONNECT_PERIPHERAL_OPTIONS_KEY_NOTIFY_ON_CONNECTION]: true,
            [BLE.CONNECT_PERIPHERAL_OPTIONS_KEY_NOTIFY_ON_DISCONNECTION]: true
        }
    });
});

cmgr.addEventListener('didConnectPeripheral', function(e) {
    peripheral = e.peripheral;

    peripheral.addEventListener('didDiscoverServices', function(e) {
        var services = e.source.services;

        services.forEach(function(service) {
            if (service.uuid.toUpperCase() == 'DEVICE_UUID') {
                e.source.discoverCharacteristics({
                    characteristics: [],
                    service: service
                });
            }
        });

        peripheral.addEventListener('didDiscoverCharacteristics', function(e) {
            var peripheral = e.peripheral;
            var characteristics = e.service.characteristics;

            characteristics.forEach(function(characteristic) {

                function _didUpdateValueForCharacteristic(e) {}
                function _didReadValue(e) {}

                if (characteristic.uuid == 'CHAR_UUID') {
                    global.charactersticObject = characteristic;

                    peripheral.subscribeToCharacteristic({
                        characteristic: characteristic,
                        descriptorUUID: BLE.CBUUID_CLIENT_CHARACTERISTIC_CONFIGURATION_STRING,
                        descriptorValue: BLE.ENABLE_NOTIFICATION_VALUE
                    });

                    peripheral.readValueForCharacteristic(characteristic);
                    peripheral.addEventListener('didReadValueForCharacteristic', _didReadValue);
                    peripheral.addEventListener('didUpdateValueForCharacteristic', _didUpdateValueForCharacteristic);
                }
            });
        });
        peripheral.addEventListener('didUpdateValueForCharacteristic', function(e) {});

        peripheral.discoverServices();
    });
});

cmgr.startScan();

I had that in an example file. That should be the correct flow