Ejemplo n.º 1
0
        /// <summary>
        /// Adds the new or updated device to the displayed or unused list
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <returns></returns>
        private void AddDeviceToList(DeviceInformation deviceInfo)
        {
            // Make sure device name isn't blank or already present in the list.
            if (deviceInfo.Name != string.Empty)
            {
                BluetoothLEDeviceWrapper dev = new BluetoothLEDeviceWrapper(deviceInfo);

                // Let's make it connectible by default, we have error handles in case it doesn't work
                var connectable = true;

                // If the connectible key exists then let's read it
                if (dev.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.Bluetooth.Le.IsConnectable") == true)
                {
                    connectable = (bool)dev.DeviceInfo.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"];
                }

                if (connectable)
                {
                    // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices
                    lock (_bluetoothLeDevicesLock)
                    {
                        if (BluetoothLeDevices.Contains(dev) == false)
                        {
                            _BluetoothLeDevices.Add(dev);
                            _BluetoothLeDevicesAdded.Add(dev);
                            DevicesChanged = true;
                        }
                    }
                }
                else
                {
                    lock (_bluetoothLeDevicesLock)
                    {
                        // Found but not adding because it's not connectable
                        _unusedDevices.Add(deviceInfo);
                    }
                }
            }
            else
            {
                lock (_bluetoothLeDevicesLock)
                {
                    //Found device without a name
                    _unusedDevices.Add(deviceInfo);
                }
            }
        }
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();
            }
        }
        /// <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, DispatcherQueue);
                var connectable = (device.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.Bluetooth.Le.IsConnectable") &&
                                   (bool)device.DeviceInfo.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"]) ||
                                  (device.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.IsConnected") &&
                                   (bool)device.DeviceInfo.Properties["System.Devices.Aep.IsConnected"]);

                if (connectable)
                {
                    await DispatcherQueue.ExecuteOnUIThreadAsync(
                        () =>
                    {
                        if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1)))
                        {
                            if (!BluetoothLeDevices.Contains(device))
                            {
                                BluetoothLeDevices.Add(device);
                            }

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

                    return;
                }
            }

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