private async Task StartSearch() { bool result = await RequestPermissions <LocationAlwaysPermission>(); if (!result) { return; } ToggleIsSearching(true); _searching = _manager.Scan().Subscribe(scanResult => { Device.BeginInvokeOnMainThread(() => { if (!_cache.ContainsKey(scanResult.Peripheral.Uuid)) { _cache.Add(scanResult.Peripheral.Uuid, scanResult.Peripheral); Devices.Add(scanResult.Peripheral); } else { _cache[scanResult.Peripheral.Uuid] = scanResult.Peripheral; var device = Devices.First(d => d.Uuid == scanResult.Peripheral.Uuid); device = scanResult.Peripheral; } }); }); }
/// <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)); }
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()); }
public static IObservable <Beacon> ScanForBeacons(this IBleManager manager, BeaconMonitorConfig?config) { var scanType = config == null ? BleScanType.LowLatency : BleScanType.LowPowered; var cfg = new ScanConfig { ScanType = scanType }; if (config?.ScanServiceUuids?.Any() ?? false) { cfg.ServiceUuids = config.ScanServiceUuids; } return(manager .Scan(cfg) .Where(x => x.IsBeacon()) .Select(x => x.AdvertisementData.ManufacturerData.Data.Parse(x.Rssi))); }
/// <summary> /// Scans only for distinct peripherals instead of repeating each peripheral scan response - this will only give you peripherals, not RSSI or ad packets /// </summary> /// <param name="bleManager"></param> /// <param name="config"></param> /// <returns></returns> public static IObservable <IPeripheral> ScanForUniquePeripherals(this IBleManager bleManager, ScanConfig?config = null) => bleManager .Scan(config) .Distinct(x => x.Peripheral.Uuid) .Select(x => x.Peripheral);
/// <summary> /// This will scan until the peripheral a specific peripheral is found, then cancel the scan /// </summary> /// <param name="bleManager"></param> /// <param name="peripheralName"></param> /// <param name="includeLocalName"></param> /// <returns></returns> public static IObservable <IPeripheral> ScanUntilPeripheralFound(this IBleManager bleManager, string peripheralName, bool includeLocalName = true) => bleManager .Scan() .Where(x => x.Peripheral.Name?.Equals(peripheralName, StringComparison.OrdinalIgnoreCase) ?? false || (includeLocalName && (x.AdvertisementData?.LocalName?.Equals(peripheralName, StringComparison.InvariantCultureIgnoreCase) ?? false)) ) .Take(1) .Select(x => x.Peripheral);
/// <summary> /// This will scan until the peripheral a specific peripheral is found, then cancel the scan /// </summary> /// <param name="bleManager"></param> /// <param name="peripheralUuid"></param> /// <returns></returns> public static IObservable <IPeripheral> ScanUntilPeripheralFound(this IBleManager bleManager, Guid peripheralUuid) => bleManager .Scan() .Where(x => x.Peripheral.Uuid.Equals(peripheralUuid)) .Take(1) .Select(x => x.Peripheral);
public static IObservable <Beacon> ScanForBeacons(this IBleManager manager, bool forMonitoring = false) => manager .Scan(new ScanConfig { //AndroidUseScanBatching = true, ScanType = forMonitoring ? BleScanType.LowPowered : BleScanType.Balanced //#if MONOANDROID // , ServiceUuids = new List<string> // { // } //#endif }) .Where(x => x.IsBeacon()) .Select(x => x.AdvertisementData.ManufacturerData.Data.Parse(x.Rssi));
public AdapterViewModel(IBleManager central, INavigationService navigator, IDialogs dialogs) { this.CanControlAdapterState = central.CanControlAdapterState(); this.WhenAnyValue(x => x.SelectedPeripheral) .Skip(1) .Where(x => x != null) .SubOnMainThread(x => navigator.Navigate("Peripheral", ("Peripheral", x.Peripheral))); this.ToggleAdapterState = ReactiveCommand.CreateFromTask( async() => { var poweredOn = central.Status == AccessState.Available; if (!central.TrySetAdapterState(!poweredOn)) { await dialogs.Alert("Cannot change bluetooth adapter state"); } } ); this.ScanToggle = ReactiveCommand.Create( () => { if (this.IsScanning) { this.IsScanning = false; this.scan?.Dispose(); } else { this.Peripherals.Clear(); this.scan = central .Scan() .Buffer(TimeSpan.FromSeconds(1)) .Synchronize() .SubOnMainThread( results => { var list = new List <PeripheralItemViewModel>(); foreach (var result in results) { var peripheral = this.Peripherals.FirstOrDefault(x => x.Equals(result.Peripheral)); if (peripheral == null) { peripheral = list.FirstOrDefault(x => x.Equals(result.Peripheral)); } if (peripheral != null) { peripheral.Update(result); } else { peripheral = new PeripheralItemViewModel(result.Peripheral); peripheral.Update(result); list.Add(peripheral); } } if (list.Any()) { this.Peripherals.AddRange(list); } }, ex => dialogs.Alert(ex.ToString(), "ERROR") ) .DisposeWith(this.DeactivateWith); this.IsScanning = true; } } ); }
/// <summary> /// This will scan until the peripheral a specific peripheral is found, then cancel the scan /// </summary> /// <param name="bleManager"></param> /// <param name="peripheralName"></param> /// <returns></returns> public static IObservable <IPeripheral> ScanUntilPeripheralFound(this IBleManager bleManager, string peripheralName) => bleManager .Scan() .Where(scanResult => { if (scanResult.Peripheral.Name?.Equals(peripheralName, StringComparison.InvariantCultureIgnoreCase) ?? false) { return(true); } if (scanResult.AdvertisementData?.LocalName?.Equals(peripheralName, StringComparison.CurrentCultureIgnoreCase) ?? false) { return(true); } return(false); }) .Take(1) .Select(x => x.Peripheral);