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.bluez.internal;
15 import java.util.Collection;
16 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.bluetooth.BluetoothAddress;
23 import com.github.hypfvieh.bluetooth.DeviceManager;
24 import com.github.hypfvieh.bluetooth.wrapper.BluetoothAdapter;
25 import com.github.hypfvieh.bluetooth.wrapper.BluetoothDevice;
28 * This is a threadsafe wrapper for a {@link DeviceManager} that also only exposes the methods
29 * required to implement this binding.
31 * @author Connor Petty - Initial Contribution
34 public class DeviceManagerWrapper {
36 private @Nullable DeviceManager deviceManager;
38 public DeviceManagerWrapper(@Nullable DeviceManager deviceManager) {
39 this.deviceManager = deviceManager;
42 @SuppressWarnings("null")
43 public synchronized Collection<BluetoothAdapter> scanForBluetoothAdapters() {
44 if (deviceManager != null) {
45 return deviceManager.scanForBluetoothAdapters();
51 public synchronized @Nullable BluetoothAdapter getAdapter(BluetoothAddress address) {
52 DeviceManager devMgr = deviceManager;
54 // we don't use `deviceManager.getAdapter` here since it might perform a scan if the adapter is missing.
55 String addr = address.toString();
56 List<BluetoothAdapter> adapters = devMgr.getAdapters();
57 if (adapters != null) {
58 for (BluetoothAdapter btAdapter : adapters) {
59 String btAddr = btAdapter.getAddress();
60 if (addr.equalsIgnoreCase(btAddr)) {
69 public synchronized List<BluetoothDevice> getDevices(BluetoothAdapter adapter) {
70 if (deviceManager != null) {
71 return deviceManager.getDevices(adapter.getAddress(), true);