首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到GATT服务

找不到GATT服务
EN

Stack Overflow用户
提问于 2019-04-09 05:10:41
回答 1查看 571关注 0票数 0

在我的UWP应用程序中,我想读取其他BLE设备的设备名称。因此,我正在尝试读取设备的this特征。我可以找到设备的通告UUID和蓝牙地址,但我无法从中找到默认的GATT服务。下面是我获取服务的代码:

代码语言:javascript
复制
if (ulong.TryParse(deviceAddress, out ulong address))
{
    BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(address);

    var genericAccessId = ConvertFromInteger(0x1800);

    GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesForUuidAsync(genericAccessId);

    if (result?.Status == GattCommunicationStatus.Success)
    {
        var genericAccess = result.Services.FirstOrDefault(s => s.Uuid == genericAccessId);

        // genericAccess is always null
        if (genericAccess != null)
        {
            var deviceNameId = ConvertFromInteger(0x2A00);
            var deviceName = await genericAccess.GetCharacteristicsForUuidAsync(deviceNameId);

            if (deviceName?.Status == GattCommunicationStatus.Success)
            {
                var c = deviceName.Characteristics.FirstOrDefault(x => x.Uuid == deviceNameId);

                if (c != null)
                {
                    var v = await c.ReadValueAsync();

                    if (v?.Status == GattCommunicationStatus.Success)
                    {
                        var reader = DataReader.FromBuffer(v.Value);
                        byte[] input = new byte[reader.UnconsumedBufferLength];
                        reader.ReadBytes(input);

                        // Utilize the data as needed
                        string str = System.Text.Encoding.Default.GetString(input);
                        Log?.Invoke(str);
                    }
                }
            }
        }
    }
}

public static Guid ConvertFromInteger(int i)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(i).CopyTo(bytes, 0);
    return new Guid(bytes);
}

Any idea where the problem is?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-09 14:11:13

BLE设备、服务和特性具有用于标识的128位UUID。对于标准化的服务和特性,也有16位短版本,例如0x1800用于Generic Access

为了将16位转换为128位UUID,必须在字节2和3处将16位值填充到以下UUID中(按小端顺序:

代码语言:javascript
复制
0000xxxx-0000-1000-8000-00805F9B34FB

因此0x1800被转换为:

代码语言:javascript
复制
00000018-0000-1000-8000-00805F9B34FB

Windows有一个函数可以帮你做到这一点:BluetoothUuidHelper.FromShortId

代码语言:javascript
复制
var uuid = BluetoothUuidHelper.FromShortId(0x1800);

在以前的Windows版本中,您将使用GattDeviceService.ConvertShortIdToUuid

所以用上面的函数替换你的ConvertFromInteger函数。您的函数填充全0,而不是上面的UUID值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55581798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档