[bluetooth.bluez] update to dbus version 0.3.0 (#18124)

Signed-off-by: Jörg Sautter <joerg.sautter@gmx.net>
pull/18019/head^2
joerg1985 2025-01-18 17:37:08 +01:00 committed by GitHub
parent 0b3383b95c
commit 241829dd18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 58 additions and 77 deletions

View File

@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>com.github.hypfvieh</groupId> <groupId>com.github.hypfvieh</groupId>
<artifactId>bluez-dbus-osgi</artifactId> <artifactId>bluez-dbus-osgi</artifactId>
<version>0.1.4</version> <version>0.3.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -4,7 +4,7 @@
<feature name="openhab-binding-bluetooth-bluez" description="Bluetooth Binding Bluez" version="${project.version}"> <feature name="openhab-binding-bluetooth-bluez" description="Bluetooth Binding Bluez" version="${project.version}">
<feature>openhab-runtime-base</feature> <feature>openhab-runtime-base</feature>
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0</bundle> <bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle> <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.bluez/${project.version}</bundle> <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.bluez/${project.version}</bundle>
</feature> </feature>

View File

@ -104,10 +104,9 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
this.name = blueZDevice.getName(); this.name = blueZDevice.getName();
Map<UInt16, byte[]> manData = blueZDevice.getManufacturerData(); Map<UInt16, byte[]> manData = blueZDevice.getManufacturerData();
if (manData != null) { if (manData != null) {
manData.entrySet().stream().map(Map.Entry::getKey).filter(Objects::nonNull).findFirst() manData.keySet().stream().filter(Objects::nonNull).findFirst().ifPresent((UInt16 manufacturerId) ->
.ifPresent((UInt16 manufacturerId) -> // Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers this.manufacturer = manufacturerId.intValue() & 0xFFFF);
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
} }
if (Boolean.TRUE.equals(blueZDevice.isConnected())) { if (Boolean.TRUE.equals(blueZDevice.isConnected())) {
@ -208,7 +207,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
private void ensureConnected() { private void ensureConnected() {
BluetoothDevice dev = device; BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) { if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
throw new IllegalStateException("DBusBlueZ device is not set or not connected"); throw new IllegalStateException("DBusBlueZ device is not set or not connected");
} }
} }
@ -265,7 +264,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
@Override @Override
public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) { public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
BluetoothDevice dev = device; BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) { if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected")); .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
} }
@ -301,7 +300,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
logger.debug("writeCharacteristic()"); logger.debug("writeCharacteristic()");
BluetoothDevice dev = device; BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) { if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected")); .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
} }
@ -346,13 +345,13 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
@Override @Override
public void onManufacturerDataUpdate(ManufacturerDataEvent event) { public void onManufacturerDataUpdate(ManufacturerDataEvent event) {
for (Map.Entry<Short, byte[]> entry : event.getData().entrySet()) { event.getData().forEach((key, value) -> {
BluetoothScanNotification notification = new BluetoothScanNotification(); BluetoothScanNotification notification = new BluetoothScanNotification();
byte[] data = new byte[entry.getValue().length + 2]; byte[] data = new byte[value.length + 2];
data[0] = (byte) (entry.getKey() & 0xFF); data[0] = (byte) (key & 0xFF);
data[1] = (byte) (entry.getKey() >>> 8); data[1] = (byte) (key >>> 8);
System.arraycopy(entry.getValue(), 0, data, 2, entry.getValue().length); System.arraycopy(value, 0, data, 2, value.length);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " ")); logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " "));
@ -360,7 +359,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
notification.setManufacturerData(data); notification.setManufacturerData(data);
notifyListeners(BluetoothEventType.SCAN_RECORD, notification); notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
} });
} }
@Override @Override
@ -513,7 +512,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
@Override @Override
public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) { public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
BluetoothDevice dev = device; BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) { if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected")); .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
} }

View File

