public override IObservable <ScanResult> Scan(ScanConfig?config = null) { if (this.IsScanning) { throw new ArgumentException("There is already an active scan"); } return(Observable.Create <ScanResult>(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.GetPeripheral(args.BluetoothAddress); if (device == null) { var btDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress); if (btDevice != null) { device = this.context.AddOrGetPeripheral(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(); }; })); }
public override IObservable <ScanResult> Scan(ScanConfig?config = null) { if (this.IsScanning) { throw new ArgumentException("There is already an active scan"); } return(this.RequestAccess() .Do(result => { if (result != AccessState.Available) { throw new PermissionException("BluetoothLE", result); } this.IsScanning = true; this.context.Clear(); }) .Select(_ => this.GetRadio()) .Switch() .Select(_ => this.CreateScanner(config)) .Switch() .Select(args => Observable.FromAsync(async ct => { var device = this.context.GetPeripheral(args.BluetoothAddress); if (device == null) { var btDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress); if (btDevice != null) { device = this.context.AddOrGetPeripheral(btDevice); } } ScanResult?scanResult = null; if (device != null) { var adData = new AdvertisementData(args); scanResult = new ScanResult(device, args.RawSignalStrengthInDBm, adData); } return scanResult; })) .Where(x => x != null) .Finally(() => this.IsScanning = false) .Switch() !); }