/// <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); } }
/// <summary> /// Executes when a device is updated /// </summary> /// <param name="sender"></param> /// <param name="deviceInfoUpdate"></param> private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { DeviceInformation di = null; bool addNewDI = false; try { // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == deviceWatcher) { ObservableBluetoothLEDevice dev; // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices try { await BluetoothLEDevicesLock.WaitAsync(); dev = BluetoothLEDevices.FirstOrDefault(device => device.DeviceInfo.Id == deviceInfoUpdate.Id); if (dev != null) { // Found a device in the list, updating it await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { dev.Update(deviceInfoUpdate); }); } else { // Need to add this device. Can't do that here as we have the lock addNewDI = true; } } finally { BluetoothLEDevicesLock.Release(); } if (addNewDI == true) { try { await BluetoothLEDevicesLock.WaitAsync(); di = unusedDevices.FirstOrDefault(device => device.Id == deviceInfoUpdate.Id); if (di != null) { // We found this device before. unusedDevices.Remove(di); di.Update(deviceInfoUpdate); } } finally { BluetoothLEDevicesLock.Release(); } if (di != null) { await AddDeviceToList(di); } } } } catch (Exception ex) { Debug.WriteLine("DeviceWatcher_Updated: " + ex.Message); } }