예제 #1
0
        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);
        }
예제 #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));
        }));
예제 #3
0
        // 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);
        });
예제 #4
0
        // 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);
        });
예제 #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);
 });
예제 #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;
        }
예제 #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);
        }
예제 #8
0
 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);
 }
예제 #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);
        }
예제 #10
0
        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);
        });
예제 #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));
        }));