private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { await DeviceListPage.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (this) { // Debug.WriteLine(String.Format("Updated {0}{1}", deviceInfoUpdate.Id, "")); // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == deviceWatcher) { BLEDeviceInfo bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id); if (bleDeviceDisplay != null) { // Device is already being displayed - update UX. bleDeviceDisplay.Update(deviceInfoUpdate); return; } DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id); if (deviceInfo != null) { // If device has been updated with a friendly name it's no longer unknown. if (deviceInfo.Name != String.Empty) { NotifyStatusMessage?.Invoke("device found:" + deviceInfo.Name); deviceInfo.Update(deviceInfoUpdate); BLEDevices.Add(new BLEDeviceInfo(deviceInfo)); BLEUnknownDevices.Remove(deviceInfo); } } } } }); }
private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { await DeviceListPage.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (this) { // Debug.WriteLine(String.Format("Updated {0}{1}", deviceInfoUpdate.Id, "")); // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == deviceWatcher) { BLEDeviceInfo bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id); if (bleDeviceDisplay != null) { // Device is already being displayed - update UX. BLEDevices.Remove(bleDeviceDisplay); NotifyStatusMessage?.Invoke(bleDeviceDisplay.Name + " Removed."); return; } DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id); if (deviceInfo != null) { BLEUnknownDevices.Remove(deviceInfo); NotifyStatusMessage?.Invoke(deviceInfo.Id + " Removed."); } } } }); }