/// <summary> /// Executes when a device is removed from enumeration /// </summary> /// <param name="sender"></param> /// <param name="deviceInfoUpdate"></param> private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { try { // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == deviceWatcher) { ObservableBluetoothLEDevice dev; try { // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices await BluetoothLEDevicesLock.WaitAsync(); // Find the corresponding DeviceInformation in the collection and remove it. dev = BluetoothLEDevices.FirstOrDefault(device => device.DeviceInfo.Id == deviceInfoUpdate.Id); if (dev != null) { // Found it in our displayed devices await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Debug.Assert(BluetoothLEDevices.Remove(dev), "DeviceWatcher_Removed: Failed to remove device from list"); }); } else { // Did not find in diplayed list, let's check the unused list DeviceInformation di = unusedDevices.FirstOrDefault(device => device.Id == deviceInfoUpdate.Id); if (di != null) { // Found in unused devices, remove it Debug.Assert(unusedDevices.Remove(di), "DeviceWatcher_Removed: Failed to remove device from unused"); } } } finally { BluetoothLEDevicesLock.Release(); } } } catch (Exception ex) { Debug.WriteLine("DeviceWatcher_Removed: " + ex.Message); } }
/// <summary> /// Executes when a device is removed from enumeration /// </summary> /// <param name="sender"></param> /// <param name="deviceInfoUpdate"></param> private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { try { // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == deviceWatcher) { ObservableBluetoothLEDevice dev; try { // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices await BluetoothLEDevicesLock.WaitAsync(); // Find the corresponding DeviceInformation in the collection and remove it. dev = BluetoothLEDevices.FirstOrDefault(device => device.DeviceInfo.Id == deviceInfoUpdate.Id); if (dev != null) { // Found it in our displayed devices OnDeviceEnumEvent(new BLTEEnumEventArgs() { Notify = BLTEEnumEvent.Remove, Device = dev }); Debug.Assert(BluetoothLEDevices.Remove(dev), "DeviceWatcher_Removed: Failed to remove device from list"); } else { // Did not find in diplayed list, let's check the unused list DeviceInformation di = unusedDevices.FirstOrDefault(device => device.Id == deviceInfoUpdate.Id); if (di != null) { // Found in unused devices, remove it Debug.Assert(unusedDevices.Remove(di), "DeviceWatcher_Removed: Failed to remove device from unused"); } } } finally { BluetoothLEDevicesLock.Release(); } } } catch (Exception ex) { Debug.WriteLine("DeviceWatcher_Removed: " + ex.Message); } }