Exemplo n.º 1
0
        protected async Task <bool> SetupCharacteristicNotifyAsync(Guid characteristicUUID, bool enable, TypedEventHandler <GattCharacteristic, GattValueChangedEventArgs> callback)
        {
            if (Service == null)
            {
                await ConnectServiceAsync();
            }

            if (Service != null)
            {
                GattCharacteristic characteristic = Service.GetCharacteristics(characteristicUUID).FirstOrDefault();
                if (characteristic != null)
                {
                    GattClientCharacteristicConfigurationDescriptorValue value = enable ? GattClientCharacteristicConfigurationDescriptorValue.Notify : GattClientCharacteristicConfigurationDescriptorValue.None;

                    GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(value);

                    if (status == GattCommunicationStatus.Success)
                    {
                        if (enable)
                        {
                            characteristic.ValueChanged += callback;
                        }
                        else
                        {
                            characteristic.ValueChanged -= callback;
                        }

                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        private void Subscribe(GattClientCharacteristicConfigurationDescriptorValue value)
        {
            var    t      = Task.Run(async() => await readCh.WriteClientCharacteristicConfigurationDescriptorAsync(value));
            string result = WaitTask(t, 2) == t?t.Result.ToString() : null;

            subscription = "CCCD subscription result: " + (result ?? "timed out");
        }
Exemplo n.º 3
0
        private async Task <bool> TurnOnNotification(Guid Characteristic, TypedEventHandler <GattCharacteristic, GattValueChangedEventArgs> Callback)
        {
            Debug.WriteLine("[miband] activando notificacion");
            var _characteristic = getCharacteristic(Characteristic);

            if (_characteristic != null)
            {
                GattClientCharacteristicConfigurationDescriptorValue type = GattClientCharacteristicConfigurationDescriptorValue.None;
                if (((int)_characteristic.CharacteristicProperties & (int)GattCharacteristicProperties.Notify) != 0)
                {
                    type = GattClientCharacteristicConfigurationDescriptorValue.Notify;
                }
                if (((int)_characteristic.CharacteristicProperties & (int)GattCharacteristicProperties.Indicate) != 0)
                {
                    type = GattClientCharacteristicConfigurationDescriptorValue.Indicate;
                }
                _characteristic.ValueChanged += Callback;
                try
                {
                    var ret = await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(type);

                    if (ret == GattCommunicationStatus.Success)
                    {
                        Debug.WriteLine("[miband] notificacion activada");
                        return(true);
                    }
                }
                catch (Exception)
                {
                }
            }
            Debug.WriteLine("[miband] error al activar notificacion");
            return(false);
        }
Exemplo n.º 4
0
 IObservable <object> SetNotify(GattClientCharacteristicConfigurationDescriptorValue value)
 => Observable.FromAsync(async ct =>
 {
     this.AssertNotify();
     var status = await this.Native.WriteClientCharacteristicConfigurationDescriptorAsync(value);
     if (status == GattCommunicationStatus.Success)
     {
         this.context.SetNotifyCharacteristic(this.Native, value != GattClientCharacteristicConfigurationDescriptorValue.None);
         return(new object());
     }
     return(null);
 });
Exemplo n.º 5
0
    private Task <GattCommunicationStatus> Notify(GattCharacteristic charac, GattClientCharacteristicConfigurationDescriptorValue value)
    {
        var tcs = new TaskCompletionSource <GattCommunicationStatus>();

        Task.Run(async() =>
        {
            var _myres = await charac.WriteClientCharacteristicConfigurationDescriptorAsync(value);
            tcs.SetResult(_myres);
        });
        tcs.Task.Wait();
        return(tcs.Task);
    }
Exemplo n.º 6
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> NotifyQWIICAsync(GattClientCharacteristicConfigurationDescriptorValue notifyType = GattClientCharacteristicConfigurationDescriptorValue.Notify)
        {
            if (!await EnsureCharacteristicAsync())
            {
                return(false);
            }
            var ch = Characteristics[5];

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

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

                if (!NotifyQWIIC_ValueChanged_Set)
                {
                    // Only set the event callback once
                    NotifyQWIIC_ValueChanged_Set = true;
                    ch.ValueChanged += (sender, args) =>
                    {
                        var datameaning = "OEB U8|HEX|Sensor U16|HEX|Channel1 U16|HEX|Channel2 U16|HEX|Channel3 U16|HEX|Channel4 U16|HEX|Channel5";
                        var parseResult = BluetoothDeviceController.BleEditor.ValueParser.Parse(args.CharacteristicValue, datameaning);

                        QWIIC_Sensor = parseResult.ValueList.GetValue("Sensor").AsDouble;

                        QWIIC_Channel1 = parseResult.ValueList.GetValue("Channel1").AsDouble;

                        QWIIC_Channel2 = parseResult.ValueList.GetValue("Channel2").AsDouble;

                        QWIIC_Channel3 = parseResult.ValueList.GetValue("Channel3").AsDouble;

                        QWIIC_Channel4 = parseResult.ValueList.GetValue("Channel4").AsDouble;

                        QWIIC_Channel5 = parseResult.ValueList.GetValue("Channel5").AsDouble;

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

            return(true);
        }
Exemplo n.º 7
0
        IObservable <CharacteristicGattResult> SetNotify(GattClientCharacteristicConfigurationDescriptorValue value)
        => Observable.FromAsync(async ct =>
        {
            var status = await this.Native.WriteClientCharacteristicConfigurationDescriptorAsync(value);
            if (status != GattCommunicationStatus.Success)
            {
                throw new BleException($"Failed to write client characteristic configuration descriptor - {status}");
            }

            this.IsNotifying = value != GattClientCharacteristicConfigurationDescriptorValue.None;
            this.context.SetNotifyCharacteristic(this);
            return(new CharacteristicGattResult(this, null));
        });
Exemplo n.º 8
0
 private async Task WriteConfigurationInChar(KeyValuePair<string, RsCharacteristic> rsCharacteristic, GattClientCharacteristicConfigurationDescriptorValue charConfigValue)
 {
     try
     {
         DevicesInformation = $"  {charConfigValue} {Environment.NewLine}";
         await
            rsCharacteristic.Value.Characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(charConfigValue);
     }
     catch (Exception exception)
     {
         DevicesInformation = $"  Exception {exception}";
     }
 }
Exemplo n.º 9
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> NotifyData_ValuesAsync(GattClientCharacteristicConfigurationDescriptorValue notifyType = GattClientCharacteristicConfigurationDescriptorValue.Notify)
        {
            if (!await EnsureCharacteristicAsync())
            {
                return(false);
            }
            var ch = Characteristics[5];

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

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

                if (!NotifyData_Values_ValueChanged_Set)
                {
                    // Only set the event callback once
                    NotifyData_Values_ValueChanged_Set = true;
                    ch.ValueChanged += (sender, args) =>
                    {
                        var datameaning = "I24|HEX|Command U8|DEC|Range OEB U16|DEC|Value U8|DEC|V3 BYTES|HEX|More_Data";
                        var parseResult = BluetoothDeviceController.BleEditor.ValueParser.Parse(args.CharacteristicValue, datameaning);

                        Data_Values_Command = parseResult.ValueList.GetValue("Command").AsDouble;

                        Data_Values_Range = parseResult.ValueList.GetValue("Range").AsDouble;

                        Data_Values_Value = parseResult.ValueList.GetValue("Value").AsDouble;

                        Data_Values_V3 = parseResult.ValueList.GetValue("V3").AsDouble;

                        Data_Values_More_Data = parseResult.ValueList.GetValue("More_Data").AsString;

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

            return(true);
        }
Exemplo n.º 10
0
 private async Task WriteConfigurationInChar(GattCharacteristic characteristic, GattClientCharacteristicConfigurationDescriptorValue charConfigValue)
 {
     try
     {
         DevicesInformation += $"  {charConfigValue} {Environment.NewLine}";
         await
            characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(charConfigValue);
         DevicesInformation += $"    - Ok{Environment.NewLine}";
     }
     catch (Exception exception)
     {
         DevicesInformation += $"    - Exception {exception.Message}{Environment.NewLine}";
     }
 }
        /// <summary>
        /// Convenience class for writing the config value.
        /// </summary>
        /// <param name="Enable"></param>
        /// <returns></returns>
        public async Task WriteBarometer_ConfigNotify(GattClientCharacteristicConfigurationDescriptorValue Enable)
        {
            switch (Enable)
            {
            case GattClientCharacteristicConfigurationDescriptorValue.Notify:
                await WriteBarometer_Config(1);

                break;

            case GattClientCharacteristicConfigurationDescriptorValue.None:
                await WriteBarometer_Config(0);

                break;
            }
        }
        /// <summary>
        /// Convenience class for writing the config value.
        /// </summary>
        /// <param name="Enable"></param>
        /// <returns></returns>
        ///
        public async Task WriteAccelerometer_ConfigNotify(GattClientCharacteristicConfigurationDescriptorValue Enable)
        {
            switch (Enable)
            {
            case GattClientCharacteristicConfigurationDescriptorValue.Notify:
                await WriteAccelerometer_Config(0xFFFF);     // always just write the same value.

                break;

            case GattClientCharacteristicConfigurationDescriptorValue.None:
                await WriteAccelerometer_Config(0);

                break;
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Convenience class for writing the config value.
        /// </summary>
        /// <param name="Enable"></param>
        /// <returns></returns>
        public async Task WriteGyroscope_ConfigNotify(GattClientCharacteristicConfigurationDescriptorValue Enable)
        {
            switch (Enable)
            {
            case GattClientCharacteristicConfigurationDescriptorValue.Notify:
                await WriteGyroscope_Configure(7);     // always just write the same value to enable all three axis

                break;

            case GattClientCharacteristicConfigurationDescriptorValue.None:
                await WriteGyroscope_Configure(0);

                break;
            }
        }
Exemplo n.º 14
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> NotifyAnalogAsync(GattClientCharacteristicConfigurationDescriptorValue notifyType = GattClientCharacteristicConfigurationDescriptorValue.Notify)
        {
            if (!await EnsureCharacteristicAsync())
            {
                return(false);
            }
            var ch = Characteristics[4];

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

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

                if (!NotifyAnalog_ValueChanged_Set)
                {
                    // Only set the event callback once
                    NotifyAnalog_ValueChanged_Set = true;
                    ch.ValueChanged += (sender, args) =>
                    {
                        var datameaning = "I32^100000_/|FIXED|A1|volts I32^100000_/|FIXED|A2|volts I32^100000_/|FIXED|A3|volts I32^100000_/|FIXED|A4|volts";
                        var parseResult = BluetoothDeviceController.BleEditor.ValueParser.Parse(args.CharacteristicValue, datameaning);

                        Analog_A1 = parseResult.ValueList.GetValue("A1").AsDouble;

                        Analog_A2 = parseResult.ValueList.GetValue("A2").AsDouble;

                        Analog_A3 = parseResult.ValueList.GetValue("A3").AsDouble;

                        Analog_A4 = parseResult.ValueList.GetValue("A4").AsDouble;

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

            return(true);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Subscribes to the given GattCharacteristic if it allows it.
        /// </summary>
        /// <param name="c">The GattCharacteristic you want to subscribe to.</param>
        /// <returns>Returns true on success.</returns>
        private async Task <bool> SubscribeToCharacteristicAsync(GattCharacteristic c)
        {
            // Check if characteristic supports subscriptions:
            GattClientCharacteristicConfigurationDescriptorValue cccdValue = GattClientCharacteristicConfigurationDescriptorValue.None;

            if (c.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Indicate))
            {
                cccdValue = GattClientCharacteristicConfigurationDescriptorValue.Indicate;
            }
            else if (c.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
            {
                cccdValue = GattClientCharacteristicConfigurationDescriptorValue.Notify;
            }
            else
            {
                return(false);
            }

            // Set subscribed:
            GattCommunicationStatus status;

            try
            {
                status = await c.WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue);
            }
            catch (Exception e)
            {
                Logger.Warn("Failed to subscribe to characteristic " + c.Uuid + " with " + e.ToString());
                return(false);
            }

            // Add event handler:
            if (status == GattCommunicationStatus.Success)
            {
                c.ValueChanged -= C_ValueChanged;
                c.ValueChanged += C_ValueChanged;
                Logger.Debug("Subscribed to characteristic: " + c.Uuid);
                return(true);
            }
            else
            {
                Logger.Warn("Failed to subscribe to characteristic " + c.Uuid + " with " + status);
            }
            return(false);
        }
Exemplo n.º 16
0
    public GattCommunicationStatus Notify_Characteristic(GattCharacteristic charac, GattClientCharacteristicConfigurationDescriptorValue value)
    {
        Task nRes = Notify(charac, value).ContinueWith((anteNC) =>
        {
            if (anteNC.Status == TaskStatus.RanToCompletion)
            {
                var CharNotStat      = anteNC.Result;
                charac.ValueChanged += ReceiveData;
                return(CharNotStat);
            }
            else
            {
                return(GattCommunicationStatus.Unreachable);
            }
        });

        return(GattCommunicationStatus.Unreachable);
    }
        /// <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> NotifyMicrophoneAsync(GattClientCharacteristicConfigurationDescriptorValue notifyType = GattClientCharacteristicConfigurationDescriptorValue.Notify)
        {
            if (!await EnsureCharacteristicAsync())
            {
                return(false);
            }
            var ch = Characteristics[9];

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

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

                if (!NotifyMicrophone_ValueChanged_Set)
                {
                    // Only set the event callback once
                    NotifyMicrophone_ValueChanged_Set = true;
                    ch.ValueChanged += (sender, args) =>
                    {
                        var datameaning = "BYTES|HEX|Audio";
                        var parseResult = BluetoothDeviceController.BleEditor.ValueParser.Parse(args.CharacteristicValue, datameaning);

                        Microphone = parseResult.ValueList.GetValue("Audio").AsString;

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

            return(true);
        }
Exemplo n.º 18
0
        public async Task InitChararacteristicsConfiguration()
        {
            ClearExceptions();
            const GattClientCharacteristicConfigurationDescriptorValue charValue =
                GattClientCharacteristicConfigurationDescriptorValue.Indicate;

            try
            {
                await InitCount1To20.WriteClientCharacteristicConfigurationDescriptorAsync(charValue);
            }
            catch (Exception exception)
            {
                AddException(exception);
            }
            try
            {
                await PowerMotors.WriteClientCharacteristicConfigurationDescriptorAsync(charValue);
            }
            catch (Exception exception)
            {
                AddException(exception);
            }
            try
            {
                await DateTime.WriteClientCharacteristicConfigurationDescriptorAsync(charValue);
            }
            catch (Exception exception)
            {
                AddException(exception);
            }
            try
            {
                await EmergencyStop.WriteClientCharacteristicConfigurationDescriptorAsync(charValue);
            }
            catch (Exception exception)
            {
                AddException(exception);
            }
        }
Exemplo n.º 19
0
        private static async void SubscribeToValueChange(GattCharacteristic characteristic)
        {
            string device = characteristic.Service.Device.DeviceInformation.Id.Substring(41);

            GattClientCharacteristicConfigurationDescriptorValue gcccdValue = GattClientCharacteristicConfigurationDescriptorValue.None;

            if ((characteristic.CharacteristicProperties & GattCharacteristicProperties.Indicate) != GattCharacteristicProperties.None)
            {
                gcccdValue = GattClientCharacteristicConfigurationDescriptorValue.Indicate;
            }
            else if ((characteristic.CharacteristicProperties & GattCharacteristicProperties.Notify) != GattCharacteristicProperties.None)
            {
                gcccdValue = GattClientCharacteristicConfigurationDescriptorValue.Notify;
            }
            else
            {
                blePowermates[device].isSubscribing = false;
                Console.WriteLine("Couldn't set Characteristic Configuration Descriptor");
            }

            try
            {
                GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(gcccdValue);

                if (status == GattCommunicationStatus.Success)
                {
                    blePowermates[device].readCharacteristic = characteristic;
                    blePowermates[device].isSubscribing      = false;

                    AddValueChangedHandler(blePowermates[device].readCharacteristic);
                    Console.WriteLine("Successfully subscribed for value changes");
                }
            }catch (Exception ex)
            {
                blePowermates[device].isSubscribing = false;
                Console.WriteLine("Error registering for value changes: Status = ", ex.Message);
            }
        }
Exemplo n.º 20
0
        private async Task WriteConfigurationInChar(GattCharacteristic characteristic, GattClientCharacteristicConfigurationDescriptorValue charConfigValue)
        {
            try
            {
                DevicesInformation += $"  {charConfigValue} {Environment.NewLine}";
                await
                characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(charConfigValue);

                DevicesInformation += $"    - Ok{Environment.NewLine}";
            }
            catch (Exception exception)
            {
                DevicesInformation += $"    - Exception {exception.Message}{Environment.NewLine}";
            }
        }
Exemplo n.º 21
0
 private async Task WriteConfigurationInChar(KeyValuePair <string, RsCharacteristic> rsCharacteristic, GattClientCharacteristicConfigurationDescriptorValue charConfigValue)
 {
     try
     {
         DevicesInformation = $"  {charConfigValue} {Environment.NewLine}";
         await
         rsCharacteristic.Value.Characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(charConfigValue);
     }
     catch (Exception exception)
     {
         DevicesInformation = $"  Exception {exception}";
     }
 }
Exemplo n.º 22
0
 public ClientDescriptorViewModel(GattClientCharacteristicConfigurationDescriptorValue descriptor, string title)
 {
     Descriptor = descriptor;
     Title      = title;
     Info       = "State unknown";
 }
Exemplo n.º 23
0
 public WriteClientConfigDescriptorCommand(CharacteristicInfo characteristicInfo, GattClientCharacteristicConfigurationDescriptorValue descriptor) : base(characteristicInfo)
 {
     Descriptor = descriptor;
 }