Multicast Support on Android in Hotspot/Tethering mode

廉价感情. 提交于 2019-12-03 02:07:29
user707606

As this article show: https://plus.google.com/+Chainfire/posts/9NMemrKYnCd

MulticastSocket::setNetworkInterface()

would be the answer

you can find the wlan0 eth by :

public static NetworkInterface getWlanEth() {
    Enumeration<NetworkInterface> enumeration = null;
    try {
        enumeration = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    NetworkInterface wlan0 = null;
    StringBuilder sb = new StringBuilder();
    while (enumeration.hasMoreElements()) {
        wlan0 = enumeration.nextElement();
        sb.append(wlan0.getName() + " ");
        if (wlan0.getName().equals("wlan0")) {
            //there is probably a better way to find ethernet interface
            Log.i(TAG, "wlan0 found");
            return wlan0;
        }
    }

    return null;
}

Have a try and lemme know if it works or not in Hotspot mode...

Do you have the manifest permission and are you creating a lock?

For permission please see: 'CHANGE_WIFI_MULTICAST_STATE' in http://developer.android.com/reference/android/Manifest.permission.html

Also, to create a multicast lock... please see: http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html

I've had the same problem and came up with a solution by the combination of @braden, @user707606 and the mainly the post by Chainfire in this Link.

Post in the link is nice but doesn't really offer any code samples but here it's. First you need to Acquire Multicast Lock, this is needed for some Android devices, didn't try in most of them but it was mentioned in some other posts, so I've included it in my code.

Permission is required, so first, add the permissions into your Manifest file.

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Then the second step is to acquire multicast lock in your method.

/* Acquire MultiCast Lock */
WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

And then, find your Wifi Network Interface

/**
 * Finds Network Interface of Wifi Ethernet.
 *
 * @return
 */
public static NetworkInterface findWifiNetworkInterface() {

    Enumeration<NetworkInterface> enumeration = null;

    try {
        enumeration = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
    }

    NetworkInterface wlan0 = null;

    while (enumeration.hasMoreElements()) {

        wlan0 = enumeration.nextElement();

        if (wlan0.getName().equals("wlan0")) {
            return wlan0;
        }
    }

    return null;
}

Later, create a Multicast socket with an available port and set your Wifi NetworkInterface.

MulticastSocket multicastSocket = new MulticastSocket();

/* Set NetworkInterface of MultiCast Socket */
NetworkInterface wifiNetworkInterface = findWifiNetworkInterface();
if (wifiNetworkInterface != null) multicastSocket.setNetworkInterface(wifiNetworkInterface);

Then the rest of your implementation remains the same. And once you are done with Multicast Lock, it's recommended to release it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!