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_Added(DeviceWatcher sender, DeviceInformation devinfo) { await DeviceListPage.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (this) { if (!string.IsNullOrEmpty(devinfo.Name)) { NotifyStatusMessage?.Invoke("device found:" + devinfo.Name); BLEDevices.Add(new BLEDeviceInfo(devinfo)); // Debug.WriteLine("Name: {0} Id {1}", devinfo.Name, devinfo.Id); } else { // Add it to a list in case the name gets updated later. BLEUnknownDevices.Add(devinfo); } } }); }