2 * Copyright (c) 2010-2020 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;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
23 * The {@link DelegateBluetoothDevice} is an abstract parent class for BluetoothDevice implementations
24 * that delegate their functions to other BluetoothDevice instances.
26 * @author Connor Petty - Initial Contribution
29 public abstract class DelegateBluetoothDevice extends BluetoothDevice {
31 public DelegateBluetoothDevice(BluetoothAdapter adapter, BluetoothAddress address) {
32 super(adapter, address);
35 protected abstract @Nullable BluetoothDevice getDelegate();
38 public @Nullable String getName() {
39 BluetoothDevice delegate = getDelegate();
40 return delegate != null ? delegate.getName() : null;
44 public @Nullable Integer getManufacturerId() {
45 BluetoothDevice delegate = getDelegate();
46 return delegate != null ? delegate.getManufacturerId() : null;
50 public @Nullable Integer getRssi() {
51 BluetoothDevice delegate = getDelegate();
52 return delegate != null ? delegate.getRssi() : null;
56 public @Nullable Integer getTxPower() {
57 BluetoothDevice delegate = getDelegate();
58 return delegate != null ? delegate.getTxPower() : null;
62 public @Nullable BluetoothService getServices(UUID uuid) {
63 BluetoothDevice delegate = getDelegate();
64 return delegate != null ? delegate.getServices(uuid) : null;
68 public Collection<BluetoothService> getServices() {
69 BluetoothDevice delegate = getDelegate();
70 return delegate != null ? delegate.getServices() : Collections.emptySet();
74 public boolean supportsService(UUID uuid) {
75 BluetoothDevice delegate = getDelegate();
76 return delegate != null ? delegate.supportsService(uuid) : false;
80 public ConnectionState getConnectionState() {
81 BluetoothDevice delegate = getDelegate();
82 return delegate != null ? delegate.getConnectionState() : ConnectionState.DISCOVERED;
86 public boolean connect() {
87 BluetoothDevice delegate = getDelegate();
88 return delegate != null ? delegate.connect() : false;
92 public boolean disconnect() {
93 BluetoothDevice delegate = getDelegate();
94 return delegate != null ? delegate.disconnect() : false;
98 public boolean discoverServices() {
99 BluetoothDevice delegate = getDelegate();
100 return delegate != null ? delegate.discoverServices() : false;
104 public boolean readCharacteristic(BluetoothCharacteristic characteristic) {
105 BluetoothDevice delegate = getDelegate();
106 return delegate != null && delegate.readCharacteristic(characteristic);
110 public boolean writeCharacteristic(BluetoothCharacteristic characteristic) {
111 BluetoothDevice delegate = getDelegate();
112 return delegate != null && delegate.writeCharacteristic(characteristic);
116 public boolean enableNotifications(BluetoothCharacteristic characteristic) {
117 BluetoothDevice delegate = getDelegate();
118 return delegate != null ? delegate.enableNotifications(characteristic) : false;
122 public boolean disableNotifications(BluetoothCharacteristic characteristic) {
123 BluetoothDevice delegate = getDelegate();
124 return delegate != null ? delegate.disableNotifications(characteristic) : false;
128 public boolean enableNotifications(BluetoothDescriptor descriptor) {
129 BluetoothDevice delegate = getDelegate();
130 return delegate != null ? delegate.enableNotifications(descriptor) : false;
134 public boolean disableNotifications(BluetoothDescriptor descriptor) {
135 BluetoothDevice delegate = getDelegate();
136 return delegate != null ? delegate.disableNotifications(descriptor) : false;
140 protected boolean addService(BluetoothService service) {
141 BluetoothDevice delegate = getDelegate();
142 return delegate != null ? delegate.addService(service) : false;
146 protected Collection<BluetoothDeviceListener> getListeners() {
147 BluetoothDevice delegate = getDelegate();
148 return delegate != null ? delegate.getListeners() : Collections.emptySet();
152 public @Nullable BluetoothCharacteristic getCharacteristic(UUID uuid) {
153 BluetoothDevice delegate = getDelegate();
154 return delegate != null ? delegate.getCharacteristic(uuid) : null;
158 protected void dispose() {
159 BluetoothDevice delegate = getDelegate();
160 if (delegate != null) {