void myScan() { this.Devices.Clear(); this.IsScanning = true; this.scan = this .adapter .Scan() .Buffer(TimeSpan.FromSeconds(1)) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe( results => { var list = new List <ScanResultViewModel>(); foreach (var result in results) { var dev = this.Devices.FirstOrDefault(x => x.Uuid.Equals(result.Device.Uuid)); if (dev != null) { dev.TrySet(result); } else { dev = new ScanResultViewModel(); dev.TrySet(result); if (dev.Name.ToUpper().Equals("MLT-BT05")) { list.Add(dev); break; } } } if (list.Any()) { this.Devices.AddRange(list); } }, ex => UserDialogs.Instance.Alert(ex.ToString()) ) .DisposeWith(this.DeactivateWith); }
public ScanViewModel(INavigationService navigationService, IUserDialogs dialogs) { this.SelectDevice = ReactiveCommand.CreateFromTask <ScanResultViewModel>( x => navigationService.NavToDevice(x.Device) ); this.OpenSettings = ReactiveCommand.Create(() => { if (this.adapter.Features.HasFlag(AdapterFeatures.OpenSettings)) { this.adapter.OpenSettings(); } else { dialogs.Alert("Cannot open bluetooth settings"); } }); this.ToggleAdapterState = ReactiveCommand.Create( () => { if (this.adapter.CanControlAdapterState()) { var poweredOn = this.adapter.Status == AdapterStatus.PoweredOn; this.adapter.SetAdapterState(!poweredOn); } else { dialogs.Alert("Cannot change bluetooth adapter state"); } } ); this.ScanToggle = ReactiveCommand.Create( () => { if (this.IsScanning) { this.scan?.Dispose(); this.IsScanning = false; } else { this.Devices.Clear(); this.IsScanning = true; this.scan = this .adapter .Scan() .Buffer(TimeSpan.FromSeconds(1)) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe( results => { var list = new List <ScanResultViewModel>(); foreach (var result in results) { var dev = this.Devices.FirstOrDefault(x => x.Uuid.Equals(result.Device.Uuid)); if (dev != null) { dev.TrySet(result); } else { dev = new ScanResultViewModel(); dev.TrySet(result); list.Add(dev); } } if (list.Any()) { this.Devices.AddRange(list); } }, ex => dialogs.Alert(ex.ToString(), "ERROR") ) .DisposeWith(this.DeactivateWith); } } ); }