예제 #1
0
        public override IObservable <IGattService> GetKnownService(Guid serviceUuid)
        => Observable.Create <IGattService>(ob =>
        {
            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 (service.Uuid.Equals(serviceUuid))
                    {
                        ob.Respond(service);
                        break;
                    }
                }
            });
            this.peripheral.DiscoveredService += handler;
            this.peripheral.DiscoverServices(new[] { serviceUuid.ToCBUuid() });

            return(() => this.peripheral.DiscoveredService -= handler);
        });
예제 #2
0
        public override IObservable <IGattService> GetKnownService(Guid serviceUuid)
        => Observable.Create <IGattService>(ob =>
        {
            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 (service.Uuid.Equals(serviceUuid))
                    {
                        ob.OnNext(service);
                        ob.OnCompleted();
                        break;
                    }
                }
            });
            this.peripheral.DiscoveredService += handler;
            this.WhenStatusChanged()
            .Where(x => x == ConnectionStatus.Connected)
            .Subscribe(x => this.peripheral.DiscoverServices(new[] { serviceUuid.ToCBUuid() }));

            return(() => this.peripheral.DiscoveredService -= handler);
        });
예제 #3
0
        public override IObservable <IGattService> DiscoverServices() => Observable.Create <IGattService>(ob =>
        {
            Log.Info(BleLogCategory.Device, "service discovery hooked 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;
            this.peripheral.DiscoverServices();

            return(() => this.peripheral.DiscoveredService -= handler);
        });
예제 #4
0
        public override IObservable <IGattService> DiscoverServices()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(async ob =>
            {
                //var handler = new TypedEventHandler<BluetoothLEDevice, object>((sender, args) =>
                //{
                //    //if (this.native.Equals(sender))
                //});
                //this.context.NativeDevice.GattServicesChanged += handler;

                var result = await this.context.NativeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);
                foreach (var nservice in result.Services)
                {
                    var service = new GattService(this.context, nservice);
                    ob.OnNext(service);
                }

                //return () => this.context.NativeDevice.GattServicesChanged -= handler;
                return(Disposable.Empty);
            })
                             .ReplayWithReset(this.WhenStatusChanged()
                                              .Skip(1)
                                              .Where(x => x == ConnectionStatus.Disconnected)
                                              )
                             .RefCount();

            return(this.serviceOb);
        }
예제 #5
0
        public override IObservable <IGattService> DiscoverServices()
        => Observable.Create <IGattService>(ob =>
        {
            var sub1 = this.context
                       .Callbacks
                       .ServicesDiscovered
                       .Where(x => x.Gatt.Device.Equals(this.context.NativeDevice))
                       .Subscribe(args =>
            {
                foreach (var ns in args.Gatt.Services)
                {
                    var service = new GattService(this, this.context, ns);
                    ob.OnNext(service);
                }
            });

            var sub2 = this
                       .WhenConnected()

                       // this helps alleviate gatt 133 error
                       .Delay(CrossBleAdapter.PauseBeforeServiceDiscovery)
                       .Subscribe(_ =>
            {
                if (!this.context.Gatt.DiscoverServices())
                {
                    ob.OnError(new Exception("Failed to request services"));
                }
            });

            return(() =>
            {
                sub1.Dispose();
                sub2.Dispose();
            });
        });
예제 #6
0
        public IObservable <IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(ob =>
                                                                                this
                                                                                .WhenStatusChanged()
                                                                                .Where(x => x == ConnectionStatus.Connected)
                                                                                .Subscribe(async x =>
            {
                var result = await this.native.GetGattServicesAsync(BluetoothCacheMode.Uncached);

                foreach (var nservice in result.Services)
                {
                    var service = new GattService(nservice, this);
                    ob.OnNext(service);
                }
            })
                                                                                )
                             .ReplayWithReset(this.WhenStatusChanged()
                                              .Skip(1)
                                              .Where(x => x == ConnectionStatus.Disconnected)
                                              )
                             .RefCount();

            return(this.serviceOb);
        }
예제 #7
0
        public override IObservable <IGattService> GetKnownService(Guid serviceUuid) => Observable.FromAsync(async ct =>
        {
            var result = await this.context.NativeDevice.GetGattServicesForUuidAsync(serviceUuid, BluetoothCacheMode.Cached);
            if (result.Status != GattCommunicationStatus.Success)
            {
                throw new ArgumentException("Could not find GATT service - " + result.Status);
            }

            var wrap = new GattService(this.context, result.Services.First());
            return(wrap);
        });
