OGeek|极客世界-中国程序员成长平台

标题: ios - GetDescriptor() 空值/128 位 UUID 描述符? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:46
标题: ios - GetDescriptor() 空值/128 位 UUID 描述符?

我正在使用 Xamarin 编写代码。我正在开发我的 IOS 应用程序,它允许我读取 BLE 设备、服务、特征值和通知的激活。

我的 BLE 信标有一个自定义服务,其中包含两个自定义特征,并且都使用 CCCD 实现了通知。

我的 ble 设备工作正常我用 BLE 扫描仪应用程序对其进行了测试,它运行良好,没有任何问题。 我可以读取值,并且可以激活这两个特征的通知。 See picture here .

我使用 xamarin 编写的应用程序可以正常工作(读取服务、特征值......)唯一没有工作的问题是通知的激活。以下是部分代码:

public UUID Charac_UUID0 = UUID.FromString("0000beef-1212-efde-1523-785fef13d123");
public UUID Charac_UUID = UUID.FromString("0000b1e0-1212-efde-1523-785fef13d123") ;
public UUID Descr_UUID =  UUID.FromString("00002902-1212-efde-1523-785fef13d123");
protected BluetoothGattCharacteristic _charac;
....
....
this._charac = App.Current.State.SelectedService.GetCharacteristic(Charac_UUID0);
BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].SetCharacteristicNotification(_charac, true);
BluetoothGattDescriptor descriptor = _charac.GetDescriptor(Descr_UUID0);
descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].WriteDescriptor(descriptor);

代码总是在 descriptor.SetValue 处给我一个错误,它表明描述符为 NULL,这意味着 _charac.GetDescriptor 没有返回任何值。

我怀疑描述符 UUID 值 (Descr_UUID) 不正确。我不知道如何确定 Descr_UUID,但我在互联网上看到了很多例子,人们用 2902 替换了特征的自定义 UUID,这给了我一个 128 描述符 UUID 等于 00002902-1212- efde-1523-785fef13d123.

但是这里有一个问题。两个特性的描述符 UUID 将相同,因为两个特性的基本 UUID 相同?

有什么办法吗?



Best Answer-推荐答案


3 年后我确定你已经明白了,但是 2902 是客户端特征配置 Descriptor 的 16 位值,而且我相信它也应该使用“BASE UUID”来膨胀,而不仅仅是你的特征描述符使用的任何东西。见:answer

这将使用 BASE UUID 将您的 16(或 32)位 uuid 转换为完整的 128 位 uuid:

    public static UUID ConvertUuid(uint uuid)
    {
        const long msbMask = 0x0000000000001000;
        const ulong lsb = 0x800000805f9b34fb;
        var msb = msbMask | ((ulong)(uuid & uint.MaxValue) << 32);
        return new UUID((long)msb, unchecked((long)lsb));
    }

关于ios - GetDescriptor() 空值/128 位 UUID 描述符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907507/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4