示例#1
0
        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);
            }
        }
 /// <summary>
 /// This method wraps the traditional scan, but waits for the bleManager to be ready before initiating scan
 /// </summary>
 /// <param name="bleManager">The bleManager to scan with</param>
 /// <param name="restart">Stops any current scan running</param>
 /// <param name="config">ScanConfig parameters you would like to use</param>
 /// <returns></returns>
 public static IObservable <ScanResult> Scan(this IBleManager bleManager, ScanConfig?config = null, bool restart = false)
 {
     if (restart && bleManager.IsScanning)
     {
         bleManager.StopScan(); // need a pause to wait for scan to end
     }
     return(bleManager.Scan(config));
 }
示例#3
0
 public ScanViewModel(IBleManager bleManager)
 {
     this.Start = ReactiveCommand.CreateFromTask(async() =>
     {
         bleManager
         //.ScanForUniquePeripherals() // this gives you the peripheral, not the scan results
         .Scan()
         .SubOnMainThread(result =>
                          // scan results duplicate per device as the RSSI and device name is read/changed
                          this.Results.Add(result)
                          );
     });
     this.Stop = ReactiveCommand.Create(() => bleManager.StopScan());
 }
示例#4
0
 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
             });
         }
     });
 }