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);
                }
            }
Example #2
0
        /// <summary>
        /// Update the battery level of a ObservableBluetoothLEDevice by searching through known dev nodes
        /// </summary>
        /// <param name="dev">device to update</param>
        /// <returns></returns>
        private async Task UpdateBatteryLevel(ObservableBluetoothLEDevice dev)
        {
            foreach (DeviceInformation devNode in devNodes)
            {
                string addr = dev.BluetoothAddressAsString.Replace(":", String.Empty);

                if (devNode.Properties.Keys.Contains(BatteryLevelGUID) &&
                    devNode.Properties[BatteryLevelGUID] != null &&
                    devNode.Properties.Keys.Contains(BluetoothDeviceAddress) &&
                    devNode.Properties[BluetoothDeviceAddress] != null)
                {
                    if ((string)devNode.Properties[BluetoothDeviceAddress] == addr)
                    {
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                            Windows.UI.Core.CoreDispatcherPriority.Normal,
                            () =>
                        {
                            dev.BatteryLevel = Convert.ToInt32((byte)devNode.Properties[BatteryLevelGUID]);
                        });

                        break;
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Adds the new or updated device to the displayed or unused list
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <returns></returns>
        private async Task AddDeviceToList(DeviceInformation deviceInfo)
        {
            ObservableBluetoothLEDevice dev = new ObservableBluetoothLEDevice(deviceInfo);

            // Let's make it connectable by default, we have error handles in case it doesn't work
            bool shouldDisplay =
                ((dev.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.Bluetooth.Le.IsConnectable") &&
                  (bool)dev.DeviceInfo.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"])) ||
                ((dev.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.IsConnected") &&
                  (bool)dev.DeviceInfo.Properties["System.Devices.Aep.IsConnected"])) ||
                ((dev.DeviceInfo.Properties.Keys.Contains("System.Devices.Aep.IsPaired") &&
                  (bool)dev.DeviceInfo.Properties["System.Devices.Aep.IsPaired"]));

            if (shouldDisplay)
            {
                // Need to lock as another DeviceWatcher might be modifying BluetoothLEDevices
                try
                {
                    await BluetoothLEDevicesLock.WaitAsync();

                    await UpdateBatteryLevel(dev);

                    if (!BluetoothLEDevices.Contains(dev))
                    {
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                            Windows.UI.Core.CoreDispatcherPriority.Normal,
                            () =>
                        {
                            BluetoothLEDevices.Add(dev);
                        });
                    }
                }
                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
            else
            {
                try
                {
                    await BluetoothLEDevicesLock.WaitAsync();

                    unusedDevices.Add(deviceInfo);
                }
                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
        }