一、有的手機不能直接接收UDP包,可能是手機廠商在定製Rom的時候把這個功能給關掉了。
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端發送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();
 
    }
 
}