Android Wifi擷取組播

來源:互聯網
上載者:User

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寫的比較簡單:

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();     } }

在Activity中開啟和釋放組播鎖。

使用組播發送報文和接收ip地址資訊的工具類代碼

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;     } }
轉自: http://marshal.easymorse.com/archives/4461
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.