Пример #1
0
        public override IObservable <IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(ob =>
            {
                Debug.WriteLine("Hooked for services for device " + this.Uuid);
                var services = new Dictionary <Guid, IGattService>();

                var handler = new EventHandler <NSErrorEventArgs>((sender, args) =>
                {
                    if (this.peripheral.Services == null)
                    {
                        return;
                    }

                    foreach (var native in this.peripheral.Services)
                    {
                        var service = new GattService(this, native);
                        if (!services.ContainsKey(service.Uuid))
                        {
                            services.Add(service.Uuid, service);
                            ob.OnNext(service);
                        }
                    }
                });
                this.peripheral.DiscoveredService += handler;

                var sub = this.WhenStatusChanged()
                          .Where(x => x == ConnectionStatus.Connected)
                          .Subscribe(_ =>
                {
                    this.peripheral.DiscoverServices();
                    Debug.WriteLine("DiscoverServices for device " + this.Uuid);
                });

                return(() =>
                {
                    sub.Dispose();
                    this.peripheral.DiscoveredService -= handler;
                });
            })
                             .ReplayWithReset(this
                                              .WhenStatusChanged()
                                              .Skip(1)
                                              .Where(x => x == ConnectionStatus.Disconnected)
                                              )
                             .RefCount();

            return(this.serviceOb);
        }
Пример #2
0
        public override IObservable<IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create<IGattService>(ob =>
            {
                var handler = new EventHandler<GattEventArgs>((sender, args) =>
                {
                    if (args.Gatt.Device.Equals(this.native))
                    {
                        foreach (var ns in args.Gatt.Services)
                        {
                            var service = new GattService(this, this.context, ns);
                            ob.OnNext(service);
                        }
                    }
                });
                this.callbacks.ServicesDiscovered += handler;
                var sub = this.WhenStatusChanged()
                    .Where(x => x == ConnectionStatus.Connected)
                    .Subscribe(_ =>
                    {
                        Thread.Sleep(1000); // this helps alleviate gatt 133 error
                        this.context.Gatt.DiscoverServices();
                    });

                return () =>
                {
                    sub.Dispose();
                    this.callbacks.ServicesDiscovered -= handler;
                };
            })
            .ReplayWithReset(this
                .WhenStatusChanged()
                .Skip(1)
                .Where(x => x == ConnectionStatus.Disconnected)
            )
            .RefCount();

            return this.serviceOb;
        }
Пример #3
0
        public IObservable <IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(ob =>
                                                                                this
                                                                                .WhenStatusChanged()
                                                                                .Where(x => x == ConnectionStatus.Connected)
                                                                                .Subscribe(x =>
            {
                foreach (var nservice in this.native.GattServices)
                {
                    var service = new GattService(nservice, this);
                    ob.OnNext(service);
                }
            })
                                                                                )
                             .ReplayWithReset(this.WhenStatusChanged()
                                              .Skip(1)
                                              .Where(x => x == ConnectionStatus.Disconnected)
                                              )
                             .RefCount();

            return(this.serviceOb);
        }