2 * Copyright (c) 2010-2023 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;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.UUID;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
25 * The {@link DelegateBluetoothDevice} is an abstract parent class for BluetoothDevice implementations
26 * that delegate their functions to other BluetoothDevice instances.
28 * @author Connor Petty - Initial Contribution
31 public abstract class DelegateBluetoothDevice extends BluetoothDevice {
33 public DelegateBluetoothDevice(BluetoothAdapter adapter, BluetoothAddress address) {
34 super(adapter, address);
37 protected abstract @Nullable BluetoothDevice getDelegate();
40 public @Nullable String getName() {
41 BluetoothDevice delegate = getDelegate();
42 return delegate != null ? delegate.getName() : null;
46 public @Nullable Integer getManufacturerId() {
47 BluetoothDevice delegate = getDelegate();
48 return delegate != null ? delegate.getManufacturerId() : null;
52 public @Nullable Integer getRssi() {
53 BluetoothDevice delegate = getDelegate();
54 return delegate != null ? delegate.getRssi() : null;
58 public @Nullable Integer getTxPower() {
59 BluetoothDevice delegate = getDelegate();
60 return delegate != null ? delegate.getTxPower() : null;
64 public @Nullable BluetoothService getServices(UUID uuid) {
65 BluetoothDevice delegate = getDelegate();
66 return delegate != null ? delegate.getServices(uuid) : null;
70 public Collection<BluetoothService> getServices() {
71 BluetoothDevice delegate = getDelegate();
72 return delegate != null ? delegate.getServices() : Collections.emptySet();
76 public boolean supportsService(UUID uuid) {
77 BluetoothDevice delegate = getDelegate();
78 return delegate != null ? delegate.supportsService(uuid) : false;
82 public ConnectionState getConnectionState() {
83 BluetoothDevice delegate = getDelegate();
84 return delegate != null ? delegate.getConnectionState() : ConnectionState.DISCOVERED;
88 public boolean connect() {
89 BluetoothDevice delegate = getDelegate();
90 return delegate != null ? delegate.connect() : false;
94 public boolean disconnect() {
95 BluetoothDevice delegate = getDelegate();
96 return delegate != null ? delegate.disconnect() : false;
100 public boolean discoverServices() {
101 BluetoothDevice delegate = getDelegate();
102 return delegate != null ? delegate.discoverServices() : false;
106 public CompletableFuture<byte[]> readCharacteristic(BluetoothCharacteristic characteristic) {
107 BluetoothDevice delegate = getDelegate();
108 if (delegate == null) {
109 return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
111 return delegate.readCharacteristic(characteristic);
115 public CompletableFuture<@Nullable Void> writeCharacteristic(BluetoothCharacteristic characteristic, byte[] value) {
116 BluetoothDevice delegate = getDelegate();
117 if (delegate == null) {
118 return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
120 return delegate.writeCharacteristic(characteristic, value);
124 public boolean isNotifying(BluetoothCharacteristic characteristic) {
125 BluetoothDevice delegate = getDelegate();
126 return delegate != null ? delegate.isNotifying(characteristic) : false;
130 public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
131 BluetoothDevice delegate = getDelegate();
132 if (delegate == null) {
133 return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
135 return delegate.enableNotifications(characteristic);
139 public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
140 BluetoothDevice delegate = getDelegate();
141 if (delegate == null) {
142 return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
144 return delegate.disableNotifications(characteristic);
148 public boolean enableNotifications(BluetoothDescriptor descriptor) {
149 BluetoothDevice delegate = getDelegate();
150 return delegate != null ? delegate.enableNotifications(descriptor) : false;
154 public boolean disableNotifications(BluetoothDescriptor descriptor) {
155 BluetoothDevice delegate = getDelegate();
156 return delegate != null ? delegate.disableNotifications(descriptor) : false;
160 protected boolean addService(BluetoothService service) {
161 BluetoothDevice delegate = getDelegate();
162 return delegate != null ? delegate.addService(service) : false;
166 protected Collection<BluetoothDeviceListener> getListeners() {
167 BluetoothDevice delegate = getDelegate();
168 return delegate != null ? delegate.getListeners() : Collections.emptySet();
172 public @Nullable BluetoothCharacteristic getCharacteristic(UUID uuid) {
173 BluetoothDevice delegate = getDelegate();
174 return delegate != null ? delegate.getCharacteristic(uuid) : null;
178 public boolean awaitConnection(long timeout, TimeUnit unit) throws InterruptedException {
179 BluetoothDevice delegate = getDelegate();
180 return delegate != null ? delegate.awaitConnection(timeout, unit) : false;
184 public boolean awaitServiceDiscovery(long timeout, TimeUnit unit) throws InterruptedException {
185 BluetoothDevice delegate = getDelegate();
186 return delegate != null ? delegate.awaitServiceDiscovery(timeout, unit) : false;
190 public boolean isServicesDiscovered() {
191 BluetoothDevice delegate = getDelegate();
192 return delegate != null ? delegate.isServicesDiscovered() : false;
196 protected void dispose() {
197 BluetoothDevice delegate = getDelegate();
198 if (delegate != null) {