private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { await Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { lock (this) { if (sender == deviceWatcher) { BluetoothLEDeviceDisplay 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) { deviceInfo.Update(deviceInfoUpdate); // If device has been updated with a friendly name it's no longer unknown. if (deviceInfo.Name != String.Empty) { dict.Add(deviceInfo.Name, deviceInfo); DeviceList.Items.Add(new System.Windows.Controls.ListViewItem { Content = deviceInfo.Name }); KnownDevices.Add(new BluetoothLEDeviceDisplay(deviceInfo)); deviceQueue.Enqueue(deviceInfo.Name); UnknownDevices.Remove(deviceInfo); ConnectDevice(); } } Update_DeviceList(); } } })); }
private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { await Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { lock (this) { if (sender == deviceWatcher) { // Find the corresponding DeviceInformation in the collection and remove it. BluetoothLEDeviceDisplay bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id); DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id); if (bleDeviceDisplay != null) { KnownDevices.Remove(bleDeviceDisplay); dict.Remove(bleDeviceDisplay.Name); if (deviceQueue.Count > 0) { while (deviceQueue.Contains(bleDeviceDisplay.Name)) { var ck = deviceQueue.Dequeue(); if (!ck.Equals(bleDeviceDisplay.Name)) { deviceQueue.Enqueue(ck); } } } } if (deviceInfo != null) { UnknownDevices.Remove(deviceInfo); } Update_DeviceList(); } } })); }