]> git.basschouten.com Git - openhab-addons.git/blob
484c1fe3d7faa9d9d20b6a0a7a0e769603d5e5ce
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.bluetooth.BluetoothAddress;
21
22 import com.github.hypfvieh.bluetooth.DeviceManager;
23 import com.github.hypfvieh.bluetooth.wrapper.BluetoothAdapter;
24 import com.github.hypfvieh.bluetooth.wrapper.BluetoothDevice;
25
26 /**
27  * This is a threadsafe wrapper for a {@link DeviceManager} that also only exposes the methods
28  * required to implement this binding.
29  *
30  * @author Connor Petty - Initial Contribution
31  */
32 @NonNullByDefault
33 public class DeviceManagerWrapper {
34
35     private DeviceManager deviceManager;
36
37     public DeviceManagerWrapper(DeviceManager deviceManager) {
38         this.deviceManager = deviceManager;
39     }
40
41     public synchronized Collection<BluetoothAdapter> scanForBluetoothAdapters() {
42         return deviceManager.scanForBluetoothAdapters();
43     }
44
45     public synchronized @Nullable BluetoothAdapter getAdapter(BluetoothAddress address) {
46         // we don't use `deviceManager.getAdapter` here since it might perform a scan if the adapter is missing.
47         String addr = address.toString();
48         List<BluetoothAdapter> adapters = deviceManager.getAdapters();
49         if (adapters != null) {
50             for (BluetoothAdapter btAdapter : adapters) {
51                 String btAddr = btAdapter.getAddress();
52                 if (addr.equalsIgnoreCase(btAddr)) {
53                     return btAdapter;
54                 }
55             }
56         }
57         return null;
58     }
59
60     public synchronized List<BluetoothDevice> getDevices(BluetoothAdapter adapter) {
61         return deviceManager.getDevices(adapter.getAddress(), true);
62     }
63 }