Esempio n. 1
0
        /// <summary>
        /// Sets up the notifications;
        /// Will call Status
        /// </summary>
        /// <param name="notifyType"></param>
        /// <returns>true if the notify was set up. </returns>

        public async Task <bool> NotifyBatteryLevelAsync(GattClientCharacteristicConfigurationDescriptorValue notifyType = GattClientCharacteristicConfigurationDescriptorValue.Notify)
        {
            if (!await EnsureCharacteristicAsync())
            {
                return(false);
            }
            var ch = Characteristics[15];

            if (ch == null)
            {
                return(false);
            }
            GattCommunicationStatus result = GattCommunicationStatus.ProtocolError;

            try
            {
                result = await ch.WriteClientCharacteristicConfigurationDescriptorAsync(notifyType);

                if (!NotifyBatteryLevel_ValueChanged_Set)
                {
                    // Only set the event callback once
                    NotifyBatteryLevel_ValueChanged_Set = true;
                    ch.ValueChanged += (sender, args) =>
                    {
                        var datameaning = "I8|DEC|BatteryLevel|%";
                        var parseResult = BluetoothDeviceController.BleEditor.ValueParser.Parse(args.CharacteristicValue, datameaning);

                        BatteryLevel = parseResult.ValueList.GetValue("BatteryLevel").AsDouble;

                        BatteryLevelEvent?.Invoke(parseResult);
                    };
                }
            }
            catch (Exception e)
            {
                Status.ReportStatus($"NotifyBatteryLevel: {e.Message}", result);
                return(false);
            }
            Status.ReportStatus($"NotifyBatteryLevel: set notification", result);

            return(true);
        }