【问题标题】:BGScript GATT characteristic write from C# not working从 C# 写入的 BGScript GATT 特征不起作用
【发布时间】:2021-06-06 12:12:35
【问题描述】:

在我的设备配置 gatt.xml 中,我添加了一个带有自定义 UUID 的自定义 GATT 特征并启用了读取和写入属性。配对后使用 Windows 蓝牙 API,当我尝试从 GATT 特征读取时,它工作正常,但写入 GATT 特征不起作用。我一直被拒绝访问,但有一个例外。下面我添加了示例 gatt.xml、bgs 和 C# 代码。我正在使用 Bluegiga v1.3.1_API。当前设置适用于 USB 加密狗,但我正在尝试用 Windows 蓝牙 API 替换它。

gatt.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

<service uuid="180A">
  <description>Device Information</description>
  
  <characteristic uuid="2a29">
    <properties read="true" const="true" />
    <value>company name</value>
    <description>Manufacturer Name String</description>
  </characteristic>
  
  <characteristic uuid="2a24">
    <properties read="true" const="true" />
    <value>device name</value>
    <description>Model Number String</description>
  </characteristic>
      
  <characteristic uuid="2a27">
    <properties read="true" const="true"/>
    <value>2.0</value>
    <description>Hardware Revision String</description>
  </characteristic>
  
  <characteristic uuid="95551f84-7562-4c30-9455-be0750914ac2" id="xgatt_Params">
    <properties read="true" write="true"/>
    <value type="user" length="5"/>
    <description>Params</description>

  </characteristic>
      
</service>
    
</configuration>

来自 bgs 脚本的属性值事件

event attributes_value(connection, reason, handle, offset, value_len, value_data)
    if handle = xgatt_Params then
        if value_len = 2 then
            if value_data(0:2) = $0115 then
                memcpy(addrPaired(0), addrConnected(0),6)
                updatePairing=1
                call attributes_user_write_response(connection, 0)
            else
                call attributes_user_write_response(connection, $80)
            end if
        else
            if value_len >= numParams then
                memcpy(params(0), value_data(0), numParams)
                call updateParams()
                call attributes_user_write_response(connection, 0)
            else
                call attributes_user_write_response(connection, $80)
            end if
        end if
    end if
end

C# 代码

if (properties.HasFlag(GattCharacteristicProperties.Write))
{
    try
    {
        var writer = new DataWriter();
        writer.WriteByte(01);
        writer.WriteByte(15);
        var result = await characteristic.WriteValueAsync(writer.DetachBuffer(), 
GattWriteOption.WriteWithResponse);

        if (result == GattCommunicationStatus.Success)
        {
            Console.Write("\n Success");
        }
        else if (result == GattCommunicationStatus.AccessDenied)
        {
            Console.Write("\n Access Denied");
        }
        else if (result == GattCommunicationStatus.Unreachable)
        {
            Console.Write("\n Unreachable");
        }

        else if (result == GattCommunicationStatus.ProtocolError)
        {
            Console.Write("\n ProtocolError");
        }
    }
    catch (Exception ex) when ((uint)ex.HResult == 0x80650003 || (uint)ex.HResult == 
0x80070005)
    {
        Console.Write("\n " + ex.Message);
    }

}

【问题讨论】:

  • 您无法从 DataWriter 中读取数据:var result = await characteristic.WriteValueAsync(writer.DetachBuffer(), GattWriteOption.WriteWithResponse);
  • 为了阅读,我使用的是 ReadValueAsync。我在写作时遇到问题,这就是我发布编写代码部分的原因。
  • 在网络中,您有一个写入流,您无法读取该流。问题在于“writer.DetachBuffer()”
  • 对不起,我没有理解你的意思。据我了解,您想说我不能一次读写?如果这就是你的意思,那么只有写作也是行不通的。或者你的意思是我写后得到的结果不可读?你能澄清一下吗?
  • 下面显示了哪些是读哪些是写:os.mbed.com/docs/mbed-os/v6.7/mbed-os-api-doxy/…

标签: c# bluetooth-lowenergy bluetooth-gatt bluegiga


【解决方案1】:

Windows 不允许写入 GAP 服务。所以我所做的是使用如下所示的自定义 UUID 制作具有特征的服务,现在它正在工作。

<service uuid="97b6682c-ca92-11eb-b8bc-0242ac130003">
  <description>meso service</description>

  <!-- custom write-only characteristic -->
    <characteristic uuid="97b66a7a-ca92-11eb-b8bc-0242ac130003" id="xgatt_params_windows">
      <description>Data</description>
      <properties write="true" />
      <value type="user" length="5"/>
    </characteristic>
</service>

要记住的另一件事是,您需要以相反的顺序从 Windows 写入十六进制值。例如,在我的 BGScript 中,它的预期值是 0115,因此您应该从窗口中反向写入值。这完全取决于您使用的系统,例如 BigEndian(MSB 优先,例如:摩托罗拉处理器), 或 LittleEndian(LSB 优先,例如:英特尔处理器)。

var writer = new DataWriter();
writer.WriteByte(15);
writer.WriteByte(01);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多