Exemple #1
0
        public override IObservable <IGattDescriptor> DiscoverDescriptors()
        {
            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);
        }
Exemple #2
0
        public override IObservable <IGattCharacteristic> EnableNotifications(bool enable, bool useIndicationsIfAvailable) => this.context.Invoke(Observable.Create <IGattCharacteristic>(ob =>
        {
            if (!this.context.Gatt.SetCharacteristicNotification(this.native, enable))
            {
                throw new BleException("Failed to set characteristic notification value");
            }

            IDisposable?sub = null;
            var descriptor  = this.native.GetDescriptor(NotifyDescriptorId);
            if (descriptor == null)
            {
                throw new ArgumentException("Notification descriptor not found");
            }

            var wrap  = new GattDescriptor(this, this.context, descriptor);
            var bytes = enable
                ? this.GetNotifyDescriptorBytes(useIndicationsIfAvailable)
                : BluetoothGattDescriptor.DisableNotificationValue.ToArray();

            sub = wrap
                  .WriteInternal(bytes)
                  .Subscribe(
                _ =>
            {
                this.IsNotifying = enable;
                ob.Respond(this);
            },
                ob.OnError
                );
            return(() => sub?.Dispose());
        }));
Exemple #3
0
 public override IObservable <IGattDescriptor> DiscoverDescriptors()
 => 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();
Exemple #4
0
 public override IObservable <IGattDescriptor> DiscoverDescriptors()
 {
     this.descriptorOb = this.descriptorOb ?? Observable.Create <IGattDescriptor>(async ob =>
     {
         var result = await this.Native.GetDescriptorsAsync(BluetoothCacheMode.Uncached);
         //if (result.Status != GattCommunicationStatus.Success)
         foreach (var dnative in result.Descriptors)
         {
             var descriptor = new GattDescriptor(dnative, this);
             ob.OnNext(descriptor);
         }
         return(Disposable.Empty);
     })
                         .Replay();
     return(this.descriptorOb);
 }