【问题标题】:BLE with RPI, wrong endpoint ID selected具有 RPI 的 BLE,选择了错误的端点 ID
【发布时间】:2021-01-03 15:34:13
【问题描述】:

尝试与 BLE 设备(智能灯)通信。

我使用以下依赖项:

    <dependency>
        <groupId>com.github.hypfvieh</groupId>
        <artifactId>bluez-dbus</artifactId>
        <version>0.1.3</version>
    </dependency>

非常有趣的库,迄今为止我发现的最好的 BLE 库,在代码质量、依赖管理和清晰度方面......

问题是,我有一个像这样的工作 gatttool 命令,在 Raspberry Pi 4 上运行:

gatttool --device=C4:AC:05:42:73:A4 -t random --char-write-req -a 0x1f -n a001037F

... 将灯的亮度设置为 100%。注意地址的值(即“-a 0x1f”),对应gattool“characteristics”中的属性“char value handle”:

handle: 0x001e, char properties: 0x28, char value handle: **0x001f**, uuid: **44092842-0567-11e6-b862-0002a5d5c51b**

我尝试在 java 中使用 bluez-dbus 来做同样的事情。我的实现似乎是正确的,但是灯没有响应。我对 dbus-monitor 有以下跟踪:

method call time=1600276508.729104 sender=:1.184 -> destination=org.bluez serial=210 path=/org/bluez/hci0/dev_C4_AC_05_42_73_A4/service001d/**char001e**; interface=org.bluez.GattCharacteristic1; member=WriteValue
   array of bytes [
      0a 01 03 7f
   ]
   array [
   ]
method return time=1600276508.776261 sender=:1.5 -> destination=:1.184 serial=6589 reply_serial=210

看起来一切都很好,除了 bluez-dbus 获取值 0x001e(又名 gatttool 特性中的“句柄”)来驱动灯,它应该是 0x001f(gatttool 中的“char 值句柄”)。

您知道这是对库的错误使用、设备上的错误还是什么?

这里是代码的一小部分摘录,如果你需要更多可以看这里:https://github.com/sebpiller/luke-roberts-lamp-f

    BluetoothDevice lampF = manager.getDevices(true)
        .stream()
        .filter(e -> Objects.equals(e.getAddress(), config.getMac()))
        .findFirst()
        .get();
....
    String uuid = config.getCustomControlService().getUuid();
    BluetoothGattService customControlService = Objects.requireNonNull(lampF.getGattServiceByUuid(uuid));
    LOG.info("found GATT custom control service {} at UUID {}", customControlService, uuid);
....
    String externalApiUuid = config.getCustomControlService().getUserExternalApiEndpoint().getUuid();
    externalApi = Objects.requireNonNull(customControlService.getGattCharacteristicByUuid(externalApiUuid));

...
private void sendCommandToExternalApi(LukeRoberts.LampF.Command command, Byte... parameters) {
    reconnectIfNeeded();

    try {
        externalApi.writeValue(/*reversed*/ command.toByteArray(parameters),    Collections.emptyMap());
    } catch (DBusException e) {
        throw new IllegalStateException("unable to change brightness: " + e, e);
    }
}

感谢您的宝贵时间!

编辑:

我是个白痴-阅读障碍者。 0x0a 与 0xa0 不同。

有时我想把头撞在墙上......

感谢您的帮助:)

【问题讨论】:

    标签: java bluetooth bluetooth-lowenergy raspberry-pi4


    【解决方案1】:

    gattool 是 BlueZ 的 eight tools that have been deprecated 之一。

    要对此进行调试,我建议使用bluetoothctl 来测试连接设备的正确路径。会话可能如下所示:

    pi@raspberrypi:~ $ bluetoothctl 
    [bluetooth]# connect C4:AC:05:42:73:A4 
    [my lamp]# menu gatt
    [my lamp]# select-attribute 44092842-0567-11e6-b862-0002a5d5c51b
    [my lamp:/service0032/char0036]# write 0xa0 0x01 0x03 0x7F
    Attempting to write /org/bluez/hci0/dev_C4_AC_05_42_73_A4/service0032/char0036
    

    在命令行上,向您展示所有路径都可以使用通用 D-Bus 工具完成:

    pi@raspberrypi:~ $ busctl tree org.bluez
    

    获得路径后,您可以使用 D-Bus 从命令行执行此操作。

    pi@raspberrypi:~ $ busctl call org.bluez /org/bluez/hci0/dev_DE_82_35_E7_43_BE org.bluez.Device1 Connect
    pi@raspberrypi:~ $ busctl call org.bluez /org/bluez/hci0/dev_DE_82_35_E7_43_BE/service0032/char0036 org.bluez.GattCharacteristic1 WriteValue aya{sv} 4 0xa0 0x01 0x03 0x7f 0
    

    希望通过这些实验的知识,您可以更好地了解 Java 应用程序的情况。

    【讨论】:

    • gatttool 是目前唯一能与灯泡正常工作的工具。灯会在 8 秒后终止任何蓝牙连接,而且我的速度不够快,无法在 bluetoothctl 交互模式下如此快速地键入命令。而且我编写 bluetoothctl 命令的尝试都失败了......无论如何,我会尝试另一个答案(d-feet)中建议的工具,这可能会有很大帮助
    • 如果您准备尝试使用 Python,那么您可能想看看这个答案:raspberrypi.stackexchange.com/a/114175/121848
    • 如果我不能用 Java 制作它,我会尝试 python。至少你给我的命令 (busctl) 可以正确地完成工作,并使用 charasteristic (.../char001e) 中指定的正确端点。仍在调试中。
    • 很高兴你解决了它!让我们绊倒的总是简单的事情 :-) 你的问题中有信息,我错过了。
    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多