/// <summary> /// Disconnects the current BLE heart rate device. /// </summary> /// <returns></returns> public async Task DisconnectAsync() { if (_heartRateDevice != null) { if (_heartRateMeasurementCharacteristic != null) { //NOTE: might want to do something here if the result is not successful var result = await _heartRateMeasurementCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None); if (_heartRateMeasurementCharacteristic.Service != null) { _heartRateMeasurementCharacteristic.Service.Dispose(); } _heartRateMeasurementCharacteristic = null; } if (_heartRateMeasurementAttribute != null) { if (_heartRateMeasurementAttribute.service != null) { _heartRateMeasurementAttribute.service.Dispose(); } _heartRateMeasurementAttribute = null; } if (_heartRateAttribute != null) { if (_heartRateAttribute.service != null) { _heartRateAttribute.service.Dispose(); } _heartRateAttribute = null; } _serviceCollection = new List <BluetoothAttribute>(); _heartRateDevice.Dispose(); _heartRateDevice = null; DeviceConnectionStatusChanged(null, null); } }
/// <summary> /// Disconnects the current BLE heart rate device. /// </summary> /// <returns></returns> public async Task DisconnectAsync() { if (_heartRateDevice != null && _heartRateDevice.ConnectionStatus == BluetoothConnectionStatus.Connected) { if (_heartRateMeasurementCharacteristic != null) { var result = await _heartRateMeasurementCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None); if (result == GattCommunicationStatus.Success) { _heartRateMeasurementCharacteristic.ValueChanged -= HeartRateValueChanged; _heartRateMeasurementCharacteristic = null; } } _heartRateMeasurementAttribute = null; _heartRateAttribute = null; _heartRateDevice.ConnectionStatusChanged -= DeviceConnectionStatusChanged; _serviceCollection = null; _heartRateDevice.Dispose(); _heartRateDevice = null; } }
private async Task <CharacteristicResult> SetupHeartRateCharacteristic() { _heartRateAttribute = _serviceCollection.Where(a => a.Name == "HeartRate").FirstOrDefault(); if (_heartRateAttribute == null) { return(new CharacteristicResult() { IsSuccess = false, Message = "Cannot find HeartRate service" }); } var characteristics = await GetServiceCharacteristicsAsync(_heartRateAttribute); _heartRateMeasurementAttribute = characteristics.Where(a => a.Name == "HeartRateMeasurement").FirstOrDefault(); if (_heartRateMeasurementAttribute == null) { return(new CharacteristicResult() { IsSuccess = false, Message = "Cannot find HeartRateMeasurement characteristic" }); } _heartRateMeasurementCharacteristic = _heartRateMeasurementAttribute.characteristic; // Get all the child descriptors of a characteristics. Use the cache mode to specify uncached descriptors only // and the new Async functions to get the descriptors of unpaired devices as well. var result = await _heartRateMeasurementCharacteristic.GetDescriptorsAsync(BluetoothCacheMode.Uncached); if (result.Status != GattCommunicationStatus.Success) { return(new CharacteristicResult() { IsSuccess = false, Message = result.Status.ToString() }); } if (_heartRateMeasurementCharacteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify)) { var status = await _heartRateMeasurementCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); if (status == GattCommunicationStatus.Success) { _heartRateMeasurementCharacteristic.ValueChanged += HeartRateValueChanged; } return(new CharacteristicResult() { IsSuccess = status == GattCommunicationStatus.Success, Message = status.ToString() }); } else { return(new CharacteristicResult() { IsSuccess = false, Message = "HeartRateMeasurement characteristic does not support notify" }); } }
private async Task <List <BluetoothAttribute> > GetServiceCharacteristicsAsync(BluetoothAttribute service) { IReadOnlyList <GattCharacteristic> characteristics = null; try { // Ensure we have access to the device. var accessStatus = await service.service.RequestAccessAsync(); if (accessStatus == DeviceAccessStatus.Allowed) { // BT_Code: Get all the child characteristics of a service. Use the cache mode to specify uncached characterstics only // and the new Async functions to get the characteristics of unpaired devices as well. var result = await service.service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached); if (result.Status == GattCommunicationStatus.Success) { characteristics = result.Characteristics; } else { characteristics = new List <GattCharacteristic>(); } } else { // Not granted access // On error, act as if there are no characteristics. characteristics = new List <GattCharacteristic>(); } } catch (Exception ex) { characteristics = new List <GattCharacteristic>(); } var characteristicCollection = new List <BluetoothAttribute>(); characteristicCollection.AddRange(characteristics.Select(a => new BluetoothAttribute(a))); return(characteristicCollection); }