[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>
<groupId>com.github.hypfvieh</groupId>
<artifactId>bluez-dbus-osgi</artifactId>
<version>0.1.4</version>
<version>0.3.0</version>
<scope>provided</scope>
</dependency>

View File

@ -4,7 +4,7 @@
<feature name="openhab-binding-bluetooth-bluez" description="Bluetooth Binding Bluez" version="${project.version}">
<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.bluez/${project.version}</bundle>
</feature>

View File

@ -104,10 +104,9 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
this.name = blueZDevice.getName();
Map<UInt16, byte[]> manData = blueZDevice.getManufacturerData();
if (manData != null) {
manData.entrySet().stream().map(Map.Entry::getKey).filter(Objects::nonNull).findFirst()
.ifPresent((UInt16 manufacturerId) ->
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
manData.keySet().stream().filter(Objects::nonNull).findFirst().ifPresent((UInt16 manufacturerId) ->
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
}
if (Boolean.TRUE.equals(blueZDevice.isConnected())) {
@ -208,7 +207,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
private void ensureConnected() {
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");
}
}
@ -265,7 +264,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
@Override
public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture
.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()");
BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
}
@ -346,13 +345,13 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
@Override
public void onManufacturerDataUpdate(ManufacturerDataEvent event) {
for (Map.Entry<Short, byte[]> entry : event.getData().entrySet()) {
event.getData().forEach((key, value) -> {
BluetoothScanNotification notification = new BluetoothScanNotification();
byte[] data = new byte[entry.getValue().length + 2];
data[0] = (byte) (entry.getKey() & 0xFF);
data[1] = (byte) (entry.getKey() >>> 8);
byte[] data = new byte[value.length + 2];
data[0] = (byte) (key & 0xFF);
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()) {
logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " "));
@ -360,7 +359,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
notification.setManufacturerData(data);
notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
}
});
}
@Override
@ -513,7 +512,7 @@ public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEv
@Override
public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
BluetoothDevice dev = device;
if (dev == null || !dev.isConnected()) {
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
return CompletableFuture
.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.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.freedesktop.dbus.types.Variant;
import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler;
import org.openhab.binding.bluetooth.BluetoothAddress;
@ -149,10 +150,10 @@ public class BlueZBridgeHandler extends AbstractBluetoothBridgeHandler<BlueZBlue
Map<String, Variant<?>> filter = new HashMap<>();
filter.put("DuplicateData", new Variant<>(true));
try {
adapter.setDiscoveryFilter(filter);
localAdapter.setDiscoveryFilter(filter);
} catch (BluezInvalidArgumentsException | BluezFailedException | BluezNotSupportedException
| 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
@ -175,14 +176,14 @@ public class BlueZBridgeHandler extends AbstractBluetoothBridgeHandler<BlueZBlue
return;
}
BluetoothAdapter adapter = prepareAdapter(deviceManager);
if (adapter == null) {
BluetoothAdapter localAdapter = prepareAdapter(deviceManager);
if (localAdapter == null) {
// adapter isn't prepared yet
return;
}
// now lets refresh devices
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(adapter);
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(localAdapter);
logger.debug("Found {} Bluetooth devices.", bluezDevices.size());
for (BluetoothDevice bluezDevice : bluezDevices) {
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.Nullable;
import org.freedesktop.dbus.DBusMap;
import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler;
import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged;
import org.freedesktop.dbus.types.UInt16;
@ -138,102 +137,84 @@ public class BlueZPropertiesChangedHandler extends AbstractPropertiesChangedHand
}
private void onDiscoveringUpdate(String dbusPath, Variant<?> variant) {
Object discovered = variant.getValue();
if (discovered instanceof Boolean) {
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, (boolean) discovered));
if (variant.getValue() instanceof Boolean discovered) {
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, discovered));
}
}
private void onPoweredUpdate(String dbusPath, Variant<?> variant) {
Object powered = variant.getValue();
if (powered instanceof Boolean) {
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, (boolean) powered));
if (variant.getValue() instanceof Boolean powered) {
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, powered));
}
}
private void onServicesResolved(String dbusPath, Variant<?> variant) {
Object resolved = variant.getValue();
if (resolved instanceof Boolean) {
notifyListeners(new ServicesResolvedEvent(dbusPath, (boolean) resolved));
if (variant.getValue() instanceof Boolean resolved) {
notifyListeners(new ServicesResolvedEvent(dbusPath, resolved));
}
}
private void onNameUpdate(String dbusPath, Variant<?> variant) {
Object name = variant.getValue();
if (name instanceof String) {
notifyListeners(new NameEvent(dbusPath, (String) name));
if (variant.getValue() instanceof String name) {
notifyListeners(new NameEvent(dbusPath, name));
}
}
private void onTXPowerUpdate(String dbusPath, Variant<?> variant) {
Object txPower = variant.getValue();
if (txPower instanceof Short) {
notifyListeners(new TXPowerEvent(dbusPath, (short) txPower));
if (variant.getValue() instanceof Short txPower) {
notifyListeners(new TXPowerEvent(dbusPath, txPower));
}
}
private void onConnectedUpdate(String dbusPath, Variant<?> variant) {
Object connected = variant.getValue();
if (connected instanceof Boolean) {
notifyListeners(new ConnectedEvent(dbusPath, (boolean) connected));
if (variant.getValue() instanceof Boolean connected) {
notifyListeners(new ConnectedEvent(dbusPath, connected));
}
}
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();
if (map instanceof DBusMap) {
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map;
for (Map.Entry<?, ?> entry : dbm.entrySet()) {
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));
}
map.forEach((key, value) -> {
if (key instanceof UInt16 iKey && value instanceof Variant<?> vValue
&& vValue.getValue() instanceof byte[] bValue) {
eventData.put(iKey.shortValue(), bValue);
}
});
if (!eventData.isEmpty()) {
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
}
}
if (!eventData.isEmpty()) {
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
}
}
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();
if (map instanceof DBusMap) {
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map;
for (Map.Entry<?, ?> entry : dbm.entrySet()) {
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));
}
map.forEach((key, value) -> {
if (key instanceof String sKey && value instanceof Variant<?> vValue
&& vValue.getValue() instanceof byte[] bValue) {
serviceData.put(sKey, bValue);
}
});
if (!serviceData.isEmpty()) {
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
}
}
if (!serviceData.isEmpty()) {
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
}
}
private void onValueUpdate(String dbusPath, Variant<?> variant) {
Object value = variant.getValue();
if (value instanceof byte[]) {
notifyListeners(new CharacteristicUpdateEvent(dbusPath, (byte[]) value));
if (variant.getValue() instanceof byte[] bytes) {
notifyListeners(new CharacteristicUpdateEvent(dbusPath, bytes));
}
}
private void onRSSIUpdate(String dbusPath, Variant<?> variant) {
Object rssi = variant.getValue();
if (rssi instanceof Short) {
notifyListeners(new RssiEvent(dbusPath, (short) rssi));
if (variant.getValue() instanceof Short rssi) {
notifyListeners(new RssiEvent(dbusPath, rssi));
}
}
}

View File

@ -2,7 +2,7 @@
<feature name="openhab-binding-bluetooth" description="Bluetooth Binding" version="${project.version}">
<feature>openhab-runtime-base</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.airthings/${project.version}</bundle>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.am43/${project.version}</bundle>