Ejemplo n.º 1
0
        /// <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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the new or updated device to the displayed or unused list
        /// </summary>
        /// <param name="deviceInfo">The device to add</param>
        /// <returns>The task being used to add a device to a list</returns>
        private async Task AddDeviceToList(DeviceInformation deviceInfo)
        {
            // Make sure device name isn't blank or already present in the list.
            if (!string.IsNullOrEmpty(deviceInfo?.Name))
            {
                var device      = new ObservableBluetoothLEDevice(deviceInfo);
                var connectable = true;

                if (device.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.Bluetooth.Le.IsConnectable"))
                {
                    connectable = (bool)device.DeviceInfo.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"];
                }

                if (connectable)
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                    {
                        if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1)))
                        {
                            if (!BluetoothLeDevices.Contains(device))
                            {
                                BluetoothLeDevices.Add(device);
                            }

                            _readerWriterLockSlim.ExitWriteLock();
                        }
                    });

                    return;
                }
            }

            if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1)))
            {
                _unusedDevices.Add(deviceInfo);
                _readerWriterLockSlim.ExitWriteLock();
            }
        }