示例#1
0
        protected virtual IObservable <IScanResult> DoScan(ScanConfig config) => Observable.Create <IScanResult>(ob =>
        {
            this.context.Clear();

            return(this.context
                   .CreateAdvertisementWatcher(config)
                   .Subscribe(async args => // CAREFUL
            {
                var device = this.context.GetDevice(args.BluetoothAddress);
                if (device == null)
                {
                    var btDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                    if (btDevice != null)
                    {
                        device = this.context.AddDevice(args.BluetoothAddress, btDevice);
                    }
                }
                if (device != null)
                {
                    var adData = new AdvertisementData(args);
                    var scanResult = new ScanResult(device, args.RawSignalStrengthInDBm, adData);
                    ob.OnNext(scanResult);
                }
            }));
        });
示例#2
0
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            return(Observable.Create <IScanResult>(ob =>
            {
                this.IsScanning = true;
                this.context.Clear();

                var sub = this
                          .WhenRadioReady()
                          .Where(rdo => rdo != null)
                          .Select(_ => this.CreateScanner(config))
                          .Switch()
                          .Subscribe(
                    async args =>     // CAREFUL
                {
                    var device = this.context.GetDevice(args.BluetoothAddress);
                    if (device == null)
                    {
                        var btDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                        if (btDevice != null)
                        {
                            device = this.context.AddOrGetDevice(btDevice);
                        }
                    }
                    if (device != null)
                    {
                        var adData = new AdvertisementData(args);
                        var scanResult = new ScanResult(device, args.RawSignalStrengthInDBm, adData);
                        ob.OnNext(scanResult);
                    }
                },
                    ob.OnError
                    );

                var stopSub = this.scanSubject.Subscribe(_ =>
                {
                    this.IsScanning = false;
                    sub?.Dispose();
                    ob.OnCompleted();
                });

                return () =>
                {
                    this.IsScanning = false;
                    sub?.Dispose();
                    stopSub?.Dispose();
                };
            }));
        }
示例#3
0
        public IObservable <IScanResult> ScanListen()
        {
            IDisposable adWatcher  = null;
            IDisposable devWatcher = null;

            this.scanListenOb = this.scanListenOb ?? Observable.Create <IScanResult>(ob =>
                                                                                     this.WhenScanningStatusChanged().Subscribe(scan =>
            {
                if (!scan)
                {
                    adWatcher?.Dispose();
                    devWatcher?.Dispose();
                }
                else
                {
                    this.context.Clear();

                    adWatcher = this.context
                                .CreateAdvertisementWatcher()
                                .Subscribe(args =>
                    {
                        var device = this.context.GetDevice(args.BluetoothAddress);
                        if (device == null)
                        {
                            Debug.WriteLine("Device not found yet - " + args.BluetoothAddress);
                            // causes Element Not Found exception
                            //var native = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                            //device = this.context.GetDevice(native);
                            return;
                        }
                        var adData     = new AdvertisementData(args);
                        var scanResult = new ScanResult(device, args.RawSignalStrengthInDBm, adData);
                        ob.OnNext(scanResult);
                    });

                    devWatcher = this.context
                                 .CreateDeviceWatcher()
                                 .Subscribe(async args =>
                    {
                        Debug.WriteLine($"[DeviceInfo] Info: {args.Id} / {args.Name}");
                        var native = await BluetoothLEDevice.FromIdAsync(args.Id);

                        Debug.WriteLine($"[DeviceInfo] BLE Device: {native.BluetoothAddress} / {native.DeviceId} / {native.Name}");
                        this.context.GetDevice(native);         // set discovered device for adscanner to see
                    });
                }
            })
                                                                                     )
                                .Publish()
                                .RefCount();

            return(this.scanListenOb);
        }