Ejemplo n.º 1
0
        public override IObservable <ScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            this.isScanning = true;
            return(this.context
                   .Scan(config ?? new ScanConfig())
                   .Finally(() => this.isScanning = false));
        }
Ejemplo n.º 2
0
        public override IObservable <ScanResult> Scan(ScanConfig config)
        {
            config = config ?? new ScanConfig();

            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an existing scan");
            }

            //if (config.ScanType == BleScanType.Background && (config.ServiceUuids == null || config.ServiceUuids.Count == 0))
            //    throw new ArgumentException("Background scan type set but not ServiceUUID");

            this.context.Clear();
            return(this.context
                   .Manager
                   .WhenReady()
                   .Select(_ => Observable.Create <ScanResult>(ob =>
            {
                var scan = this.context
                           .ScanResultReceived
                           .AsObservable()
                           .Subscribe(ob.OnNext);

                if (config.ServiceUuids == null || config.ServiceUuids.Count == 0)
                {
                    this.context.Manager.ScanForPeripherals(null, new PeripheralScanningOptions {
                        AllowDuplicatesKey = true
                    });
                }
                else
                {
                    var uuids = config.ServiceUuids.Select(o => o.ToCBUuid()).ToArray();
                    this.context.Manager.ScanForPeripherals(uuids, new PeripheralScanningOptions {
                        AllowDuplicatesKey = true
                    });
                }

                return () =>
                {
                    this.context.Manager.StopScan();
                    scan.Dispose();
                };
            }))
                   .Switch());
        }
Ejemplo n.º 3
0
        IObservable <BluetoothLEAdvertisementReceivedEventArgs> CreateScanner(ScanConfig config)
        => Observable.Create <BluetoothLEAdvertisementReceivedEventArgs>(ob =>
        {
            this.context.Clear();
            config = config ?? new ScanConfig {
                ScanType = BleScanType.Balanced
            };

            var adWatcher = new BluetoothLEAdvertisementWatcher();
            if (config.ServiceUuids != null)
            {
                foreach (var serviceUuid in config.ServiceUuids)
                {
                    adWatcher.AdvertisementFilter.Advertisement.ServiceUuids.Add(serviceUuid);
                }
            }

            switch (config.ScanType)
            {
            case BleScanType.Balanced:
                adWatcher.ScanningMode = BluetoothLEScanningMode.Active;
                break;

            //case BleScanType.Background:
            case BleScanType.LowLatency:
            case BleScanType.LowPowered:
                adWatcher.ScanningMode = BluetoothLEScanningMode.Passive;
                break;
            }
            var handler = new TypedEventHandler <BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementReceivedEventArgs>
                              ((sender, args) => ob.OnNext(args)
                              );

            adWatcher.Received += handler;
            adWatcher.Start();

            return(() =>
            {
                adWatcher.Stop();
                adWatcher.Received -= handler;
            });
        });