C# 无法使用Windows UWP访问蓝牙特征值?

C# 无法使用Windows UWP访问蓝牙特征值?,c#,uwp,bluetooth,C#,Uwp,Bluetooth,我知道这很简单,但就是不明白。我可以很好地读取特征,但无法访问特征值。下面是尝试执行此操作的少量代码。输入缓冲区内容只是读取为“{byte[0]}”,而不是值0x1234,该值是编程值,由其他BLE客户端读取 GattReadResult cVal = await selectedCharacteristic.ReadValueAsync(); if (cVal.Status == GattCommunicationStatus.Success) { try {

我知道这很简单,但就是不明白。我可以很好地读取特征,但无法访问特征值。下面是尝试执行此操作的少量代码。输入缓冲区内容只是读取为“{byte[0]}”,而不是值0x1234,该值是编程值,由其他BLE客户端读取

GattReadResult cVal = await selectedCharacteristic.ReadValueAsync();
if (cVal.Status == GattCommunicationStatus.Success)
{
    try
    {
        var reader = DataReader.FromBuffer(cVal.Value);
        var input = new byte[reader.UnconsumedBufferLength];
        reader.ReadBytes(input);
    }
    catch
    {

    }
}

它应该取决于您如何传递数据以及是否使用加密操作。根据官方数据和后面的代码,CharacteristicReadButton\u Click方法读取实际值

 GattReadResult result = await selectedCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
            if (result.Status == GattCommunicationStatus.Success)
            {
                string formattedResult = FormatValueByPresentation(result.Value, presentationFormat);
            }
代码背后有FormatValueByPresentation方法的详细代码,您应该能够根据代码示例实现您的读取值:

private string FormatValueByPresentation(IBuffer buffer, GattPresentationFormat format)
        {
            // BT_Code: For the purpose of this sample, this function converts only UInt32 and
            // UTF-8 buffers to readable text. It can be extended to support other formats if your app needs them.
            byte[] data;
            CryptographicBuffer.CopyToByteArray(buffer, out data);
            if (format != null)
            {
                if (format.FormatType == GattPresentationFormatTypes.UInt32 && data.Length >= 4)
                {
                    return BitConverter.ToInt32(data, 0).ToString();
                }
                else if (format.FormatType == GattPresentationFormatTypes.Utf8)
                {
                    try
                    {
                        return Encoding.UTF8.GetString(data);
                    }
                    catch (ArgumentException)
                    {
                        return "(error: Invalid UTF-8 string)";
                    }
                }
                else
                {
                    // Add support for other format types as needed.
                    return "Unsupported format: " + CryptographicBuffer.EncodeToHexString(buffer);
                }
            }
            else if (data != null)
            {
                // We don't know what format to use. Let's try some well-known profiles, or default back to UTF-8.
                if (selectedCharacteristic.Uuid.Equals(GattCharacteristicUuids.HeartRateMeasurement))
                {
                    try
                    {
                        return "Heart Rate: " + ParseHeartRateValue(data).ToString();
                    }
                    catch (ArgumentException)
                    {
                        return "Heart Rate: (unable to parse)";
                    }
                }
                else if (selectedCharacteristic.Uuid.Equals(GattCharacteristicUuids.BatteryLevel))
                {
                    try
                    {
                        // battery level is encoded as a percentage value in the first byte according to
                        // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml
                        return "Battery Level: " + data[0].ToString() + "%";
                    }
                    catch (ArgumentException)
                    {
                        return "Battery Level: (unable to parse)";
                    }
                }
                // This is our custom calc service Result UUID. Format it like an Int
                else if (selectedCharacteristic.Uuid.Equals(Constants.ResultCharacteristicUuid))
                {
                    return BitConverter.ToInt32(data, 0).ToString();
                }
                // No guarantees on if a characteristic is registered for notifications.
                else if (registeredCharacteristic != null)
                {
                    // This is our custom calc service Result UUID. Format it like an Int
                    if (registeredCharacteristic.Uuid.Equals(Constants.ResultCharacteristicUuid))
                    {
                        return BitConverter.ToInt32(data, 0).ToString();
                    }
                }
                else
                {
                    try
                    {
                        return "Unknown format: " + Encoding.UTF8.GetString(data);
                    }
                    catch (ArgumentException)
                    {
                        return "Unknown format";
                    }
                }
            }
            else
            {
                return "Empty data received";
            }
            return "Unknown format";
        }

有时需要在其他客户端(如nRF Connect)中多次尝试读取才能工作。所以,我把这个例程放在一个按钮中,可以重试多次。谢谢。工作起来很有魅力!Hi @ michael meier如果这个或任何答案已经解决了你的问题,请考虑点击复选标记。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。