示例#1
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"]));


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

                    await UpdateBatteryLevel(dev);

                    if ((dev.Name.Length != 0) && (!BluetoothLEDevices.Contains(dev)))
                    {
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                            Windows.UI.Core.CoreDispatcherPriority.Normal,
                            () =>
                        {
                            Log.WriteLine("~~~Finding device: " + dev.Name);
                            BluetoothLEDevices.Add(dev);
                            OnPropertyChanged(new PropertyChangedEventArgs("DeviceList"));
                        });
                    }
                }
                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
            else
            {
                try
                {
                    await BluetoothLEDevicesLock.WaitAsync();

                    unusedDevices.Add(deviceInfo);
                }

                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
        }
示例#2
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();

                    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);
                    dev.Dispose();
                }
                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
        }
        /// <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();

                    UpdateBatteryLevel(dev);

                    if (!BluetoothLEDevices.Contains(dev))
                    {
                        BluetoothLEDevices.Add(dev);
                        OnDeviceEnumEvent(new BLTEEnumEventArgs()
                        {
                            Notify = BLTEEnumEvent.Add, Device = dev
                        });
                    }
                }
                finally
                {
                    BluetoothLEDevicesLock.Release();
                }
            }
            else
            {
                try
                {
                    await BluetoothLEDevicesLock.WaitAsync();

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