/// <summary>
        /// Executes when a device is updated
        /// </summary>
        /// <param name="sender">The device watcher.</param>
        /// <param name="deviceInfoUpdate">The update device information.</param>
        private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            ObservableBluetoothLEDevice device = null;

            device = BluetoothLeDevices.FirstOrDefault(i => i.DeviceInfo.Id == deviceInfoUpdate.Id);

            if (device != null)
            {
                await device.UpdateAsync(deviceInfoUpdate);
            }

            if (device == null)
            {
                if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(2)))
                {
                    var unusedDevice = _unusedDevices.FirstOrDefault(i => i.Id == deviceInfoUpdate.Id);

                    if (unusedDevice != null)
                    {
                        _unusedDevices.Remove(unusedDevice);
                        unusedDevice.Update(deviceInfoUpdate);
                    }

                    _readerWriterLockSlim.ExitWriteLock();

                    // The update to the unknown device means we should move it to the Bluetooth LE Device collection.
                    await AddDeviceToList(unusedDevice);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Executes when a device is updated
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="deviceInfoUpdate"></param>
        private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            DeviceInformation di = null;
            var addNewDI         = false;

            try
            {
                // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                if (sender == _deviceWatcher)
                {
                    BluetoothLEDeviceWrapper dev;

                    // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices
                    lock (_bluetoothLeDevicesLock)
                    {
                        dev =
                            BluetoothLeDevices.FirstOrDefault(
                                device => device.DeviceInfo.Id == deviceInfoUpdate.Id);
                        if (dev != null)
                        {
                            // Found a device in the list, updating it
                            dev.Update(deviceInfoUpdate);
                        }
                        else
                        {
                            // Need to add this device. Can't do that here as we have the lock
                            addNewDI = true;
                        }
                    }

                    if (addNewDI)
                    {
                        lock (_bluetoothLeDevicesLock)
                        {
                            di = _unusedDevices.FirstOrDefault(device => device.Id == deviceInfoUpdate.Id);
                            if (di != null)
                            {
                                // We found this device before.
                                _unusedDevices.Remove(di);
                                di.Update(deviceInfoUpdate);
                            }
                        }

                        if (di != null)
                        {
                            AddDeviceToList(di);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("DeviceWatcher_Updated exception: " + ex.Message);
            }
        }
        /// <summary>
        /// Executes when a device is removed from enumeration
        /// </summary>
        /// <param name="sender">The device watcher.</param>
        /// <param name="deviceInfoUpdate">An update of the device.</param>
        private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1)))
                {
                    var device = BluetoothLeDevices.FirstOrDefault(i => i.DeviceInfo.Id == deviceInfoUpdate.Id);
                    BluetoothLeDevices.Remove(device);

                    var unusedDevice = _unusedDevices.FirstOrDefault(i => i.Id == deviceInfoUpdate.Id);
                    _unusedDevices?.Remove(unusedDevice);

                    _readerWriterLockSlim.ExitWriteLock();
                }
            });
        }
Esempio n. 4
0
        /// <summary>
        /// Executes when a device is removed from enumeration
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="deviceInfoUpdate"></param>
        private 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)
                {
                    BluetoothLEDeviceWrapper dev;

                    // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices
                    lock (_bluetoothLeDevicesLock)
                    {
                        // 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
                            var removed = _BluetoothLeDevices.Remove(dev);
                            _BluetoothLeDevicesRemoved.Add(dev);
                            DevicesChanged = true;
                            Debug.Assert(removed == true, "DeviceWatcher_Removed: Failed to remove device from list");
                        }
                        else
                        {
                            // Did not find in displayed list, let's check the unused list
                            var di = _unusedDevices.FirstOrDefault(device => device.Id == deviceInfoUpdate.Id);

                            if (di != null)
                            {
                                // Found in unused devices, remove it
                                var removed = _unusedDevices.Remove(di);
                                Debug.Assert(removed == true, "DeviceWatcher_Removed: Failed to remove device from unused");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("DeviceWatcher_Removed: " + ex.Message);
            }
        }
        /// <summary>
        /// Executes when a device is removed from enumeration
        /// </summary>
        /// <param name="sender">The device watcher.</param>
        /// <param name="deviceInfoUpdate">An update of the device.</param>
        private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            // Protect against race condition if the task runs after the app stopped the deviceWatcher.
            if (sender == _deviceWatcher)
            {
                await DispatcherQueue.ExecuteOnUIThreadAsync(
                    () =>
                {
                    if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1)))
                    {
                        var device = BluetoothLeDevices.FirstOrDefault(i => i.DeviceInfo.Id == deviceInfoUpdate.Id);
                        BluetoothLeDevices.Remove(device);

                        var unusedDevice = _unusedDevices.FirstOrDefault(i => i.Id == deviceInfoUpdate.Id);
                        _unusedDevices?.Remove(unusedDevice);

                        _readerWriterLockSlim.ExitWriteLock();
                    }
                }, DispatcherQueuePriority.Normal);
            }
        }