1、可先在oncreate()方法里面实例化一个WifiManager.MulticastLock 对象lock;具体如下:
WifiManager manager = (WifiManager) this .getSystemService(Context.WIFI_SERVICE); WifiManager.MulticastLock lock= manager.createMulticastLock("test wifi");
2、在调用广播发送、接收报文之前先调用lock.acquire()方法;
3、用完之后及时调用lock.release()释放资源,否决多次调用lock.acquire()方法,程序可能会崩,详情请见
Caused by: java.lang.UnsupportedOperationException: Exceeded maximum number of wifi locks
注;记得在配置文件里面添加如下权限:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
经过这样处理后,多数手机都能正常发送接收到广播报文。
本小点转载自Android手机接收不到UDP报文
在UDP通信中,android端发送UDP广播包没有问题。至于接收的话,有时候不能接收到包。但是如果UDP包中指定了目标主机的地址的话,那么android端就能正常接收。
下面上一段代码,大家可用这段代码进行测试。
1、在一个Service里面,我们创建一个线程
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(); }
2、弄一个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