Skip to content

Commit 241829d

Browse files
authored
[bluetooth.bluez] update to dbus version 0.3.0 (openhab#18124)
Signed-off-by: Jörg Sautter <[email protected]>
1 parent 0b3383b commit 241829d

File tree

6 files changed

+60
-79
lines changed

6 files changed

+60
-79
lines changed

bundles/org.openhab.binding.bluetooth.bluez/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.github.hypfvieh</groupId>
2828
<artifactId>bluez-dbus-osgi</artifactId>
29-
<version>0.1.4</version>
29+
<version>0.3.0</version>
3030
<scope>provided</scope>
3131
</dependency>
3232

bundles/org.openhab.binding.bluetooth.bluez/src/main/feature/feature.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBluetoothDevice.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ public synchronized void updateBlueZDevice(@Nullable BluetoothDevice blueZDevice
104104
this.name = blueZDevice.getName();
105105
Map<UInt16, byte[]> manData = blueZDevice.getManufacturerData();
106106
if (manData != null) {
107-
manData.entrySet().stream().map(Map.Entry::getKey).filter(Objects::nonNull).findFirst()
108-
.ifPresent((UInt16 manufacturerId) ->
109-
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
110-
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
107+
manData.keySet().stream().filter(Objects::nonNull).findFirst().ifPresent((UInt16 manufacturerId) ->
108+
// Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
109+
this.manufacturer = manufacturerId.intValue() & 0xFFFF);
111110
}
112111

113112
if (Boolean.TRUE.equals(blueZDevice.isConnected())) {
@@ -208,7 +207,7 @@ public boolean disconnect() {
208207

209208
private void ensureConnected() {
210209
BluetoothDevice dev = device;
211-
if (dev == null || !dev.isConnected()) {
210+
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
212211
throw new IllegalStateException("DBusBlueZ device is not set or not connected");
213212
}
214213
}
@@ -265,7 +264,7 @@ private void ensureConnected() {
265264
@Override
266265
public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
267266
BluetoothDevice dev = device;
268-
if (dev == null || !dev.isConnected()) {
267+
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
269268
return CompletableFuture
270269
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
271270
}
@@ -301,7 +300,7 @@ private void ensureConnected() {
301300
logger.debug("writeCharacteristic()");
302301

303302
BluetoothDevice dev = device;
304-
if (dev == null || !dev.isConnected()) {
303+
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
305304
return CompletableFuture
306305
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
307306
}
@@ -346,21 +345,21 @@ public void onNameUpdate(NameEvent event) {
346345

347346
@Override
348347
public void onManufacturerDataUpdate(ManufacturerDataEvent event) {
349-
for (Map.Entry<Short, byte[]> entry : event.getData().entrySet()) {
348+
event.getData().forEach((key, value) -> {
350349
BluetoothScanNotification notification = new BluetoothScanNotification();
351-
byte[] data = new byte[entry.getValue().length + 2];
352-
data[0] = (byte) (entry.getKey() & 0xFF);
353-
data[1] = (byte) (entry.getKey() >>> 8);
350+
byte[] data = new byte[value.length + 2];
351+
data[0] = (byte) (key & 0xFF);
352+
data[1] = (byte) (key >>> 8);
354353

355-
System.arraycopy(entry.getValue(), 0, data, 2, entry.getValue().length);
354+
System.arraycopy(value, 0, data, 2, value.length);
356355

357356
if (logger.isDebugEnabled()) {
358357
logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " "));
359358
}
360359

361360
notification.setManufacturerData(data);
362361
notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
363-
}
362+
});
364363
}
365364

366365
@Override
@@ -513,7 +512,7 @@ public boolean isNotifying(BluetoothCharacteristic characteristic) {
513512
@Override
514513
public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
515514
BluetoothDevice dev = device;
516-
if (dev == null || !dev.isConnected()) {
515+
if (dev == null || Boolean.FALSE.equals(dev.isConnected())) {
517516
return CompletableFuture
518517
.failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
519518
}

bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZBridgeHandler.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bluez.exceptions.BluezNotSupportedException;
2626
import org.eclipse.jdt.annotation.NonNullByDefault;
2727
import org.eclipse.jdt.annotation.Nullable;
28+
import org.freedesktop.dbus.exceptions.DBusExecutionException;
2829
import org.freedesktop.dbus.types.Variant;
2930
import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler;
3031
import org.openhab.binding.bluetooth.BluetoothAddress;
@@ -149,10 +150,10 @@ public void dispose() {
149150
Map<String, Variant<?>> filter = new HashMap<>();
150151
filter.put("DuplicateData", new Variant<>(true));
151152
try {
152-
adapter.setDiscoveryFilter(filter);
153+
localAdapter.setDiscoveryFilter(filter);
153154
} catch (BluezInvalidArgumentsException | BluezFailedException | BluezNotSupportedException
154155
| BluezNotReadyException e) {
155-
throw new RuntimeException(e);
156+
throw new DBusExecutionException("failed to set the discovery filter", e);
156157
}
157158

158159
// now lets make sure that discovery is turned on
@@ -175,14 +176,14 @@ private void initializeAndRefreshDevices() {
175176
return;
176177
}
177178

178-
BluetoothAdapter adapter = prepareAdapter(deviceManager);
179-
if (adapter == null) {
179+
BluetoothAdapter localAdapter = prepareAdapter(deviceManager);
180+
if (localAdapter == null) {
180181
// adapter isn't prepared yet
181182
return;
182183
}
183184

184185
// now lets refresh devices
185-
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(adapter);
186+
List<BluetoothDevice> bluezDevices = deviceManager.getDevices(localAdapter);
186187
logger.debug("Found {} Bluetooth devices.", bluezDevices.size());
187188
for (BluetoothDevice bluezDevice : bluezDevices) {
188189
if (bluezDevice.getAddress() == null) {

bundles/org.openhab.binding.bluetooth.bluez/src/main/java/org/openhab/binding/bluetooth/bluez/internal/BlueZPropertiesChangedHandler.java

+38-57
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.eclipse.jdt.annotation.NonNullByDefault;
2222
import org.eclipse.jdt.annotation.Nullable;
23-
import org.freedesktop.dbus.DBusMap;
2423
import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler;
2524
import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged;
2625
import org.freedesktop.dbus.types.UInt16;
@@ -138,102 +137,84 @@ public void handle(@Nullable PropertiesChanged properties) {
138137
}
139138

140139
private void onDiscoveringUpdate(String dbusPath, Variant<?> variant) {
141-
Object discovered = variant.getValue();
142-
if (discovered instanceof Boolean) {
143-
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, (boolean) discovered));
140+
if (variant.getValue() instanceof Boolean discovered) {
141+
notifyListeners(new AdapterDiscoveringChangedEvent(dbusPath, discovered));
144142
}
145143
}
146144

147145
private void onPoweredUpdate(String dbusPath, Variant<?> variant) {
148-
Object powered = variant.getValue();
149-
if (powered instanceof Boolean) {
150-
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, (boolean) powered));
146+
if (variant.getValue() instanceof Boolean powered) {
147+
notifyListeners(new AdapterPoweredChangedEvent(dbusPath, powered));
151148
}
152149
}
153150

154151
private void onServicesResolved(String dbusPath, Variant<?> variant) {
155-
Object resolved = variant.getValue();
156-
if (resolved instanceof Boolean) {
157-
notifyListeners(new ServicesResolvedEvent(dbusPath, (boolean) resolved));
152+
if (variant.getValue() instanceof Boolean resolved) {
153+
notifyListeners(new ServicesResolvedEvent(dbusPath, resolved));
158154
}
159155
}
160156

161157
private void onNameUpdate(String dbusPath, Variant<?> variant) {
162-
Object name = variant.getValue();
163-
if (name instanceof String) {
164-
notifyListeners(new NameEvent(dbusPath, (String) name));
158+
if (variant.getValue() instanceof String name) {
159+
notifyListeners(new NameEvent(dbusPath, name));
165160
}
166161
}
167162

168163
private void onTXPowerUpdate(String dbusPath, Variant<?> variant) {
169-
Object txPower = variant.getValue();
170-
if (txPower instanceof Short) {
171-
notifyListeners(new TXPowerEvent(dbusPath, (short) txPower));
164+
if (variant.getValue() instanceof Short txPower) {
165+
notifyListeners(new TXPowerEvent(dbusPath, txPower));
172166
}
173167
}
174168

175169
private void onConnectedUpdate(String dbusPath, Variant<?> variant) {
176-
Object connected = variant.getValue();
177-
if (connected instanceof Boolean) {
178-
notifyListeners(new ConnectedEvent(dbusPath, (boolean) connected));
170+
if (variant.getValue() instanceof Boolean connected) {
171+
notifyListeners(new ConnectedEvent(dbusPath, connected));
179172
}
180173
}
181174

182175
private void onManufacturerDataUpdate(String dbusPath, Variant<?> variant) {
183-
Map<Short, byte[]> eventData = new HashMap<>();
184-
185-
Object map = variant.getValue();
186-
if (map instanceof DBusMap) {
187-
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map;
188-
for (Map.Entry<?, ?> entry : dbm.entrySet()) {
189-
Object key = entry.getKey();
190-
Object value = entry.getValue();
191-
if (key instanceof UInt16 && value instanceof Variant<?>) {
192-
value = ((Variant<?>) value).getValue();
193-
if (value instanceof byte[]) {
194-
eventData.put(((UInt16) key).shortValue(), ((byte[]) value));
195-
}
176+
if (variant.getValue() instanceof Map<?, ?> map) {
177+
Map<Short, byte[]> eventData = new HashMap<>();
178+
179+
map.forEach((key, value) -> {
180+
if (key instanceof UInt16 iKey && value instanceof Variant<?> vValue
181+
&& vValue.getValue() instanceof byte[] bValue) {
182+
eventData.put(iKey.shortValue(), bValue);
196183
}
184+
});
185+
186+
if (!eventData.isEmpty()) {
187+
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
197188
}
198189
}
199-
if (!eventData.isEmpty()) {
200-
notifyListeners(new ManufacturerDataEvent(dbusPath, eventData));
201-
}
202190
}
203191

204192
private void onServiceDataUpdate(String dbusPath, Variant<?> variant) {
205-
Map<String, byte[]> serviceData = new HashMap<>();
206-
207-
Object map = variant.getValue();
208-
if (map instanceof DBusMap) {
209-
DBusMap<?, ?> dbm = (DBusMap<?, ?>) map;
210-
for (Map.Entry<?, ?> entry : dbm.entrySet()) {
211-
Object key = entry.getKey();
212-
Object value = entry.getValue();
213-
if (key instanceof String && value instanceof Variant<?>) {
214-
value = ((Variant<?>) value).getValue();
215-
if (value instanceof byte[]) {
216-
serviceData.put(((String) key), ((byte[]) value));
217-
}
193+
if (variant.getValue() instanceof Map<?, ?> map) {
194+
Map<String, byte[]> serviceData = new HashMap<>();
195+
196+
map.forEach((key, value) -> {
197+
if (key instanceof String sKey && value instanceof Variant<?> vValue
198+
&& vValue.getValue() instanceof byte[] bValue) {
199+
serviceData.put(sKey, bValue);
218200
}
201+
});
202+
203+
if (!serviceData.isEmpty()) {
204+
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
219205
}
220206
}
221-
if (!serviceData.isEmpty()) {
222-
notifyListeners(new ServiceDataEvent(dbusPath, serviceData));
223-
}
224207
}
225208

226209
private void onValueUpdate(String dbusPath, Variant<?> variant) {
227-
Object value = variant.getValue();
228-
if (value instanceof byte[]) {
229-
notifyListeners(new CharacteristicUpdateEvent(dbusPath, (byte[]) value));
210+
if (variant.getValue() instanceof byte[] bytes) {
211+
notifyListeners(new CharacteristicUpdateEvent(dbusPath, bytes));
230212
}
231213
}
232214

233215
private void onRSSIUpdate(String dbusPath, Variant<?> variant) {
234-
Object rssi = variant.getValue();
235-
if (rssi instanceof Short) {
236-
notifyListeners(new RssiEvent(dbusPath, (short) rssi));
216+
if (variant.getValue() instanceof Short rssi) {
217+
notifyListeners(new RssiEvent(dbusPath, rssi));
237218
}
238219
}
239220
}

features/openhab-addons/src/main/resources/footer.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<feature name="openhab-binding-bluetooth" description="Bluetooth Binding" version="${project.version}">
33
<feature>openhab-runtime-base</feature>
44
<feature>openhab-transport-serial</feature>
5-
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.2.0</bundle>
5+
<bundle dependency="true">mvn:com.github.hypfvieh/bluez-dbus-osgi/0.3.0</bundle>
66
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle>
77
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.airthings/${project.version}</bundle>
88
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.am43/${project.version}</bundle>

0 commit comments

Comments
 (0)