蓝牙低能耗(BLE)与树莓派(RPI)配合使用,选择了错误的端点ID。

huangapple go评论71阅读模式
英文:

BLE with RPI, wrong endpoint ID selected

问题

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

我使用以下依赖项:

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

非常有趣的库,到目前为止是我找到的最好的BLE库,代码质量、依赖管理和清晰度方面都很出色...

问题是,我在树莓派4上运行了一个类似这样的工作的gatttool命令:

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

... 这会将灯的亮度设置为100%。注意地址的值(即 "-a 0x1f"),它对应于gatttool "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特征中的“handle”)来控制灯之外,一切看起来都很好,实际上应该是0x001f(gatttool中的“char value handle”)。

你知道这是库的错误使用,设备的错误,还是其他什么原因吗?

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

蓝牙设备"lampF"的获取:

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));
String externalApiUuid = config.getCustomControlService().getUserExternalApiEndpoint().getUuid();
externalApi = Objects.requireNonNull(customControlService.getGattCharacteristicByUuid(externalApiUuid));

发送命令到外部API:

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。

有时候我真想把头撞到墙上...

谢谢你的帮助 蓝牙低能耗(BLE)与树莓派(RPI)配合使用,选择了错误的端点ID。

英文:

Trying to communicate with a BLE device (smart lamp).

I use the following dependency:

    &lt;dependency&gt;
        &lt;groupId&gt;com.github.hypfvieh&lt;/groupId&gt;
        &lt;artifactId&gt;bluez-dbus&lt;/artifactId&gt;
        &lt;version&gt;0.1.3&lt;/version&gt;
    &lt;/dependency&gt;

Very interesting library, by far the best I have found so far for BLE, in term of code quality, dependency management, and clarity...

The problem is, I have a working gatttool command like this, running on a Raspberry Pi 4:

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

... which set the brightness of the lamp to 100%. Note the value of the address (ie. "-a 0x1f"), which corresponds to the attribute "char value handle" in gattool "characteristics":

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

I try to make the same using bluez-dbus in java. My implementation seems correct, but the lamp doesn't respond. I have the following trace with dbus-monitor :

method call time=1600276508.729104 sender=:1.184 -&gt; 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 -&gt; destination=:1.184 serial=6589 reply_serial=210

It looks everything is fine except bluez-dbus pick up the value 0x001e (aka. the "handle" in gatttool characteristics), to drive the lamp, where it should have been 0x001f ("char value handle" in gatttool).

Do you know if this is a bad usage of the library, an error on the device, or what ?

Here is a little excerpt of the code, if you need more you can look here: https://github.com/sebpiller/luke-roberts-lamp-f

    BluetoothDevice lampF = manager.getDevices(true)
        .stream()
        .filter(e -&gt; Objects.equals(e.getAddress(), config.getMac()))
        .findFirst()
        .get();
....
    String uuid = config.getCustomControlService().getUuid();
    BluetoothGattService customControlService = Objects.requireNonNull(lampF.getGattServiceByUuid(uuid));
    LOG.info(&quot;found GATT custom control service {} at UUID {}&quot;, 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(&quot;unable to change brightness: &quot; + e, e);
    }
}

Thanks for your time !

EDIT:

I am an idiotic-dyslexic. 0x0a is not the same as 0xa0.

Sometimes I'd like to crush my head on the wall....

Thanks for your help 蓝牙低能耗(BLE)与树莓派(RPI)配合使用,选择了错误的端点ID。

答案1

得分: 1

gattool是BlueZ已弃用的八种工具之一

要调试此问题,我建议使用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应用程序中正在发生的情况。

英文:

gattool is one of the eight tools that have been deprecated by BlueZ.

To debug this I would advise using bluetoothctl to workout what the correct paths are for the connected device. A session might look like this:

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

On the command line, to show you all the paths can be done with generic D-Bus tools:

pi@raspberrypi:~ $ busctl tree org.bluez

Once you have the paths then you can do it from the command line with 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

Hopefully with the knowledge from these experiments you can better understand what is going on with the Java application.

huangapple
  • 本文由 发表于 2020年9月17日 02:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63926205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定