public MainPageViewModel() { if (App.Current.Properties.ContainsKey("defaultPrinter")) { string guidName = App.Current.Properties["defaultPrinter"].ToString(); _connectedDisposable = _centralManager.GetConnectedPeripherals().Subscribe(scanResult => { scanResult.ToList().ForEach( item => { if (!string.IsNullOrEmpty(item.Name) && item.Name == guidName) { Peripherals.Add(item); _centralManager.StopScan(); Device.BeginInvokeOnMainThread(async() => { await App.Current.MainPage.Navigation.PushAsync(new PrintPage(item)); SelectedPeripheral = null; }); _scanDisposable?.Dispose(); IsScanning = _centralManager.IsScanning; } }); _connectedDisposable?.Dispose(); }); if (_centralManager.IsScanning) { _centralManager.StopScan(); } if (_centralManager.Status == Shiny.AccessState.Available && !_centralManager.IsScanning) { _scanDisposable = _centralManager.ScanForUniquePeripherals().Subscribe(scanResult => { if (!string.IsNullOrEmpty(scanResult.Name) && !Peripherals.Contains(scanResult)) { Peripherals.Add(scanResult); Device.BeginInvokeOnMainThread(async() => { await App.Current.MainPage.Navigation.PushAsync(new PrintPage(scanResult)); SelectedPeripheral = null; }); } }); } } else { GetDeviceListCommand = new Command(GetDeviceList); SetAdapterCommand = new Command(async() => await SetAdapter()); CheckPermissionsCommand = new Command(async() => await CheckPermissions()); CheckPermissionsCommand.Execute(null); } }
public CentralExtensionsViewModel(IBleManager centralManager, IDialogs dialogs) { this.Tasks = new List <TaskViewModel> { new TaskViewModel( "Scan Find Peripheral", ct => centralManager .ScanUntilPeripheralFound(this.PeripheralName) .ToTask(ct), this.WhenAny( x => x.PeripheralName, x => !x.GetValue().IsEmpty() ) ), new TaskViewModel( "Scan For Unique Peripherals", ct => centralManager .ScanForUniquePeripherals() .ToTask(ct) ), new TaskViewModel( "Scan Interval", ct => centralManager .ScanInterval( TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10) ) .ToTask(ct) ) }; }
//public override void OnAppearing() //{ // this.bleManager // .Scan() // .Subscribe(x => // { // }) // .DisposeWith(this.DeactivateWith); //} //public override void OnDisappearing() //{ // Console.WriteLine("ONDISAPPEARING CALLED"); //} async Task PairingTest() { this.Append("Scanning.."); this.peripheral = await bleManager .ScanForUniquePeripherals(new ScanConfig { ServiceUuids = { "FFF0" } }) .Take(1) .ToTask(this.cancelSrc.Token); this.Append("Device Found"); //await this.peripheral.WithConnectIf().ToTask(this.cancelSrc.Token); this.Append("Device Connected - trying to pair"); var result = await this.peripheral.TryPairingRequest().ToTask(this.cancelSrc.Token); if (result == null) { this.Append("Pairing Not Supported"); } else { this.Append("Pairing Result: " + result.Value); } }
void RestartScan() { Debug.WriteLine("Scanning restarted"); Devices.Clear(); ble.StopScan(); ble.ScanForUniquePeripherals().Subscribe(async device => { // TODO: Actually look at advertising data. if (device.Name == "Passing Link") { Debug.WriteLine($"Found device: {device.Uuid}"); Devices.Add(new DeviceModel { Name = device.Name, Id = device.Uuid, Handle = device }); } }); }