Android Wifi, by default is not Accept multicast, see: http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html
By default, the application does not receive multicast information. In this way, too many packets need to be received and the battery will soon be exhausted. You need to know that the most important factor for mobile devices (especially phones, tablets are much better) is power.
To enable the multicast function, follow these steps:
- Add android. permission. CHANGE_WIFI_MULTICAST_STATE to the Manifest file.
- Gets the MulticastLock object. This object cannot be directly instantiated and needs to be obtained indirectly through WifiManager. The factory Mode
- Call the acquire method of the MulticastLock object to obtain the multicast lock.
- Correspondingly, when multicast is used up, the MulticastLock release method should be called to release the lock in order not to waste power.
Below is a simple example for discovering servers through multicast.
Activity writing is relatively simple:
public class MulticastDemoActivity extends Activity { MulticastLock multicastLock; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); allowMulticast(); try { NetUtil.findServerIpAddress(); } catch (IOException e) { throw new RuntimeException(e); } Log.d("multicast.demo", "find ip ok."); multicastLock.release(); } private void allowMulticast(){ WifiManager wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE); multicastLock=wifiManager.createMulticastLock("multicast.test"); multicastLock.acquire(); } }
Enable or release the multicast lock in the Activity.
Tool-type code for sending messages and receiving IP address information using group Broadcasting
public class NetUtil { private static final String TAG="Net.Utils"; private static final int MULTICAST_PORT=5111; private static final String GROUP_IP="224.5.0.7"; private static byte[] sendData; static{ sendData=new byte[4]; // 0xEE78F1FB sendData[3] = (byte) 0xEE; sendData[2] = (byte) 0×78; sendData[1] = (byte) 0xF1; sendData[0] = (byte) 0xFB; } public static String findServerIpAddress() throws IOException{ String ip=null; MulticastSocket multicastSocket=new MulticastSocket(MULTICAST_PORT); multicastSocket.setLoopbackMode(true); InetAddress group = InetAddress.getByName(GROUP_IP); multicastSocket.joinGroup(group); DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,MULTICAST_PORT); for(;;){ multicastSocket.send(packet); Log.d(TAG,">>>send packet ok"); byte[] receiveData=new byte[256]; packet=new DatagramPacket(receiveData, receiveData.length); multicastSocket.receive(packet); String packetIpAddress=packet.getAddress().toString(); packetIpAddress=packetIpAddress.substring(1, packetIpAddress.length()); Log.d(TAG,"packet ip address: "+packetIpAddress); StringBuilder packetContent=new StringBuilder(); for(int i=0;i<receiveData.length;i++){ if(receiveData[i]==0){ break; } packetContent.append((char)receiveData[i]); } ip=packetContent.toString(); Log.d(TAG,"packet content ip is: "+ip); if(ip.equals(packetIpAddress)){ Log.d(TAG,"find server ip address: "+ip); break; }else{ Log.d(TAG,"not find server ip address, continue …"); try { Thread.sleep(1000); } catch (InterruptedException e) { } } } return ip; } }
From: http://marshal.easymorse.com/archives/4461