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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用Windows UWP访问蓝牙特征值?相关的知识,希望对你有一定的参考价值。

我知道这很简单,但是无法得到它。我可以很好地阅读特征,但我无法访问特征值。这是尝试执行此操作的少量代码。输入缓冲区内容只读为'{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
    {

    }
}
答案

它应该取决于您如何传递数据以及您是否使用加密操作。基于官方的BluetoothLE和背后的Scenario2_Client.xaml.cs代码,CharacteristicReadButton_Click方法读取实际值。

 GattReadResult result = await selectedCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
            if (result.Status == GattCommunicationStatus.Success)
            {
                string formattedResult = FormatValueByPresentation(result.Value, presentationFormat);
            }

FormatValueByPresentation代码后面有Scenario2_Client.xaml.cs方法的详细代码,你应该能够根据代码示例实现你的阅读价值:

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";
        }

以上是关于无法使用Windows UWP访问蓝牙特征值?的主要内容,如果未能解决你的问题,请参考以下文章

每个用户都无法访问UWP App

用户无法访问的 UWP 文件存储

Microsoft Band 2 + UWP + 后台任务 => 尝试获取蓝牙设备服务时出错

在 Windows UWP 应用程序中使用 Python 和 C#

通过 Windows 10 uwp 上的代码重新启动

UWP 应用程序设置默认代理和默认 Windows 身份验证