[英]UWP app can't get data from heart rate monitor, receiving protocol error
I'm trying to make a simple UWP app that displays the value read from a heart rate monitor (Wahoo TICKR FIT). 我正在尝试制作一个简单的UWP应用程序,显示从心率监测器(Wahoo TICKR FIT)读取的值。 I am able to connect to the monitor, but cannot access the value of the HeartRateMeasurement characteristic. 我能够连接到监视器,但无法访问HeartRateMeasurement特性的值。 The GattReadResult has a Status of ProtocolError and a Value of null. GattReadResult具有ProtocolError状态和null值。 How do I fix the ProtocolError? 我如何修复ProtocolError?
This is all in a plain UWP app using the Visual Studio 2017 template. 这是使用Visual Studio 2017模板的简单UWP应用程序。 The plan is to then transfer the working code into a HoloLens project. 计划是将工作代码转移到HoloLens项目中。
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. 问题出现在ReadValueAsync调用的最后。
Mike Petrichenko's comment was the solution. Mike Petrichenko的评论是解决方案。 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. 该特性是不可读的,我需要为ValueChanged事件创建一个事件处理程序方法,以便在数据进入时接收数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.