Example #1
0
        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;
                    }
                }
            }
        }
            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 #3
0
        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"]));

            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);
                            Debug.WriteLine($"Added device name = {dev.DeviceInfo.Name}, address = {dev.BluetoothAddressAsString}");
                        });
                    }
                }
                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
            else
            {
                try
                {
                    await BluetoothLEDevicesLock.WaitAsync();

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