Android Wifi获取组播

Android的Wifi,默认情况下是不接受组播的,见:http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html

默认情况下,应用是不接收组播信息的,这样要接收处理的报文太多,很快就会把电池用尽。要知道移动设备(特指电话一类的,平板要好得多)目前最重要的因素是电量。

要想打开组播功能,有以下几个步骤:

  • 在Manifest文件中加入:android.permission.CHANGE_WIFI_MULTICAST_STATE,这个权限
  • 获取到MulticastLock对象,这个对象不能直接实例化,要通过WifiManager间接得到,工厂模式
  • 调用MulticastLock对象的acquire方法,获取到组播锁
  • 相应的,用完组播,为了不浪费电力,要调用MulticastLock的release方法释放锁

下面写了个简单示例,通过组播发现服务器。

Activity写的比较简单:

  1. public class MulticastDemoActivity extends Activity {   
  2.       
  3.     MulticastLock multicastLock;   
  4.       
  5.     /** Called when the activity is first created. */   
  6.     @Override   
  7.     public void onCreate(Bundle savedInstanceState) {   
  8.         super.onCreate(savedInstanceState);   
  9.         setContentView(R.layout.main);   
  10.           
  11.         allowMulticast();   
  12.           
  13.         try {   
  14.             NetUtil.findServerIpAddress();   
  15.         } catch (IOException e) {   
  16.             throw new RuntimeException(e);   
  17.         }   
  18.           
  19.         Log.d("multicast.demo""find ip ok.");   
  20.           
  21.         multicastLock.release();   
  22.     }   
  23.       
  24.     private void allowMulticast(){   
  25.         WifiManager wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);   
  26.         multicastLock=wifiManager.createMulticastLock("multicast.test");   
  27.         multicastLock.acquire();   
  28.     }   
  29. }  


在Activity中打开和释放组播锁。

使用组播发送报文和接收ip地址信息的工具类代码

  1. public class NetUtil {   
  2.       
  3.     private static final String TAG="Net.Utils";   
  4.     private static final int MULTICAST_PORT=5111;   
  5.     private static final String GROUP_IP="224.5.0.7";   
  6.     private static byte[] sendData;   
  7.       
  8.     static{   
  9.         sendData=new byte[4];   
  10.         // 0xEE78F1FB   
  11.         sendData[3] = (byte0xEE;   
  12.         sendData[2] = (byte0×78;   
  13.         sendData[1] = (byte0xF1;   
  14.         sendData[0] = (byte0xFB;   
  15.     }   
  16.       
  17.     public static String findServerIpAddress() throws IOException{   
  18.         String ip=null;  
  19.   
  20.         MulticastSocket multicastSocket=new MulticastSocket(MULTICAST_PORT);   
  21.         multicastSocket.setLoopbackMode(true);   
  22.         InetAddress group = InetAddress.getByName(GROUP_IP);   
  23.         multicastSocket.joinGroup(group);   
  24.           
  25.         DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,MULTICAST_PORT);   
  26.           
  27.         for(;;){   
  28.             multicastSocket.send(packet);   
  29.             Log.d(TAG,">>>send packet ok");   
  30.               
  31.             byte[] receiveData=new byte[256];   
  32.             packet=new DatagramPacket(receiveData, receiveData.length);   
  33.             multicastSocket.receive(packet);   
  34.               
  35.             String packetIpAddress=packet.getAddress().toString();   
  36.             packetIpAddress=packetIpAddress.substring(1, packetIpAddress.length());   
  37.             Log.d(TAG,"packet ip address: "+packetIpAddress);   
  38.               
  39.             StringBuilder packetContent=new StringBuilder();   
  40.             for(int i=0;i<receiveData.length;i++){   
  41.                 if(receiveData[i]==0){   
  42.                     break;   
  43.                 }   
  44.                 packetContent.append((char)receiveData[i]);   
  45.             }   
  46.             ip=packetContent.toString();   
  47.             Log.d(TAG,"packet content ip is: "+ip);   
  48.               
  49.             if(ip.equals(packetIpAddress)){   
  50.                 Log.d(TAG,"find server ip address: "+ip);   
  51.                 break;   
  52.             }else{   
  53.                 Log.d(TAG,"not find server ip address, continue …");   
  54.                 try {   
  55.                     Thread.sleep(1000);   
  56.                 } catch (InterruptedException e) {   
  57.                 }   
  58.             }   
  59.         }   
  60.           
  61.         return ip;   
  62.     }   
  63. }  
  1. 转自: http://marshal.easymorse.com/archives/4461 

你可能感兴趣的:(Android Wifi获取组播)