/// <summary>
            /// Compares two <see cref="ObservableBluetoothLEDevice"/> and returns a value indicating
            /// whether one is less than, equal to, or greater than the other.
            /// </summary>
            /// <param name="x">First object to compare</param>
            /// <param name="y">Second object to compare</param>
            /// <returns>Returns 0 if equal</returns>
            public int Compare(object x, object y)
            {
                ObservableBluetoothLEDevice a = x as ObservableBluetoothLEDevice;
                ObservableBluetoothLEDevice b = y as ObservableBluetoothLEDevice;

                if (a == null || b == null)
                {
                    throw new InvalidOperationException("Compared objects are not ObservableBluetoothLEDevice");
                }

                // If they're equal
                if (a.RSSI == b.RSSI)
                {
                    return(0);
                }

                // RSSI == 0 means we don't know it. Always make that the end.
                if (b.RSSI == 0)
                {
                    return(-1);
                }

                if (a.RSSI < b.RSSI || a.RSSI == 0)
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
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();
            }
        }