I have a prototype Android app that is listening for multicast packets to 'discover' clients to communicate with. The socket set up is similar to this:
InetAddress group = InetAddress.getByName("228.1.2.3");
MulticastSocket s = new MulticastSocket(4000);
s.joinGroup(group);
This works very well when all the devices are connected via WiFi. I would like to support this with the phone acting as a portable hotspot. However, while all my devices appear to connect to the hotspot correctly I no longer receive multicast data. I'm wondering if there are restrictions that disallow this type of communication in hotspot mode, or if there are is any additional network configuration required to enable this? I've tried this on a couple different devices running Gingerbread and Froyo with no luck.
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.
来源:https://stackoverflow.com/questions/6550618/multicast-support-on-android-in-hotspot-tethering-mode