/// <summary> /// Executes when the connection state changes /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The arguments.</param> private async void BluetoothLEDevice_ConnectionStatusChanged(BluetoothLEDevice sender, object args) { await DispatcherQueue.ExecuteOnUIThreadAsync( () => { IsPaired = DeviceInfo.Pairing.IsPaired; IsConnected = BluetoothLEDevice.ConnectionStatus == BluetoothConnectionStatus.Connected; }, DispatcherQueuePriority.Normal); }
/// <summary> /// Load the glyph for this device /// </summary> private async void LoadGlyph() { await DispatcherQueue.ExecuteOnUIThreadAsync( async() => { var deviceThumbnail = await DeviceInfo.GetGlyphThumbnailAsync(); var glyphBitmapImage = new BitmapImage(); await glyphBitmapImage.SetSourceAsync(deviceThumbnail); Glyph = glyphBitmapImage; }, DispatcherQueuePriority.Normal); }
/// <summary> /// Restores stored state to given element. /// </summary> /// <param name="element">Element to restore state to</param> public void Restore(FrameworkElement element) { _dispatcherQueue.ExecuteOnUIThreadAsync(() => { element.HorizontalAlignment = HorizontalAlignment; element.VerticalAlignment = VerticalAlignment; element.Width = Width; element.Height = Height; element.Margin = Margin; }); }
/// <summary> /// ConnectAsync to this bluetooth device /// </summary> /// <returns>Connection task</returns> /// <exception cref="Exception">Throws Exception when no permission to access device</exception> public async Task ConnectAsync() { await DispatcherQueue.ExecuteOnUIThreadAsync( async() => { if (BluetoothLEDevice == null) { BluetoothLEDevice = await BluetoothLEDevice.FromIdAsync(DeviceInfo.Id); if (BluetoothLEDevice == null) { throw new Exception("Connection error, no permission to access device"); } } BluetoothLEDevice.ConnectionStatusChanged += BluetoothLEDevice_ConnectionStatusChanged; BluetoothLEDevice.NameChanged += BluetoothLEDevice_NameChanged; IsPaired = DeviceInfo.Pairing.IsPaired; IsConnected = BluetoothLEDevice.ConnectionStatus == BluetoothConnectionStatus.Connected; Name = BluetoothLEDevice.Name; // Get all the services for this device var getGattServicesAsyncTokenSource = new CancellationTokenSource(5000); var getGattServicesAsyncTask = await Task.Run( () => BluetoothLEDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached), getGattServicesAsyncTokenSource.Token); _result = await getGattServicesAsyncTask; if (_result.Status == GattCommunicationStatus.Success) { // In case we connected before, clear the service list and recreate it Services.Clear(); foreach (var service in _result.Services) { Services.Add(new ObservableGattDeviceService(service)); } ServiceCount = Services.Count; } else { if (_result.ProtocolError != null) { throw new Exception(_result.ProtocolError.GetErrorString()); } } }, DispatcherQueuePriority.Normal); }
/// <summary> /// Updates this device's deviceInformation /// </summary> /// <param name="deviceUpdate">The device information which has been updated.</param> /// <returns>The task of the update.</returns> public async Task UpdateAsync(DeviceInformationUpdate deviceUpdate) { await DispatcherQueue.ExecuteOnUIThreadAsync( () => { DeviceInfo.Update(deviceUpdate); Name = DeviceInfo.Name; IsPaired = DeviceInfo.Pairing.IsPaired; LoadGlyph(); OnPropertyChanged("DeviceInfo"); }, DispatcherQueuePriority.Normal); }
/// <summary> /// Updates device metadata based on advertisement received /// </summary> /// <param name="sender">The Bluetooth LE Advertisement Watcher.</param> /// <param name="args">The advertisement.</param> private async void AdvertisementWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) { await DispatcherQueue.ExecuteOnUIThreadAsync( () => { if (_readerWriterLockSlim.TryEnterReadLock(TimeSpan.FromSeconds(1))) { foreach (var device in BluetoothLeDevices) { if (device.BluetoothAddressAsUlong == args.BluetoothAddress) { device.ServiceCount = args.Advertisement.ServiceUuids.Count(); } } _readerWriterLockSlim.ExitReadLock(); } }, DispatcherQueuePriority.Normal); }
/// <summary> /// Executes when a device is removed from enumeration /// </summary> /// <param name="sender">The device watcher.</param> /// <param name="deviceInfoUpdate">An update of the device.</param> private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { // Protect against race condition if the task runs after the app stopped the deviceWatcher. if (sender == _deviceWatcher) { await DispatcherQueue.ExecuteOnUIThreadAsync( () => { if (_readerWriterLockSlim.TryEnterWriteLock(TimeSpan.FromSeconds(1))) { var device = BluetoothLeDevices.FirstOrDefault(i => i.DeviceInfo.Id == deviceInfoUpdate.Id); BluetoothLeDevices.Remove(device); var unusedDevice = _unusedDevices.FirstOrDefault(i => i.Id == deviceInfoUpdate.Id); _unusedDevices?.Remove(unusedDevice); _readerWriterLockSlim.ExitWriteLock(); } }, DispatcherQueuePriority.Normal); } }
/// <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(); } }
/// <summary> /// Executes when the name of this devices changes /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The arguments.</param> private async void BluetoothLEDevice_NameChanged(BluetoothLEDevice sender, object args) { await DispatcherQueue.ExecuteOnUIThreadAsync(() => { Name = BluetoothLEDevice.Name; }, DispatcherQueuePriority.Normal); }
private static async void RunInUIThread(DispatcherQueue dispatcherQueue, Action action) { await dispatcherQueue.ExecuteOnUIThreadAsync(action, DispatcherQueuePriority.Normal); }
/// <summary> /// When the Characteristics value changes. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="GattValueChangedEventArgs"/> instance containing the event data.</param> private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { await DispatcherQueue.ExecuteOnUIThreadAsync(() => { SetValue(args.CharacteristicValue); }, DispatcherQueuePriority.Normal); }