简体   繁体   English

某些设备未收到Android UDP

[英]Android UDP doesn't receive in some devices

I have an UDP send and receive which works in my device Samsung Galaxy Ace Plus (S7500) but the same code doesn't work in other devices, for example Samsung Galaxy S4. 我有一个可以在我的设备Samsung Galaxy Ace Plus(S7500)中工作的UDP发送和接收,但是相同的代码在其他设备(例如Samsung Galaxy S4)中不起作用。 I don't have any error. 我没有任何错误。

Send : 发送:

public class SendThread extends Thread {

byte[] receiveData = new byte[1024];
DatagramSocket serverSocket = null;


public SendThread() {
    this.start();
}

public void run() {
    DatagramSocket serverSocket = null;
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];

    try {
        serverSocket = new DatagramSocket("MY SOCKET PORT");
        InetAddress IP = InetAddress.getByName("MY IP");
        String send= "I am Android";
        sendData = send.getBytes();
        DatagramPacket send = new DatagramPacket(sendData, sendData.length, IP, "MY SEND PORT");
        serverSocket.send(send);

        serverSocket.close();
    } catch (Exception e) {
    }

}

} }

Receive : 收到:

public class ReceiveThread extends Thread {

byte[] receiveData = new byte[1024];
DatagramSocket serverSocket = null;
boolean isActive = true;

public ReceiveThread() {
    this.start();
}

public void run() {

    DatagramSocket serverSocket = null;
    byte[] receiveData = new byte[1024];

    while (isActive) {
        try {
            serverSocket = new DatagramSocket("MY RECEIVE PORT");

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String( receivePacket.getData());
            System.out.println("RECEIVED: " + sentence);

            serverSocket.close();

        } catch (Exception e){
        }
    }
}

} }

This problem ocurred because some devices lock the Datagram receiver because the protocol security implemented by factory. 发生此问题的原因是某些设备锁定了数据报接收器,因为工厂实现了协议安全性。

Your code is not wrong, but you need change the DatagramSocket for MulticastSocket. 您的代码没有错,但是您需要将DatagramSocket更改为MulticastSocket。

For this your need execute some steps: 为此,您需要执行一些步骤:

First, it's needed to add the uses-permission: 首先,需要添加uses-permission:

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

On AmdroidManifest.xml 在AmdroidManifest.xml上

Second, it's necessary create a MulticastLock; 其次,有必要创建一个MulticastLock; Without this the MulticastSocket is not work properly; 没有这个,MulticastSocket将无法正常工作。

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);

Thirdy, replace the DatagramSocket by MulticastSocket. 第三,用MulticastSocket替换DatagramSocket。 Only on receive methods was needed put the code below or similar: 仅在接收方法上需要将代码放在下面或类似的地方:

MulticastSocket ms = new MulticastScoket("Your socket port");
ms.joinGroup("Your IP");

It's not needed any modifies to send messages. 无需任何修改即可发送消息。

I use the multcast ip equals to 239.255.255.255. 我使用的多播IP等于239.255.255.255。 Attempt to range of multicast ip because the wrong ip will block the method flow correctly. 尝试设置多播ip的范围,因为错误的ip会正确阻止方法流。

Finally, before use MulticastSocket it's needed to execute MulticastLock.acquire() , and after use execute MulticastLock.release() ; 最后,在使用MulticastSocket之前,需要执行MulticastLock.acquire() ,在使用之后,必须执行MulticastLock.release()

It could be puted on service, and acquire or release MulticastLock on start or stop service. 它可以投入使用,并在启动或停止服务时获取或释放MulticastLock。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM