2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.bluez.internal;
16 import java.util.Objects;
17 import java.util.UUID;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.freedesktop.dbus.errors.NoReply;
25 import org.freedesktop.dbus.exceptions.DBusException;
26 import org.freedesktop.dbus.exceptions.DBusExecutionException;
27 import org.freedesktop.dbus.types.UInt16;
28 import org.openhab.binding.bluetooth.BaseBluetoothDevice;
29 import org.openhab.binding.bluetooth.BluetoothAddress;
30 import org.openhab.binding.bluetooth.BluetoothCharacteristic;
31 import org.openhab.binding.bluetooth.BluetoothDescriptor;
32 import org.openhab.binding.bluetooth.BluetoothService;
33 import org.openhab.binding.bluetooth.bluez.internal.events.BlueZEvent;
34 import org.openhab.binding.bluetooth.bluez.internal.events.BlueZEventListener;
35 import org.openhab.binding.bluetooth.bluez.internal.events.CharacteristicUpdateEvent;
36 import org.openhab.binding.bluetooth.bluez.internal.events.ConnectedEvent;
37 import org.openhab.binding.bluetooth.bluez.internal.events.ManufacturerDataEvent;
38 import org.openhab.binding.bluetooth.bluez.internal.events.NameEvent;
39 import org.openhab.binding.bluetooth.bluez.internal.events.RssiEvent;
40 import org.openhab.binding.bluetooth.bluez.internal.events.ServicesResolvedEvent;
41 import org.openhab.binding.bluetooth.bluez.internal.events.TXPowerEvent;
42 import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
43 import org.openhab.binding.bluetooth.notification.BluetoothScanNotification;
44 import org.openhab.binding.bluetooth.util.RetryException;
45 import org.openhab.binding.bluetooth.util.RetryFuture;
46 import org.openhab.core.common.ThreadPoolManager;
47 import org.openhab.core.util.HexUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.github.hypfvieh.bluetooth.wrapper.BluetoothDevice;
52 import com.github.hypfvieh.bluetooth.wrapper.BluetoothGattCharacteristic;
53 import com.github.hypfvieh.bluetooth.wrapper.BluetoothGattDescriptor;
54 import com.github.hypfvieh.bluetooth.wrapper.BluetoothGattService;
57 * Implementation of BluetoothDevice for BlueZ via DBus-BlueZ API
59 * @author Kai Kreuzer - Initial contribution and API
60 * @author Benjamin Lafois - Replaced tinyB with bluezDbus
61 * @author Peter Rosenberg - Improve notifications and properties support
65 public class BlueZBluetoothDevice extends BaseBluetoothDevice implements BlueZEventListener {
67 private final Logger logger = LoggerFactory.getLogger(BlueZBluetoothDevice.class);
69 private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool("bluetooth");
71 // Device from native lib
72 private @Nullable BluetoothDevice device = null;
77 * @param adapter the bridge handler through which this device is connected
78 * @param address the Bluetooth address of the device
79 * @param name the name of the device
81 public BlueZBluetoothDevice(BlueZBridgeHandler adapter, BluetoothAddress address) {
82 super(adapter, address);
83 logger.debug("Creating DBusBlueZ device with address '{}'", address);
86 public synchronized void updateBlueZDevice(@Nullable BluetoothDevice blueZDevice) {
87 if (this.device != null && this.device == blueZDevice) {
90 logger.debug("updateBlueZDevice({})", blueZDevice);
92 this.device = blueZDevice;
94 if (blueZDevice == null) {
98 Short rssi = blueZDevice.getRssi();
100 this.rssi = rssi.intValue();
102 this.name = blueZDevice.getName();
103 Map<UInt16, byte[]> manData = blueZDevice.getManufacturerData();
104 if (manData != null) {
105 manData.entrySet().stream().map(Map.Entry::getKey).filter(Objects::nonNull).findFirst()
106 .ifPresent((UInt16 manufacturerId) ->
107 // Convert to unsigned int to match the convention in BluetoothCompanyIdentifiers
108 this.manufacturer = manufacturerId.intValue() & 0xFFFF);
111 if (Boolean.TRUE.equals(blueZDevice.isConnected())) {
112 setConnectionState(ConnectionState.CONNECTED);
119 * Clean up and release memory.
122 public void dispose() {
123 BluetoothDevice dev = device;
125 if (Boolean.TRUE.equals(dev.isPaired())) {
130 dev.getAdapter().removeDevice(dev.getRawDevice());
131 } catch (DBusException ex) {
132 if (ex.getMessage().contains("Does Not Exist")) {
133 // this happens when the underlying device has already been removed
134 // but we don't have a way to check if that is the case beforehand so
135 // we will just eat the error here.
137 logger.debug("Exception occurred when trying to remove inactive device '{}': {}", address,
140 } catch (RuntimeException ex) {
141 // try to catch any other exceptions
142 logger.debug("Exception occurred when trying to remove inactive device '{}': {}", address,
148 private void setConnectionState(ConnectionState state) {
149 if (this.connectionState != state) {
150 this.connectionState = state;
151 notifyListeners(BluetoothEventType.CONNECTION_STATE, new BluetoothConnectionStatusNotification(state));
156 public boolean connect() {
157 logger.debug("Connect({})", device);
159 BluetoothDevice dev = device;
161 if (Boolean.FALSE.equals(dev.isConnected())) {
163 boolean ret = dev.connect();
164 logger.debug("Connect result: {}", ret);
166 } catch (NoReply e) {
167 // Have to double check because sometimes, exception but still worked
168 logger.debug("Got a timeout - but sometimes happen. Is Connected ? {}", dev.isConnected());
169 if (Boolean.FALSE.equals(dev.isConnected())) {
171 notifyListeners(BluetoothEventType.CONNECTION_STATE,
172 new BluetoothConnectionStatusNotification(ConnectionState.DISCONNECTED));
177 } catch (DBusExecutionException e) {
178 // Catch "software caused connection abort"
180 } catch (Exception e) {
181 logger.warn("error occured while trying to connect", e);
185 logger.debug("Device was already connected");
186 // we might be stuck in another state atm so we need to trigger a connected in this case
187 setConnectionState(ConnectionState.CONNECTED);
195 public boolean disconnect() {
196 BluetoothDevice dev = device;
198 logger.debug("Disconnecting '{}'", address);
199 return dev.disconnect();
204 private void ensureConnected() {
205 BluetoothDevice dev = device;
206 if (dev == null || !dev.isConnected()) {
207 throw new IllegalStateException("DBusBlueZ device is not set or not connected");
211 private @Nullable BluetoothGattCharacteristic getDBusBlueZCharacteristicByUUID(String uuid) {
212 BluetoothDevice dev = device;
216 for (BluetoothGattService service : dev.getGattServices()) {
217 for (BluetoothGattCharacteristic c : service.getGattCharacteristics()) {
218 if (c.getUuid().equalsIgnoreCase(uuid)) {
226 private @Nullable BluetoothGattCharacteristic getDBusBlueZCharacteristicByDBusPath(String dBusPath) {
227 BluetoothDevice dev = device;
231 for (BluetoothGattService service : dev.getGattServices()) {
232 if (dBusPath.startsWith(service.getDbusPath())) {
233 for (BluetoothGattCharacteristic characteristic : service.getGattCharacteristics()) {
234 if (dBusPath.startsWith(characteristic.getDbusPath())) {
235 return characteristic;
243 private @Nullable BluetoothGattDescriptor getDBusBlueZDescriptorByUUID(String uuid) {
244 BluetoothDevice dev = device;
248 for (BluetoothGattService service : dev.getGattServices()) {
249 for (BluetoothGattCharacteristic c : service.getGattCharacteristics()) {
250 for (BluetoothGattDescriptor d : c.getGattDescriptors()) {
251 if (d.getUuid().equalsIgnoreCase(uuid)) {
261 public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
262 BluetoothDevice dev = device;
263 if (dev == null || !dev.isConnected()) {
264 return CompletableFuture
265 .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
268 BluetoothGattCharacteristic c = getDBusBlueZCharacteristicByUUID(characteristic.getUuid().toString());
270 logger.warn("Characteristic '{}' is missing on device '{}'.", characteristic.getUuid(), address);
271 return CompletableFuture.failedFuture(
272 new IllegalStateException("Characteristic " + characteristic.getUuid() + " is missing on device"));
275 return RetryFuture.callWithRetry(() -> {
278 } catch (DBusException e) {
279 if (e.getMessage().contains("Already notifying")) {
281 } else if (e.getMessage().contains("In Progress")) {
282 // let's retry in half a second
283 throw new RetryException(500, TimeUnit.MILLISECONDS);
285 logger.warn("Exception occurred while activating notifications on '{}'", address, e);
294 public CompletableFuture<@Nullable Void> writeCharacteristic(BluetoothCharacteristic characteristic, byte[] value) {
295 logger.debug("writeCharacteristic()");
297 BluetoothDevice dev = device;
298 if (dev == null || !dev.isConnected()) {
299 return CompletableFuture
300 .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
303 BluetoothGattCharacteristic c = getDBusBlueZCharacteristicByUUID(characteristic.getUuid().toString());
305 logger.warn("Characteristic '{}' is missing on device '{}'.", characteristic.getUuid(), address);
306 return CompletableFuture.failedFuture(
307 new IllegalStateException("Characteristic " + characteristic.getUuid() + " is missing on device"));
310 return RetryFuture.callWithRetry(() -> {
312 c.writeValue(value, null);
314 } catch (DBusException e) {
315 logger.debug("Exception occurred when trying to write characteristic '{}': {}",
316 characteristic.getUuid(), e.getMessage());
323 public void onDBusBlueZEvent(BlueZEvent event) {
324 logger.debug("Unsupported event: {}", event);
328 public void onServicesResolved(ServicesResolvedEvent event) {
329 if (event.isResolved()) {
330 notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
335 public void onNameUpdate(NameEvent event) {
336 BluetoothScanNotification notification = new BluetoothScanNotification();
337 notification.setDeviceName(event.getName());
338 notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
342 public void onManufacturerDataUpdate(ManufacturerDataEvent event) {
343 for (Map.Entry<Short, byte[]> entry : event.getData().entrySet()) {
344 BluetoothScanNotification notification = new BluetoothScanNotification();
345 byte[] data = new byte[entry.getValue().length + 2];
346 data[0] = (byte) (entry.getKey() & 0xFF);
347 data[1] = (byte) (entry.getKey() >>> 8);
349 System.arraycopy(entry.getValue(), 0, data, 2, entry.getValue().length);
351 if (logger.isDebugEnabled()) {
352 logger.debug("Received manufacturer data for '{}': {}", address, HexUtils.bytesToHex(data, " "));
355 notification.setManufacturerData(data);
356 notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
361 public void onTxPowerUpdate(TXPowerEvent event) {
362 this.txPower = (int) event.getTxPower();
366 public void onCharacteristicNotify(CharacteristicUpdateEvent event) {
367 // Here it is a bit special - as the event is linked to the DBUS path, not characteristic UUID.
368 // So we need to find the characteristic by its DBUS path.
369 BluetoothGattCharacteristic characteristic = getDBusBlueZCharacteristicByDBusPath(event.getDbusPath());
370 if (characteristic == null) {
371 logger.debug("Received a notification for a characteristic not found on device.");
374 BluetoothCharacteristic c = getCharacteristic(UUID.fromString(characteristic.getUuid()));
376 notifyListeners(BluetoothEventType.CHARACTERISTIC_UPDATED, c, event.getData());
381 public void onRssiUpdate(RssiEvent event) {
382 int rssiTmp = event.getRssi();
384 BluetoothScanNotification notification = new BluetoothScanNotification();
385 notification.setRssi(rssiTmp);
386 notifyListeners(BluetoothEventType.SCAN_RECORD, notification);
390 public void onConnectedStatusUpdate(ConnectedEvent event) {
391 this.connectionState = event.isConnected() ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
392 notifyListeners(BluetoothEventType.CONNECTION_STATE,
393 new BluetoothConnectionStatusNotification(connectionState));
397 public boolean discoverServices() {
398 BluetoothDevice dev = device;
402 if (dev.getGattServices().size() > getServices().size()) {
403 for (BluetoothGattService dBusBlueZService : dev.getGattServices()) {
404 BluetoothService service = new BluetoothService(UUID.fromString(dBusBlueZService.getUuid()),
405 dBusBlueZService.isPrimary());
406 for (BluetoothGattCharacteristic dBusBlueZCharacteristic : dBusBlueZService.getGattCharacteristics()) {
407 BluetoothCharacteristic characteristic = new BluetoothCharacteristic(
408 UUID.fromString(dBusBlueZCharacteristic.getUuid()), 0);
409 convertCharacteristicProperties(dBusBlueZCharacteristic, characteristic);
411 for (BluetoothGattDescriptor dBusBlueZDescriptor : dBusBlueZCharacteristic.getGattDescriptors()) {
412 BluetoothDescriptor descriptor = new BluetoothDescriptor(characteristic,
413 UUID.fromString(dBusBlueZDescriptor.getUuid()), 0);
414 characteristic.addDescriptor(descriptor);
416 service.addCharacteristic(characteristic);
420 notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
426 * Convert the flags of BluetoothGattCharacteristic to the int bitset used by BluetoothCharacteristic.
428 * @param dBusBlueZCharacteristic source characteristic to read the flags from
429 * @param characteristic destination characteristic to write to properties to
431 private void convertCharacteristicProperties(BluetoothGattCharacteristic dBusBlueZCharacteristic,
432 BluetoothCharacteristic characteristic) {
435 for (String property : dBusBlueZCharacteristic.getFlags()) {
438 properties |= BluetoothCharacteristic.PROPERTY_BROADCAST;
441 properties |= BluetoothCharacteristic.PROPERTY_READ;
443 case "write-without-response":
444 properties |= BluetoothCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
447 properties |= BluetoothCharacteristic.PROPERTY_WRITE;
450 properties |= BluetoothCharacteristic.PROPERTY_NOTIFY;
453 properties |= BluetoothCharacteristic.PROPERTY_INDICATE;
458 characteristic.setProperties(properties);
462 public CompletableFuture<byte[]> readCharacteristic(BluetoothCharacteristic characteristic) {
463 BluetoothDevice dev = device;
464 if (dev == null || !dev.isConnected()) {
465 return CompletableFuture
466 .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
469 BluetoothGattCharacteristic c = getDBusBlueZCharacteristicByUUID(characteristic.getUuid().toString());
471 logger.warn("Characteristic '{}' is missing on device '{}'.", characteristic.getUuid(), address);
472 return CompletableFuture.failedFuture(
473 new IllegalStateException("Characteristic " + characteristic.getUuid() + " is missing on device"));
476 return RetryFuture.callWithRetry(() -> {
478 return c.readValue(null);
479 } catch (DBusException | DBusExecutionException e) {
480 // DBusExecutionException is thrown if the value cannot be read
481 logger.debug("Exception occurred when trying to read characteristic '{}': {}", characteristic.getUuid(),
489 public boolean isNotifying(BluetoothCharacteristic characteristic) {
490 BluetoothGattCharacteristic c = getDBusBlueZCharacteristicByUUID(characteristic.getUuid().toString());
492 Boolean isNotifying = c.isNotifying();
493 return Objects.requireNonNullElse(isNotifying, false);
495 logger.warn("Characteristic '{}' is missing on device '{}'.", characteristic.getUuid(), address);
501 public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
502 BluetoothDevice dev = device;
503 if (dev == null || !dev.isConnected()) {
504 return CompletableFuture
505 .failedFuture(new IllegalStateException("DBusBlueZ device is not set or not connected"));
507 BluetoothGattCharacteristic c = getDBusBlueZCharacteristicByUUID(characteristic.getUuid().toString());
509 logger.warn("Characteristic '{}' is missing on device '{}'.", characteristic.getUuid(), address);
510 return CompletableFuture.failedFuture(
511 new IllegalStateException("Characteristic " + characteristic.getUuid() + " is missing on device"));
514 return RetryFuture.callWithRetry(() -> {
517 } catch (DBusException e) {
518 if (e.getMessage().contains("Already notifying")) {
520 } else if (e.getMessage().contains("In Progress")) {
521 // let's retry in half a second
522 throw new RetryException(500, TimeUnit.MILLISECONDS);
524 logger.warn("Exception occurred while deactivating notifications on '{}'", address, e);
533 public boolean enableNotifications(BluetoothDescriptor descriptor) {
534 // Not sure if it is possible to implement this
539 public boolean disableNotifications(BluetoothDescriptor descriptor) {
540 // Not sure if it is possible to implement this