I'm trying to make a simple UWP app that displays the value read from a heart rate monitor (Wahoo TICKR FIT). I am able to connect to the monitor, but cannot access the value of the HeartRateMeasurement characteristic. The GattReadResult has a Status of ProtocolError and a Value of null. How do I fix the ProtocolError?
This is all in a plain UWP app using the Visual Studio 2017 template. The plan is to then transfer the working code into a HoloLens project.
Here is the code:
string btAddr = "F013C3B15603";
System.UInt64 addrInt = Convert.ToUInt64(btAddr, 16);
BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(addrInt);
string guidString = "0000180d-0000-1000-8000-00805f9b34fb"; // Armband HeartRate uuid
Guid guid = new Guid(guidString);
GattDeviceService gattService = device.GetGattService(guid);
// Try to access the characteristic we want
DeviceAccessStatus serviceAccessStatus = await gattService.RequestAccessAsync(); // allowed
GattOpenStatus openStatus = await gattService.OpenAsync(GattSharingMode.SharedReadAndWrite); // success
GattCharacteristicsResult results = await gattService.GetCharacteristicsAsync();
IReadOnlyList<GattCharacteristic> charList = results.Characteristics;
Debug.WriteLine(charList.Count); // should be 2
GattCharacteristic heartRateChar = charList[0];
Debug.WriteLine(heartRateChar.AttributeHandle); // should be 15
// Try to pull the heart rate data
GattCommunicationStatus commStatus = await heartRateChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); // This works
GattReadResult heartRateResult = await heartRateChar.ReadValueAsync(BluetoothCacheMode.Uncached); // Status is ProtocolError (2)
The issue appears at the very end, at the ReadValueAsync call.
Mike Petrichenko's comment was the solution. The characteristic was not readable, and I needed to create an event handler method for the ValueChanged event to receive the data as it comes in.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.