简体   繁体   English

通过UDP将数据从android发送到外部传感器

[英]Send data through UDP from android to external sensor

I'm trying to send some data from my android to an external sensor through wifi connection. 我正在尝试通过wifi连接将一些数据从我的android发送到外部传感器。 While I am able to send data from the sensor to the android with a UDP connection with success, I'm unable to do the opposite. 虽然我能够成功地通过UDP连接将数据从传感器发送到android,但我却无法做到相反。 The code where the data are send is the one below: 发送数据的代码如下:

public void onClick(View v) {
            Thread t = new Thread(){
                @Override
                public void run(){
                    while(true){
                        int server_port = 12345;
                        byte[] message = "1".getBytes();
                        System.out.println(message.toString());
                        try {
                            InetAddress local = InetAddress.getByName("255.255.255.255");
                            DatagramPacket p = new DatagramPacket(message,message.length,local,server_port);
                            DatagramSocket s = new DatagramSocket();

                            s.send(p);
                            s.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.start();
        }

As you can see above, OnClick of a button, I want the app to send the data through UDP. 如您在上方看到的,在按钮的OnClick上,我希望应用程序通过UDP发送数据。 The problem is that when i try to do so, this error occurs: 问题是,当我尝试这样做时,会发生此错误:

11-24 16:10:13.335: W/System.err(8077): java.net.SocketException: sendto failed: ENETUNREACH    (Network is unreachable)
11-24 16:10:13.335: W/System.err(8077): at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
11-24 16:10:13.335: W/System.err(8077):at libcore.io.IoBridge.sendto(IoBridge.java:475)
11-24 16:10:13.335: W/System.err(8077):at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
11-24 16:10:13.335: W/System.err(8077):     at java.net.DatagramSocket.send(DatagramSocket.java:284)
11-24 16:10:13.343: W/System.err(8077):     at com.example.waspmoteagriculture.MainActivity$3$1.run(MainActivity.java:97)
11-24 16:10:13.343: W/System.err(8077): Caused by: libcore.io.ErrnoException: sendto failed: ENETUNREACH (Network is unreachable)
11-24 16:10:13.343: W/System.err(8077):     at libcore.io.Posix.sendtoBytes(Native Method)
11-24 16:10:13.343: W/System.err(8077):     at libcore.io.Posix.sendto(Posix.java:151)
11-24 16:10:13.343: W/System.err(8077):     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
11-24 16:10:13.343: W/System.err(8077):     at libcore.io.IoBridge.sendto(IoBridge.java:473)
11-24 16:10:13.343: W/System.err(8077):     ... 3 more

I have already included the network permission. 我已经包含了网络许可。 Also I should mention that the sensor is connected to android's wifi hotspot in order to send information to the mobile. 我还要提到传感器已连接到android的wifi热点,以便将信息发送到手机。 I don't know if there is a problem in receiving data that way. 我不知道以这种方式接收数据是否有问题。

Is there any problem with the code or is this error related to something different? 代码是否存在任何问题,或者此错误与某些其他问题相关? Thank you in advance. 先感谢您。

I solved the problem in some way. 我以某种方式解决了这个问题。 It seemed that the broadcast address 255.255.255.255 was invalid so it could not find the network currently connected to (that also explains the network unreachable problem). 广播地址255.255.255.255似乎无效,因此找不到当前连接的网络(这也说明了网络无法访问的问题)。 In order for it to send the data I used the IP address of the sensor (found it through a received package with the use of the System.out.println(packet.getAddress().toString()); . So, in order to send data broadcast, i guess (not tested yet) that the broadcast adress of the current network is supposed to be used. 为了发送数据,我使用了传感器的IP地址(使用System.out.println(packet.getAddress().toString());通过接收到的包找到了它的IP地址System.out.println(packet.getAddress().toString());要发送数据广播,我猜(尚未测试)应该使用当前网络的广播地址。

Probably You need to set permission to access the network. 可能您需要设置访问网络的权限。 Add the following line to AndroidManifest.xml 将以下行添加到AndroidManifest.xml

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

If it does not work, then you need some changes in the code. 如果它不起作用,则您需要对代码进行一些更改。 Read more here: https://code.google.com/p/boxeeremote/wiki/AndroidUDP 在此处了解更多信息: https : //code.google.com/p/boxeeremote/wiki/AndroidUDP

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

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