예제 #8
0
 public override IObservable <IGattService> WhenServiceDiscovered() => Observable.Create <IGattService>(ob =>
 {
     // TODO: refresh per connection
     foreach (var path in this.native.GattServices)
     {
         var service = Bus.System.GetObject <GattService1>(BlueZPath.Service, path);
         var acr     = new GattService(service, this);
         ob.OnNext(acr);
     }
     return(Disposable.Empty);
 });
예제 #9
0
        public override IObservable <IGattService> DiscoverServices() => Observable.Create <IGattService>(async ob =>
        {
            var result = await this.context.NativeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);
            foreach (var nservice in result.Services)
            {
                var service = new GattService(this.context, nservice);
                ob.OnNext(service);
            }
            ob.OnCompleted();

            return(Disposable.Empty);
        });
예제 #10
0
        public override IObservable <IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(ob =>
            {
                Log.Info("Device", "service discovery hooked 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)
                          .Delay(TimeSpan.FromMilliseconds(300))
                          .Subscribe(_ =>
                {
                    this.peripheral.DiscoverServices();
                    Log.Info("Device", "service discovery running 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);
        }
예제 #11
0
        public override IObservable <IGattService> DiscoverServices()
        => Observable.Create <IGattService>(ob =>
        {
            this.context
            .Callbacks
            .ServicesDiscovered
            .Where(x => x.Gatt.Device.Equals(this.context.NativeDevice))
            .Subscribe(args =>
            {
                foreach (var ns in args.Gatt.Services)
                {
                    var service = new GattService(this, this.context, ns);
                    ob.OnNext(service);
                }
                ob.OnCompleted();
            });

            this.context.Gatt.DiscoverServices();

            return(() => { });
        });
예제 #12
0
        // android does not have a find "1" service - it must discover all services.... seems shit
        public override IObservable <IGattService> GetKnownService(Guid serviceUuid)
        {
            var uuid          = serviceUuid.ToUuid();
            var nativeService = context.Gatt.GetService(uuid);

            // If native service is null, it may be because the underlying
            // BT library hasn't yet discovered all the services on the device
            if (nativeService == null)
            {
                return(this
                       .WhenServiceDiscovered()
                       .Where(x => x.Uuid.Equals(serviceUuid))
                       .Take(1)
                       .Select(x => x));
            }
            else
            {
                var service = new GattService(this, context, nativeService);
                return(Observable.Return <IGattService>(service));
            }
        }
예제 #13
0
        public override IObservable <IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(ob =>
            {
                var sub1 = this.context
                           .Callbacks
                           .ServicesDiscovered
                           .Where(x => x.Gatt.Device.Equals(this.context.NativeDevice))
                           .Subscribe(args =>
                {
                    foreach (var ns in args.Gatt.Services)
                    {
                        var service = new GattService(this, this.context, ns);
                        ob.OnNext(service);
                    }
                });

                var sub2 = this
                           .WhenStatusChanged()
                           .Where(x => x == ConnectionStatus.Connected)
                           .Subscribe(_ =>
                {
                    Thread.Sleep(Convert.ToInt32(CrossBleAdapter.AndroidPauseBeforeServiceDiscovery.TotalMilliseconds));     // this helps alleviate gatt 133 error
                    this.context.Gatt.DiscoverServices();
                });

                return(() =>
                {
                    sub1.Dispose();
                    sub2.Dispose();
                });
            })
                             .ReplayWithReset(this
                                              .WhenStatusChanged()
                                              .Where(x => x == ConnectionStatus.Disconnected)
                                              )
                             .RefCount();

            return(this.serviceOb);
        }
예제 #14
0
        public override IObservable <IGattService> WhenServiceDiscovered()
        {
            this.serviceOb = this.serviceOb ?? Observable.Create <IGattService>(ob =>
            {
                var handler = new TypedEventHandler <BluetoothLEDevice, object>((sender, args) =>
                {
                    //if (this.native.Equals(sender))
                });
                this.context.NativeDevice.GattServicesChanged += handler;

                var sub = this
                          .WhenStatusChanged()
                          .Where(x => x == ConnectionStatus.Connected)
                          .Select(_ => Observable.FromAsync(async ct =>
                {
                    var result = await this.context.NativeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

                    foreach (var nservice in result.Services)
                    {
                        var service = new GattService(this.context, nservice);
                        ob.OnNext(service);
                    }
                }))
                          .Merge()
                          .Subscribe();

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

            return(this.serviceOb);
        }
예제 #15
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);
        }
예제 #16
0
 public GattCharacteristic(GattService service, CBCharacteristic native)
     : base(service, native.UUID.ToGuid(), (CharacteristicProperties)(int)native.Properties)
 {
     this.serivceObj           = service;
     this.NativeCharacteristic = native;
 }