]> git.basschouten.com Git - openhab-addons.git/commitdiff
[androiddebugbridge] Add mDNS discovery for android tv (#13462)
authorGiviMAD <GiviMAD@users.noreply.github.com>
Sun, 2 Oct 2022 10:38:59 +0000 (12:38 +0200)
committerGitHub <noreply@github.com>
Sun, 2 Oct 2022 10:38:59 +0000 (12:38 +0200)
* [androiddebugbridge] add mDNS discovery for android tv

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
bundles/org.openhab.binding.androiddebugbridge/README.md
bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/discovery/AndroidTVMDNSDiscoveryParticipant.java [new file with mode: 0644]
bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/discovery/FireTVStickMDNSDiscoveryParticipant.java

index b2607502396223bf8dcb1fe5e3c79aaee82e8083..46139bb993d704f832c6647158b368edd2ccb63c 100644 (file)
@@ -20,9 +20,11 @@ Please update this document if you tested it with other android versions to refl
 
 ## Discovery
 
-As I can not find a way to identify android devices in the network the discovery will try to connect through adb to all the reachable ip in the defined range.
+Android TV and Fire TV devices should be discovered automatically (using mDNS).
 
-You could customize the discovery process through the binding options. 
+Since other Android devices cannot be discovered automatically on the network, the manual discovery scan will try to connect via adb to all reachable IP addresses in the defined range.
+
+You could customize the discovery process through the binding options.
 
 **Your device will prompt a message requesting you to authorize the connection, you should check the option "Always allow connections from this device" (or something similar) and accept**.
 
diff --git a/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/discovery/AndroidTVMDNSDiscoveryParticipant.java b/bundles/org.openhab.binding.androiddebugbridge/src/main/java/org/openhab/binding/androiddebugbridge/internal/discovery/AndroidTVMDNSDiscoveryParticipant.java
new file mode 100644 (file)
index 0000000..5592301
--- /dev/null
@@ -0,0 +1,114 @@
+/**
+ * Copyright (c) 2010-2022 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.androiddebugbridge.internal.discovery;
+
+import static org.openhab.binding.androiddebugbridge.internal.AndroidDebugBridgeBindingConstants.*;
+
+import java.util.Dictionary;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jmdns.ServiceInfo;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.core.config.discovery.DiscoveryResult;
+import org.openhab.core.config.discovery.DiscoveryResultBuilder;
+import org.openhab.core.config.discovery.DiscoveryService;
+import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
+import org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService;
+import org.openhab.core.thing.Thing;
+import org.openhab.core.thing.ThingTypeUID;
+import org.openhab.core.thing.ThingUID;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Modified;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The {@link AndroidTVMDNSDiscoveryParticipant} is responsible for discovering new and removed Android TV devices. It
+ * uses
+ * the central {@link MDNSDiscoveryService}.
+ *
+ * @author Miguel Álvarez - Initial contribution
+ */
+@Component(service = MDNSDiscoveryParticipant.class, configurationPid = "discovery.androiddebugbridge")
+@NonNullByDefault
+public class AndroidTVMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
+
+    private static final String SERVICE_TYPE = "_androidtvremote2._tcp.local.";
+    private static final String MDNS_PROPERTY_MAC_ADDRESS = "bt";
+    private final Logger logger = LoggerFactory.getLogger(AndroidTVMDNSDiscoveryParticipant.class);
+
+    private boolean isAutoDiscoveryEnabled = true;
+
+    @Activate
+    protected void activate(ComponentContext componentContext) {
+        activateOrModifyService(componentContext);
+    }
+
+    @Modified
+    protected void modified(ComponentContext componentContext) {
+        activateOrModifyService(componentContext);
+    }
+
+    private void activateOrModifyService(ComponentContext componentContext) {
+        Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
+        String autoDiscoveryPropertyValue = (String) properties
+                .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
+        if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
+            isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
+        }
+    }
+
+    @Override
+    public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
+        return SUPPORTED_THING_TYPES;
+    }
+
+    @Override
+    public String getServiceType() {
+        return SERVICE_TYPE;
+    }
+
+    @Override
+    public @Nullable DiscoveryResult createResult(ServiceInfo service) {
+        if (isAutoDiscoveryEnabled) {
+            ThingUID uid = getThingUID(service);
+            if (uid != null) {
+                String ip = service.getHostAddresses()[0];
+                String macAddress = service.getPropertyString(MDNS_PROPERTY_MAC_ADDRESS);
+                String friendlyName = String.format("%s (%s)", service.getName(), ip);
+                return DiscoveryResultBuilder.create(uid) //
+                        .withProperties(Map.of( //
+                                PARAMETER_IP, ip, //
+                                Thing.PROPERTY_MAC_ADDRESS, macAddress.toLowerCase())) //
+                        .withLabel(friendlyName) //
+                        .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS) //
+                        .build();
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public @Nullable ThingUID getThingUID(ServiceInfo service) {
+        String macAddress = service.getPropertyString(MDNS_PROPERTY_MAC_ADDRESS);
+        if (macAddress != null && !macAddress.isBlank()) {
+            return new ThingUID(THING_TYPE_ANDROID_DEVICE, macAddress.replaceAll(":", "").toLowerCase());
+        }
+        return null;
+    }
+}
index e14f4beb475aa75aa7877c7e7329c30ecb1ad82f..4257f03c8eeebf47a5b1d4eec0a8a47c929f4066 100644 (file)
@@ -41,7 +41,7 @@ import org.osgi.service.component.annotations.Modified;
  *
  * @author Christoph Weitkamp - Initial contribution
  */
-@Component(configurationPid = "discovery.androiddebugbridge")
+@Component(service = MDNSDiscoveryParticipant.class, configurationPid = "discovery.androiddebugbridge")
 @NonNullByDefault
 public class FireTVStickMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {