【发布时间】: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