jrowberg / bglib

BGLib implementation for Bluegiga BLE Bluetooth Smart modules
Other
239 stars 170 forks source link

Indication not working #51

Closed faxioman closed 6 years ago

faxioman commented 6 years ago

Hi. I'm using your great bglib library (c#) to create a react native module for windows wpf/uwp. I'm starting to implement notification and indication. Notifications are working perfectly but I'm unable to work with indication. This is my piece of code:

        [ReactMethod()]
        public async void monitorCharacteristicForService(JObject device, string serviceIdentifier, string characteristicUUID, IPromise promise)
        {
            Device dev = device.ToObject<Device>();
            TaskCompletionSource<byte[]> task = new TaskCompletionSource<byte[]>();
            ushort? notify_chrhandle, chrhandle;

            // get notify chrhandle (0x29, 0x02)
            notify_chrhandle = dev.GetCharacteristicHandle(serviceIdentifier, "2902");
            if (notify_chrhandle == null)
            {
                promise.Reject(new NotSupportedException("The requested service is not notificable."));
                return;
            }
            // get characteristic chrhandle
            chrhandle = dev.GetCharacteristicHandle(serviceIdentifier, characteristicUUID);
            if (chrhandle == null)
            {
                promise.Reject(new KeyNotFoundException("Service or characteristic not found for this device."));
                return;
            }

            //Byte[] cmdNotify = bglib.BLECommandATTClientAttributeWrite(dev.connectionHandle.Value, notify_chrhandle.Value, new Byte[] { 0x01, 0x00 });
            Byte[] cmdIndicate = bglib.BLECommandATTClientAttributeWrite(dev.connectionHandle.Value, notify_chrhandle.Value, new Byte[] { 0x02, 0x00 });

            Bluegiga.BLE.Events.ATTClient.AttributeValueEventHandler avEventHandler = new Bluegiga.BLE.Events.ATTClient.AttributeValueEventHandler((sender, e) => {
                if (e.connection == dev.connectionHandle && e.atthandle == chrhandle)
                {
                    task.TrySetResult(e.value);
                }
            });

            bglib.BLEEventATTClientAttributeValue += avEventHandler;
            //bglib.SendCommand(serialAPI, cmdNotify);
            bglib.SendCommand(serialAPI, cmdIndicate);

            await task.Task;

            bglib.BLEEventATTClientAttributeValue -= avEventHandler;

            promise.Resolve(task.Task.Result);
        }

But the AttributeValueEvent is never fired. Am I missing something?

jrowberg commented 6 years ago

The code here looks reasonable. Are you certain that the characteristic you're testing with actually supports indications, and not just notifications? For example, a correctly built Health Thermometer service has the right permissions on the temperature characteristic.

faxioman commented 6 years ago

Yes, I'm successfully testing the device with an iOS app. Anyway, the next week I'll have a meeting with the device manufacturer. He wrote a driver like mine using the C bgapi. I'll check my code with him and I'll let you know ;) Thanks!

faxioman commented 6 years ago

Ok, solved ... even if I cannot figure why this change solves the problem:

Byte[] cmdIndicate = bglib.BLECommandATTClientAttributeWrite(dev.connectionHandle.Value, (ushort)(chrhandle.Value + 1), new Byte[] { 0x02, 0x00 });

instead of:

Byte[] cmdIndicate = bglib.BLECommandATTClientAttributeWrite(dev.connectionHandle.Value, notify_chrhandle.Value, new Byte[] { 0x02, 0x00 });
cpseager commented 6 years ago

Just searching for 2902 directly may give you the 'wrong one' if your service has multiple characteristics which each have their own 2902. You need to find the 'next' 2902 that occurs after the desired chrhandle

faxioman commented 6 years ago

Ok, I got it! Thanks!!