public async Task <IReadOnlyList <TDevice> > Browse() { var devices = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected), requestedProperties); return(devices.Select(d => new TDevice(HIOStaticValues.tmain?.SettingManager, d.Name, id: d.Id, mac: GetMac(d), signalValue: BLEDevice.GetSignal(d.Properties), isConnected: d.Properties.ContainsKey(BLEDevice.IsConnectableProperty) ? (bool)d.Properties[BLEDevice.IsConnectedProperty] : false)).ToList()); }
public void StartScan(ICollection <TDevice> devices, Action onChangeCallback = null) { devices.Clear(); if (_Watcher == null) { Radio radio = null; Task.Run(async() => { radio = (await Radio.GetRadiosAsync()).FirstOrDefault(r => r.Kind == RadioKind.Bluetooth); if (radio != null) { radio.StateChanged += (r, args) => { if (radio.State == RadioState.Off) { App.Current.Dispatcher.Invoke(() => devices.Clear()); } } } ; }); _Watcher = DeviceInformation.CreateWatcher(BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected), requestedProperties); _Watcher.Added += async(s, e) => { await semaphoreSlim.WaitAsync(); try { var di = await BluetoothLEDevice.FromIdAsync(e.Id); if (di != null && await IsConnected(di) && await IsHIO(di) && !devices.Any(i => i.Id == e.Id)) { App.Current.Dispatcher.Invoke(() => devices.Add(new TDevice(HIOStaticValues.tmain?.SettingManager, e.Name, id: e.Id, mac: GetMac(e), signalValue: BLEDevice.GetSignal(e.Properties), isConnected: e.Properties.ContainsKey(BLEDevice.IsConnectableProperty) ? (bool)e.Properties[BLEDevice.IsConnectedProperty] : false))); onChangeCallback?.Invoke(); } } catch (Exception ex) { _errorHandler.ErrorFunc(ex); } finally { semaphoreSlim.Release(); } }; _Watcher.Updated += async(s, e) => { await semaphoreSlim.WaitAsync(); try { if (!devices.Any(i => i.Id == e.Id)) { var di = await BluetoothLEDevice.FromIdAsync(e.Id); if (await IsConnected(di) && await IsHIO(di)) { App.Current.Dispatcher.Invoke(() => devices.Add(new TDevice(HIOStaticValues.tmain?.SettingManager, di.Name, id: di.DeviceId, signalValue: BLEDevice.GetSignal(e.Properties), mac: GetMac(di)))); onChangeCallback?.Invoke(); } else { } } } catch (Exception ex) { _errorHandler.ErrorFunc(ex); } finally { semaphoreSlim.Release(); } }; _Watcher.Removed += (s, e) => { semaphoreSlim.WaitAsync(); try { App.Current.Dispatcher.Invoke(() => devices.Remove(devices.FirstOrDefault(i => i.Id == e.Id))); onChangeCallback?.Invoke(); } catch (Exception ex) { _errorHandler.ErrorFunc(ex); } finally { semaphoreSlim.Release(); } }; _Watcher.Start(); } else if (_Watcher.Status == DeviceWatcherStatus.Created || _Watcher.Status == DeviceWatcherStatus.Stopped || _Watcher.Status == DeviceWatcherStatus.EnumerationCompleted || _Watcher.Status == DeviceWatcherStatus.Aborted) { if (_Watcher.Status == DeviceWatcherStatus.EnumerationCompleted) { _Watcher.Stop(); _Watcher = null; StartScan(devices, onChangeCallback); return; } _Watcher.Start(); } }