]> git.basschouten.com Git - openhab-addons.git/blob
6260726966094601aacf54f703b6362465cdf640
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.bluetooth.bluez.internal;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.bluetooth.BluetoothAddress;
22
23 import com.github.hypfvieh.bluetooth.DeviceManager;
24 import com.github.hypfvieh.bluetooth.wrapper.BluetoothAdapter;
25 import com.github.hypfvieh.bluetooth.wrapper.BluetoothDevice;
26
27 /**
28  * This is a threadsafe wrapper for a {@link DeviceManager} that also only exposes the methods
29  * required to implement this binding.
30  *
31  * @author Connor Petty - Initial Contribution
32  */
33 @NonNullByDefault
34 public class DeviceManagerWrapper {
35
36     private @Nullable DeviceManager deviceManager;
37
38     public DeviceManagerWrapper(@Nullable DeviceManager deviceManager) {
39         this.deviceManager = deviceManager;
40     }
41
42     @SuppressWarnings("null")
43     public synchronized Collection<BluetoothAdapter> scanForBluetoothAdapters() {
44         if (deviceManager != null) {
45             return deviceManager.scanForBluetoothAdapters();
46         } else {
47             return Set.of();
48         }
49     }
50
51     public synchronized @Nullable BluetoothAdapter getAdapter(BluetoothAddress address) {
52         DeviceManager devMgr = deviceManager;
53         if (devMgr != null) {
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)) {
61                         return btAdapter;
62                     }
63                 }
64             }
65         }
66         return null;
67     }
68
69     public synchronized List<BluetoothDevice> getDevices(BluetoothAdapter adapter) {
70         if (deviceManager != null) {
71             return deviceManager.getDevices(adapter.getAddress(), true);
72         } else {
73             return List.of();
74         }
75     }
76 }