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.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bluetooth.BluetoothAdapter;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.UID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.framework.ServiceRegistration;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
38 * The {@link BlueZHandlerFactory} is responsible for creating things and thing
41 * @author Kai Kreuzer - Initial contribution and API
42 * @author Connor Petty - Added DeviceManagerFactory
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluetooth.bluez")
46 public class BlueZHandlerFactory extends BaseThingHandlerFactory {
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
49 .singleton(BlueZAdapterConstants.THING_TYPE_BLUEZ);
51 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
53 private final DeviceManagerFactory deviceManagerFactory;
56 public BlueZHandlerFactory(@Reference DeviceManagerFactory deviceManagerFactory) {
57 this.deviceManagerFactory = deviceManagerFactory;
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected @Nullable ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69 if (thingTypeUID.equals(BlueZAdapterConstants.THING_TYPE_BLUEZ)) {
70 BlueZBridgeHandler handler = new BlueZBridgeHandler((Bridge) thing, deviceManagerFactory);
71 registerBluetoothAdapter(handler);
78 private synchronized void registerBluetoothAdapter(BluetoothAdapter adapter) {
79 this.serviceRegs.put(adapter.getUID(),
80 bundleContext.registerService(BluetoothAdapter.class.getName(), adapter, new Hashtable<>()));
84 protected synchronized void removeHandler(ThingHandler thingHandler) {
85 if (thingHandler instanceof BluetoothAdapter) {
86 UID uid = ((BluetoothAdapter) thingHandler).getUID();
87 ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
88 if (serviceReg != null) {
89 serviceReg.unregister();