Example #1
0
        public override IObservable <CharacteristicGattResult> EnableNotifications(bool useIndicationsIfAvailable)
        => this.context.Invoke(Observable.FromAsync(async ct =>
        {
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);
            if (descriptor == null)
            {
                throw new BleException("Characteristic Client Configuration Descriptor not found");
            }

            var wrap = new GattDescriptor(this, this.context, descriptor);
            if (!this.context.Gatt.SetCharacteristicNotification(this.native, true))
            {
                throw new BleException("Failed to set characteristic notification value");
            }

            await this.context.OpPause(ct).ConfigureAwait(false);
            var bytes = useIndicationsIfAvailable && this.CanIndicate()
                ? BluetoothGattDescriptor.EnableIndicationValue.ToArray()
                : BluetoothGattDescriptor.EnableNotificationValue.ToArray();

            await wrap
            .WriteInternal(bytes)
            .ToTask(ct)
            .ConfigureAwait(false);

            this.IsNotifying = true;
            return(new CharacteristicGattResult(this, null));
        }));
        // this should not be placed in a lock - let it fall to the descriptor
        public override IObservable <object> DisableNotifications() => Observable.FromAsync <object>(async ct =>
        {
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);
            if (descriptor == null)
            {
                throw new ArgumentException("Characteristic Client Configuration Descriptor not found");
            }

            var wrap    = new GattDescriptor(this, this.context, descriptor);
            var success = this.context
                          .Gatt
                          .SetCharacteristicNotification(this.native, false);

            if (CrossBleAdapter.AndroidOperationPause != null)
            {
                await Task.Delay(CrossBleAdapter.AndroidOperationPause.Value, ct);
            }

            if (success)
            {
                await wrap.Write(BluetoothGattDescriptor.DisableNotificationValue.ToArray());
                this.IsNotifying = false;
            }
            return(null);
        });
        public override IObservable <bool> SetNotificationValue(CharacteristicConfigDescriptorValue value)
        => Observable.FromAsync(async ct =>
        {
            var enable     = value != CharacteristicConfigDescriptorValue.None;
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);
            if (descriptor == null)
            {
                throw new ArgumentException("Characteristic Client Configuration Descriptor not found");
            }

            var wrap    = new GattDescriptor(this, this.context, descriptor);
            var success = this.context.Gatt.SetCharacteristicNotification(this.native, enable);
            await Task.Delay(250, ct);

            if (success)
            {
                await wrap.Write(GetDescriptionConfigBytes(value));
                this.IsNotifying = enable;
            }
            return(success);
        });
Example #4
0
        public override IObservable <CharacteristicGattResult> DisableNotifications()
        => this.context.Invoke(Observable.FromAsync(async ct =>
        {
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);
            if (descriptor == null)
            {
                throw new BleException("Characteristic Client Configuration Descriptor not found");
            }

            var wrap = new GattDescriptor(this, this.context, descriptor);
            if (!this.context.Gatt.SetCharacteristicNotification(this.native, false))
            {
                throw new BleException("Could not set characteristic notification value");
            }

            await this.context.OpPause(ct).ConfigureAwait(false);
            await wrap
            .WriteInternal(BluetoothGattDescriptor.DisableNotificationValue.ToArray())
            .ToTask(ct)
            .ConfigureAwait(false);

            this.IsNotifying = false;
            return(new CharacteristicGattResult(this, null));
        }));
        public override IObservable <bool> EnableNotifications(bool useIndicationsIfAvailable)
        => Observable.FromAsync(async ct =>
        {
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);
            if (descriptor == null)
            {
                throw new ArgumentException("Characteristic Client Configuration Descriptor not found");
            }

            var wrap    = new GattDescriptor(this, this.context, descriptor);
            var success = this.context.Gatt.SetCharacteristicNotification(this.native, true);
            await Task.Delay(250, ct);

            if (success)
            {
                var bytes = useIndicationsIfAvailable && this.CanIndicate()
                        ? BluetoothGattDescriptor.EnableIndicationValue.ToArray()
                        : BluetoothGattDescriptor.EnableNotificationValue.ToArray();

                await wrap.Write(bytes);
                this.IsNotifying = true;
            }
            return(success);
        });