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.ruuvitag.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
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;
33 * This discovery participant is able to recognize ruuvitag devices and create discovery results for them.
35 * @author Sami Salonen - Initial contribution
40 public class RuuviTagDiscoveryParticipant implements BluetoothDiscoveryParticipant {
42 private static final int RUUVITAG_COMPANY_ID = 1177;
45 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
46 return Collections.singleton(RuuviTagBindingConstants.THING_TYPE_BEACON);
50 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
51 Integer manufacturerId = device.getManufacturerId();
52 if (manufacturerId != null && manufacturerId == RUUVITAG_COMPANY_ID) {
53 return new ThingUID(RuuviTagBindingConstants.THING_TYPE_BEACON, device.getAdapter().getUID(),
54 device.getAddress().toString().toLowerCase().replace(":", ""));
60 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
61 ThingUID thingUID = getThingUID(device);
62 if (thingUID == null) {
65 String label = "Ruuvi Tag";
66 Map<String, Object> properties = new HashMap<>();
67 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
68 properties.put(Thing.PROPERTY_VENDOR, "Ruuvi Innovations Ltd (Oy)");
69 Integer txPower = device.getTxPower();
70 if (txPower != null) {
71 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
74 // Create the discovery result and add to the inbox
75 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
76 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
77 .withBridge(device.getAdapter().getUID()).withLabel(label).build();