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.generic.internal;
15 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
22 import org.openhab.binding.bluetooth.BluetoothCompanyIdentifiers;
23 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
24 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class implements the BluetoothDiscoveryParticipant for generic bluetooth devices.
37 * @author Connor Petty - Initial contribution
41 @Component(service = BluetoothDiscoveryParticipant.class)
42 public class GenericDiscoveryParticipant implements BluetoothDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(GenericDiscoveryParticipant.class);
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Set.of(GenericBindingConstants.THING_TYPE_GENERIC);
52 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
53 ThingUID thingUID = getThingUID(device);
54 if (thingUID == null) {
55 // the thingUID will never be null in practice but this makes the null checker happy
58 String label = "Generic Connectable Bluetooth Device";
59 Map<String, Object> properties = new HashMap<>();
60 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
61 Integer txPower = device.getTxPower();
62 if (txPower != null && txPower > 0) {
63 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
65 String manufacturer = BluetoothCompanyIdentifiers.get(device.getManufacturerId());
66 if (manufacturer == null) {
67 logger.debug("Unknown manufacturer Id ({}) found on bluetooth device.", device.getManufacturerId());
69 properties.put(Thing.PROPERTY_VENDOR, manufacturer);
70 label += " (" + manufacturer + ")";
73 addPropertyIfPresent(properties, Thing.PROPERTY_MODEL_ID, device.getModel());
74 addPropertyIfPresent(properties, Thing.PROPERTY_SERIAL_NUMBER, device.getSerialNumber());
75 addPropertyIfPresent(properties, Thing.PROPERTY_HARDWARE_VERSION, device.getHardwareRevision());
76 addPropertyIfPresent(properties, Thing.PROPERTY_FIRMWARE_VERSION, device.getFirmwareRevision());
77 addPropertyIfPresent(properties, BluetoothBindingConstants.PROPERTY_SOFTWARE_VERSION,
78 device.getSoftwareRevision());
80 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
81 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
82 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
85 private static void addPropertyIfPresent(Map<String, Object> properties, String key, @Nullable Object value) {
87 properties.put(key, value);
92 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
93 return new ThingUID(GenericBindingConstants.THING_TYPE_GENERIC, device.getAdapter().getUID(),
94 device.getAddress().toString().toLowerCase().replace(":", ""));
98 public boolean requiresConnection(BluetoothDiscoveryDevice device) {
104 // we want to go last
105 return Integer.MAX_VALUE;