简体   繁体   中英

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. 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. 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. 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. 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). 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.

Probably You need to set permission to access the network. Add the following line to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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