From 1f5cd4298bd5e05332780807bd31954a03989bf7 Mon Sep 17 00:00:00 2001 From: khk624 Date: Mon, 16 Jun 2014 16:58:19 +0900 Subject: [PATCH] Attempt fixing issues with jmdns #107 --- src/com/connectsdk/core/Util.java | 19 +++++++++++++++++++ .../discovery/DiscoveryManager.java | 5 +++-- .../provider/SSDPDiscoveryProvider.java | 13 +++---------- .../provider/ZeroconfDiscoveryProvider.java | 14 +++++++++++--- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/com/connectsdk/core/Util.java b/src/com/connectsdk/core/Util.java index f52b4ec9..5264a259 100644 --- a/src/com/connectsdk/core/Util.java +++ b/src/com/connectsdk/core/Util.java @@ -20,6 +20,8 @@ package com.connectsdk.core; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -28,6 +30,9 @@ import org.apache.http.conn.util.InetAddressUtils; +import android.content.Context; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; import android.os.Handler; import android.os.Looper; @@ -123,4 +128,18 @@ public static boolean isIPv4Address(String ipAddress) { public static boolean isIPv6Address(String ipAddress) { return InetAddressUtils.isIPv6Address(ipAddress); } + + public static InetAddress getIpAddress(Context context) throws UnknownHostException { + WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); + int ip = wifiInfo.getIpAddress(); + + if (ip == 0) { + return null; + } + else { + byte[] ipAddress = convertIpAddress(ip); + return InetAddress.getByAddress(ipAddress); + } + } } \ No newline at end of file diff --git a/src/com/connectsdk/discovery/DiscoveryManager.java b/src/com/connectsdk/discovery/DiscoveryManager.java index 44652ce9..6320023c 100644 --- a/src/com/connectsdk/discovery/DiscoveryManager.java +++ b/src/com/connectsdk/discovery/DiscoveryManager.java @@ -51,9 +51,9 @@ import com.connectsdk.device.ConnectableDeviceListener; import com.connectsdk.device.ConnectableDeviceStore; import com.connectsdk.device.DefaultConnectableDeviceStore; -import com.connectsdk.discovery.provider.ZeroconfDiscoveryProvider; import com.connectsdk.discovery.provider.CastDiscoveryProvider; import com.connectsdk.discovery.provider.SSDPDiscoveryProvider; +import com.connectsdk.discovery.provider.ZeroconfDiscoveryProvider; import com.connectsdk.service.AirPlayService; import com.connectsdk.service.CastService; import com.connectsdk.service.DIALService; @@ -199,6 +199,7 @@ public DiscoveryManager(Context context, ConnectableDeviceStore connectableDevic WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); multicastLock = wifiMgr.createMulticastLock("Connect SDK"); + multicastLock.setReferenceCounted(true); capabilityFilters = new ArrayList(); pairingLevel = PairingLevel.OFF; @@ -364,7 +365,7 @@ public boolean deviceIsCompatible(ConnectableDevice device) { * + NetcastTVService * + RokuService * + WebOSTVService - * - AirPlayDiscoveryProvider + * - ZeroconfDiscoveryProvider * + AirPlayService */ public void registerDefaultDeviceTypes() { diff --git a/src/com/connectsdk/discovery/provider/SSDPDiscoveryProvider.java b/src/com/connectsdk/discovery/provider/SSDPDiscoveryProvider.java index 8926efba..f3446f70 100644 --- a/src/com/connectsdk/discovery/provider/SSDPDiscoveryProvider.java +++ b/src/com/connectsdk/discovery/provider/SSDPDiscoveryProvider.java @@ -91,17 +91,10 @@ private void openSocket() { if (mSSDPSocket != null && mSSDPSocket.isConnected()) return; - WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); - WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); - - int ip = wifiInfo.getIpAddress(); - if (ip == 0) - return; - - byte[] ipAddress = Util.convertIpAddress(ip); - try { - InetAddress source = InetAddress.getByAddress(ipAddress); + InetAddress source = Util.getIpAddress(context); + if (source == null) + return; mSSDPSocket = new SSDPSocket(source); } catch (UnknownHostException e) { diff --git a/src/com/connectsdk/discovery/provider/ZeroconfDiscoveryProvider.java b/src/com/connectsdk/discovery/provider/ZeroconfDiscoveryProvider.java index aa4373cd..f0b70c4b 100644 --- a/src/com/connectsdk/discovery/provider/ZeroconfDiscoveryProvider.java +++ b/src/com/connectsdk/discovery/provider/ZeroconfDiscoveryProvider.java @@ -21,6 +21,7 @@ package com.connectsdk.discovery.provider; import java.io.IOException; +import java.net.InetAddress; import java.util.ArrayList; import java.util.List; import java.util.Timer; @@ -31,6 +32,7 @@ import javax.jmdns.JmDNS; import javax.jmdns.ServiceEvent; import javax.jmdns.ServiceListener; +import javax.jmdns.ServiceTypeListener; import org.json.JSONException; import org.json.JSONObject; @@ -45,6 +47,8 @@ import com.connectsdk.service.config.ServiceDescription; public class ZeroconfDiscoveryProvider implements DiscoveryProvider { + private static final String HOSTNAME = "connectsdk"; + JmDNS jmdns; private final static int RESCAN_INTERVAL = 10000; @@ -116,20 +120,24 @@ public void serviceAdded(ServiceEvent event) { private CopyOnWriteArrayList serviceListeners; public ZeroconfDiscoveryProvider(Context context) { - initJmDNS(); + initJmDNS(context); services = new ConcurrentHashMap(8, 0.75f, 2); serviceListeners = new CopyOnWriteArrayList(); serviceFilters = new ArrayList(); } - private void initJmDNS() { + private void initJmDNS(final Context context) { Util.runInBackground(new Runnable() { @Override public void run() { try { - jmdns = JmDNS.create(); + InetAddress source = Util.getIpAddress(context); + if (source == null) + return; + + jmdns = JmDNS.create(source, HOSTNAME); } catch (IOException e) { e.printStackTrace(); }