菜鸟教程小白 发表于 2022-12-11 17:46:09

ios - GetDescriptor() 空值/128 位 UUID 描述符?


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

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

<p>我的 ble 设备工作正常我用 BLE 扫描仪应用程序对其进行了测试,它运行良好,没有任何问题。
我可以读取值,并且可以激活这两个特征的通知。 <a href="http://i.stack.imgur.com/SM4Cm.png" rel="noreferrer noopener nofollow">See picture here</a> .</p>

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

<pre><code>public UUID Charac_UUID0 = UUID.FromString(&#34;0000beef-1212-efde-1523-785fef13d123&#34;);
public UUID Charac_UUID = UUID.FromString(&#34;0000b1e0-1212-efde-1523-785fef13d123&#34;) ;
public UUID Descr_UUID =UUID.FromString(&#34;00002902-1212-efde-1523-785fef13d123&#34;);
protected BluetoothGattCharacteristic _charac;
....
....
this._charac = App.Current.State.SelectedService.GetCharacteristic(Charac_UUID0);
BluetoothLEManager.Current.ConnectedDevices.SetCharacteristicNotification(_charac, true);
BluetoothGattDescriptor descriptor = _charac.GetDescriptor(Descr_UUID0);
descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
BluetoothLEManager.Current.ConnectedDevices.WriteDescriptor(descriptor);
</code></pre>

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

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

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

<p>有什么办法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>3 年后我确定你已经明白了,但是 2902 是客户端特征配置 <a href="https://www.bluetooth.com/specifications/gatt/descriptors/" rel="noreferrer noopener nofollow">Descriptor</a> 的 16 位值,而且我相信它也应该使用“BASE UUID”来膨胀,而不仅仅是你的特征描述符使用的任何东西。见:<a href="https://stackoverflow.com/a/36212021/5122089" rel="noreferrer noopener nofollow">answer</a> </p>

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

<pre><code>    public static UUID ConvertUuid(uint uuid)
    {
      const long msbMask = 0x0000000000001000;
      const ulong lsb = 0x800000805f9b34fb;
      var msb = msbMask | ((ulong)(uuid &amp; uint.MaxValue) &lt;&lt; 32);
      return new UUID((long)msb, unchecked((long)lsb));
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - GetDescriptor() 空值/128 位 UUID 描述符?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39907507/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39907507/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - GetDescriptor() 空值/128 位 UUID 描述符?