@ -25,6 +25,7 @@ import org.bluez.exceptions.BluezNotReadyException;
import org.bluez.exceptions.BluezNotSupportedException; import org.bluez.exceptions.BluezNotSupportedException;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.freedesktop.dbus.types.Variant; import org.freedesktop.dbus.types.Variant;
import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler; import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler;
import org.openhab.binding.bluetooth.BluetoothAddress; import org.openhab.binding.bluetooth.BluetoothAddress;
@ -149,10 +150,10 @@ public class BlueZBridgeHandler extends AbstractBluetoothBridgeHandler<BlueZBlue
Map<String, Variant<?>> filter = new HashMap<>(); Map<String, Variant<?>> filter = new HashMap<>();
filter.put("DuplicateData", new Variant<>(true)); filter.put("DuplicateData", new Variant<>(true));
try { try {
adapter.setDiscoveryFilter(filter); localAdapter.setDiscoveryFilter(filter);
} catch (BluezInvalidArgumentsException | BluezFailedException | BluezNotSupportedException } catch (BluezInvalidArgumentsException | BluezFailedException | BluezNotSupportedException
| BluezNotReadyException e) { | BluezNotReadyException e) {
throw new RuntimeException(e); throw new DBusExecutionException("failed to set the discovery filter", e);
} }
// now lets make sure that discovery is turned on // now lets make sure that discovery is turned on
@ -175,14 +176,14 @@ public class BlueZBridgeHandler extends AbstractBluetoothBridgeHandler<BlueZBlue
return; return;
} }
BluetoothAdapter adapter = prepareAdapter(deviceManager); BluetoothAdapter localAdapter = prepareAdapter(deviceManager);
if (adapter == null) { if (localAdapter == null) {
// adapter isn't prepared yet // adapter isn't prepared yet
return; return;
} }
// now lets refresh devices // now lets refresh devices
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(adapter); List<BluetoothDevice> bluezDevices = deviceManager.getDevices(localAdapter);
logger.debug("Found {} Bluetooth devices.", bluezDevices.size()); logger.debug("Found {} Bluetooth devices.", bluezDevices.size());
for (BluetoothDevice bluezDevice : bluezDevices) { for (BluetoothDevice bluezDevice : bluezDevices) {
if (bluezDevice.getAddress() == null) { if (bluezDevice.getAddress() == null) {

View File

@ -20,7 +20,6 @@ import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.freedesktop.dbus.DBusMap;
import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler; import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler;
import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged; import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged;
import org.freedesktop.dbus.types.UInt16; import org.freedesktop.dbus.types.UInt16;
@ -138,102 +137,84 @@ public class BlueZPropertiesChangedHandler extends AbstractPropertiesChangedHand
} }
private void onDiscoveringUpdate(String dbusPath, Variant<?> variant) { private void onDiscoveringUpdate(String dbusPath, Variant<?> variant) {
Object discovered = variant.getValue(); if (variant.getValue() instanceof Boolean discovered) {
if (discovered instanceof Boolean) { notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, discovered));
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, (boolean) discovered));
} }
} }
private void onPoweredUpdate(String dbusPath, Variant<?> variant) { private void onPoweredUpdate(String dbusPath, Variant<?> variant) {
Object powered = variant.getValue(); if (variant.getValue() instanceof Boolean powered) {
if (powered instanceof Boolean) { notifyListeners(new AdapterPoweredChangedEvent(dbusPath, powered));
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, (boolean) powered));
} }
} }
private void onServicesResolved(String dbusPath, Variant<?> variant) { private void onServicesResolved(String dbusPath, Variant<?> variant) {
Object resolved = variant.getValue(); if (variant.getValue() instanceof Boolean resolved) {
if (resolved instanceof Boolean) { notifyListeners(new ServicesResolvedEvent(dbusPath, resolved));
notifyListeners(new ServicesResolvedEvent(dbusPath, (boolean) resolved));
} }
} }
private void onNameUpdate(String dbusPath, Variant<?> variant) { private void onNameUpdate(String dbusPath, Variant<?> variant) {
Object name = variant.getValue(); if (variant.getValue() instanceof String name) {
if (name instanceof String) { notifyListeners(new NameEvent(dbusPath, name));
notifyListeners(new NameEvent(dbusPath, (String) name));
} }
} }
private void onTXPowerUpdate(String dbusPath, Variant<?> variant) { private void onTXPowerUpdate(String dbusPath, Variant<?> variant) {
Object txPower = variant.getValue(); if (variant.getValue() instanceof Short txPower) {
if (txPower instanceof Short) { notifyListeners(new TXPowerEvent(dbusPath, txPower));
notifyListeners(new TXPowerEvent(dbusPath, (short) txPower));
} }
} }
private void onConnectedUpdate(String dbusPath, Variant<?> variant) { private void onConnectedUpdate(String dbusPath, Variant<?> variant) {
Object connected = variant.getValue(); if (variant.getValue() instanceof Boolean connected) {
if (connected instanceof Boolean) { notifyListeners(new ConnectedEvent(dbusPath, connected));
notifyListeners(new ConnectedEvent(dbusPath, (boolean) connected));
} }
} }
private void onManufacturerDataUpdate(String dbusPath, Variant<?> variant) { private void onManufacturerDataUpdate(String dbusPath, Variant<?> variant) {
Map<Short, byte[]> eventData = new HashMap<>(); if (variant.getValue() instanceof Map<?, ?> map) {
Map<Short, byte[]> eventData = new HashMap<>();
Object map = variant.getValue(); map.forEach((key, value) -> {
if (map instanceof DBusMap) { if (key instanceof UInt16 iKey && value instanceof Variant<?> vValue
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map; && vValue.getValue() instanceof byte[] bValue) {
for (Map.Entry<?, ?> entry : dbm.entrySet()) { eventData.put(iKey.shortValue(), bValue);
Object key = entry.getKey();
Object value = entry.getValue();
if (key instanceof UInt16 && value instanceof Variant<?>) {
value = ((Variant<?>) value).getValue();
if (value instanceof byte[]) {
eventData.put(((UInt16) key).shortValue(), ((byte[]) value));
}
} }
});
if (!eventData.isEmpty()) {
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
} }
} }
if (!eventData.isEmpty()) {
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
}
} }
private void onServiceDataUpdate(String dbusPath, Variant<?> variant) { private void onServiceDataUpdate(String dbusPath, Variant<?> variant) {
Map<String, byte[]> serviceData = new HashMap<>(); if (variant.getValue() instanceof Map<?, ?> map) {
Map<String, byte[]> serviceData = new HashMap<>();
Object map = variant.getValue(); map.forEach((key, value) -> {
if (map instanceof DBusMap) { if (key instanceof String sKey && value instanceof Variant<?> vValue
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map; && vValue.getValue() instanceof byte[] bValue) {
for (Map.Entry<?, ?> entry : dbm.entrySet()) { serviceData.put(sKey, bValue);
Object key = entry.getKey();
Object value = entry.getValue();
if (key instanceof String && value instanceof Variant<?>) {
value = ((Variant<?>) value).getValue();
if (value instanceof byte[]) {
serviceData.put(((String) key), ((byte[]) value));
}
} }
});
if (!serviceData.isEmpty()) {
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
} }
} }
if (!serviceData.isEmpty()) {
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
}
} }
private void onValueUpdate(String dbusPath, Variant<?> variant) { private void onValueUpdate(String dbusPath, Variant<?> variant) {
Object value = variant.getValue(); if (variant.getValue() instanceof byte[] bytes) {
if (value instanceof byte[]) { notifyListeners(new CharacteristicUpdateEvent(dbusPath, bytes));
notifyListeners(new CharacteristicUpdateEvent(dbusPath, (byte[]) value));
} }
} }
private void onRSSIUpdate(String dbusPath, Variant<?> variant) { private void onRSSIUpdate(String dbusPath, Variant<?> variant) {
Object rssi = variant.getValue(); if (variant.getValue() instanceof Short rssi) {
if (rssi instanceof Short) { notifyListeners(new RssiEvent(dbusPath, rssi));
notifyListeners(new RssiEvent(dbusPath, (short) rssi));
} }
} }
} }

View File

@ -2,7 +2,7 @@
<feature name="openhab-binding-bluetooth" description="Bluetooth Binding" version="${project.version}"> <feature name="openhab-binding-bluetooth" description="Bluetooth Binding" version="${project.version}">
<feature>openhab-runtime-base</feature> <feature>openhab-runtime-base</feature>
<feature>openhab-transport-serial</feature> <feature>openhab-transport-serial</feature>
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0</bundle> <bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle> <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.airthings/${project.version}</bundle> <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.airthings/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.am43/${project.version}</bundle> <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.am43/${project.version}</bundle>