一、可先在oncreate()方法裏面實例化一個WifiManager.MulticastLock 對象lock;具體以下:html
WifiManager manager = (WifiManager) this .getSystemService(Context.WIFI_SERVICE); WifiManager.MulticastLock lock= manager.createMulticastLock("test wifi");
二、在調用廣播發送、接收報文以前先調用lock.acquire()方法;java
三、用完以後及時調用lock.release()釋放資源,否決屢次調用lock.acquire()方法,程序可能會崩,詳情請見android
Caused by: java.lang.UnsupportedOperationException: Exceeded maximum number of wifi locks服務器
注;記得在配置文件裏面添加以下權限:ide
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
通過這樣處理後,多數手機都能正常發送接收到廣播報文。測試
本小點轉載自Android手機接收不到UDP報文ui
在UDP通訊中,android端發送UDP廣播包沒有問題。至於接收的話,有時候不能接收到包。可是若是UDP包中指定了目標主機的地址的話,那麼android端就能正常接收。this
下面上一段代碼,你們可用這段代碼進行測試。spa
一、在一個Service裏面,咱們建立一個線程.net
public void onCreate() {//用於建立線程 WifiManager manager = (WifiManager) this .getSystemService(Context.WIFI_SERVICE); udphelper = new UdpHelper(manager); //傳遞WifiManager對象,以便在UDPHelper類裏面使用MulticastLock udphelper.addObserver(MsgReceiveService.this); tReceived = new Thread(udphelper); tReceived.start(); super.onCreate(); }
二、弄一個UDP幫助類,這個類主要用於發送和接收數據
package com.example.com.ihome.bang.util; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Observable; import com.example.com.ihome.bang.tool.SendThread; import android.net.wifi.WifiManager; import android.util.Log; /** * * UdpHelper幫助類 * * @author 陳喆榕 * */ public class UdpHelper implements Runnable { public Boolean IsThreadDisable = false;//指示監聽線程是否終止 private static WifiManager.MulticastLock lock; InetAddress mInetAddress; public UdpHelper(WifiManager manager) { this.lock= manager.createMulticastLock("UDPwifi"); } public void StartListen() { // UDP服務器監聽的端口 Integer port = 8903; // 接收的字節大小,客戶端發送的數據不能超過這個大小 byte[] message = new byte[100]; try { // 創建Socket鏈接 DatagramSocket datagramSocket = new DatagramSocket(port); datagramSocket.setBroadcast(true); DatagramPacket datagramPacket = new DatagramPacket(message, message.length); try { while (!IsThreadDisable) { // 準備接收數據 Log.d("UDP Demo", "準備接受"); this.lock.acquire(); datagramSocket.receive(datagramPacket); String strMsg=new String(datagramPacket.getData()).trim(); Log.d("UDP Demo", datagramPacket.getAddress() .getHostAddress().toString() + ":" +strMsg );this.lock.release(); } } catch (IOException e) {//IOException e.printStackTrace(); } } catch (SocketException e) { e.printStackTrace(); } } public static void send(String message) { message = (message == null ? "Hello IdeasAndroid!" : message); int server_port = 8904; Log.d("UDP Demo", "UDP發送數據:"+message); DatagramSocket s = null; try { s = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); } InetAddress local = null; try { local = InetAddress.getByName("255.255.255.255"); } catch (UnknownHostException e) { e.printStackTrace(); } int msg_length = message.length(); byte[] messageByte = message.getBytes(); DatagramPacket p = new DatagramPacket(messageByte, msg_length, local, server_port); try { s.send(p); s.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { StartListen(); } }
轉http://www.cnblogs.com/kissazi2/p/3157755.html