]> git.basschouten.com Git - openhab-addons.git/blob
494b0cf3be233b5d2858e68cd284896aa4a3ae59
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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     public synchronized Collection<BluetoothAdapter> scanForBluetoothAdapters() {
43         if (deviceManager != null) {
44             return deviceManager.scanForBluetoothAdapters();
45         } else {
46             return Set.of();
47         }
48     }
49
50     public synchronized @Nullable BluetoothAdapter getAdapter(BluetoothAddress address) {
51         DeviceManager devMgr = deviceManager;
52         if (devMgr != null) {
53             // we don't use `deviceManager.getAdapter` here since it might perform a scan if the adapter is missing.
54             String addr = address.toString();
55             List<BluetoothAdapter> adapters = devMgr.getAdapters();
56             if (adapters != null) {
57                 for (BluetoothAdapter btAdapter : adapters) {
58                     String btAddr = btAdapter.getAddress();
59                     if (addr.equalsIgnoreCase(btAddr)) {
60                         return btAdapter;
61                     }
62                 }
63             }
64         }
65         return null;
66     }
67
68     public synchronized List<BluetoothDevice> getDevices(BluetoothAdapter adapter) {
69         if (deviceManager != null) {
70             return deviceManager.getDevices(adapter.getAddress(), true);
71         } else {
72             return List.of();
73         }
74     }
75 }