public override IObservable <IGattDescriptor> WhenDescriptorDiscovered()
        {
            this.descriptorOb = this.descriptorOb ?? Observable.Create <IGattDescriptor>(ob =>
            {
                var descriptors = new Dictionary <Guid, IGattDescriptor>();

                var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) =>
                {
                    if (this.NativeCharacteristic.Descriptors == null)
                    {
                        return;
                    }

                    foreach (var dnative in this.NativeCharacteristic.Descriptors)
                    {
                        var wrap = new GattDescriptor(this, dnative);
                        if (!descriptors.ContainsKey(wrap.Uuid))
                        {
                            descriptors.Add(wrap.Uuid, wrap);
                            ob.OnNext(wrap);
                        }
                    }
                });
                this.Peripheral.DiscoveredDescriptor += handler;
                this.Peripheral.DiscoverDescriptors(this.NativeCharacteristic);

                return(() => this.Peripheral.DiscoveredDescriptor -= handler);
            })
                                .Replay()
                                .RefCount();

            return(this.descriptorOb);
        }
Example #2
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 <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);

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

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

                await wrap.Write(bytes);
                this.IsNotifying = true;
            }
            return(success);
        });
        // 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);
        });
Example #5
0
 public override IObservable <IGattDescriptor> WhenDescriptorDiscovered() => Observable.Create <IGattDescriptor>(ob =>
 {
     // TODO: refresh per connection
     foreach (var path in this.native.Descriptors)
     {
         var desc = Bus.System.GetObject <GattDescriptor1>(BlueZPath.Service, path);
         var acr  = new GattDescriptor(desc, this);
         ob.OnNext(acr);
     }
     return(Disposable.Empty);
 });
Example #6
0
        protected virtual async Task DisableNotifications()
        {
            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);
            await wrap.Write(BluetoothGattDescriptor.DisableNotificationValue.ToArray());

            this.context.Gatt.SetCharacteristicNotification(this.native, false);
            this.IsNotifying = false;
        }
Example #7
0
        public override IObservable <IGattDescriptor> DiscoverDescriptors()
        {
            this.descriptorOb = this.descriptorOb ?? Observable.Create <IGattDescriptor>(ob =>
            {
                foreach (var nd in this.native.Descriptors)
                {
                    var wrap = new GattDescriptor(this, this.context, nd);
                    ob.OnNext(wrap);
                }
                return(Disposable.Empty);
            })
                                .Replay()
                                .RefCount();

            return(this.descriptorOb);
        }
 public override IObservable <IGattDescriptor> WhenDescriptorDiscovered()
 {
     this.descriptorOb = this.descriptorOb ?? Observable.Create <IGattDescriptor>(async ob =>
     {
         var result = await this.Native.GetDescriptorsAsync(BluetoothCacheMode.Uncached);
         //if (result.Status)
         foreach (var dnative in result.Descriptors)
         {
             var descriptor = new GattDescriptor(dnative, this);
             ob.OnNext(descriptor);
         }
         return(Disposable.Empty);
     })
                         .Replay();
     return(this.descriptorOb);
 }
Example #9
0
        protected virtual async Task <bool> EnableNotifications()
        {
            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);

            if (success)
            {
                await wrap.Write(BluetoothGattDescriptor.EnableNotificationValue.ToArray());

                this.IsNotifying = true;
            }
            return(success);
        }
        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 #11
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));
        }));