user1739999
user1739999

Reputation: 93

Android UDP NetworkReceiver not receving any data on WI-FI

The following code was tried in android and it does not receive any data using a direct WI-FI meaning the both sender (desktop app) and Android app are on the same network. The sender app knows ipaddress of the WI-FI tablet and sending the data to that address.

However the following code works fine running it as a standalone java application. The receive code does not use a InetAddress address; Is it needed in Android. Could someone post example of receive udp data via WI-FI.

The sender sends the data to the tablet knowing the tablet ipaddress. Both the sender and receiver are using the same port.

I am trying to get a data packet and get the contents out of the data packet. I am not echoing anything back to the sender. Do I need to echo anything back to the sender. Also do I need a WI-FI manager?

import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class NetworkReceiver {

  static boolean isFinish = false;

    try {

         DatagramSocket s = new DatagramSocket(2010);
         System.out.println("setting up port");
         byte[] data = new byte[12];

        while (!isFinish) 
        {          
            System.out.println("creating datagram");
            DatagramPacket p = new DatagramPacket(data, data.length);
            s.receive(p);
            System.out.println("got packet");
            ByteBuffer bb = ByteBuffer.allocate(12).order(ByteOrder.LITTLE_ENDIAN);
            bb.put(data);
            bb.rewind();
            System.out.println(bb.getFloat());
            System.out.println(bb.getFloat());
            System.out.println(bb.getFloat());

         }
    } catch (IOException e) {
      e.printStackTrace();
    }

}

Upvotes: 0

Views: 395

Answers (1)

dubmojo
dubmojo

Reputation: 6838

However the following code works fine running it as a standalone java application.

I'm assuming that you ran the sender and receiver applications from two separate PCs connective to the same WIFI router correct?

I am trying to get a data packet and get the contents out of the data packet. I am not echoing anything back to the sender. Do I need to echo anything back to the sender. Also do I need a WI-FI manager?

No, you don't need to echo or respond to a received packet in any way.

Your receiver code looks ok enough that it should at least receive the 1st packet sent. Android would have complained if your AndroidManifest.xml permissions didn't allow for network access.

All I can suggest until you confirm that your sender app (you should include the code) is actually sending packets out into the WIFI network, is to make sure you can ping your tablet from the PC. Then install Wireshark on your sender PC and take a look at the UDP packets issued when it runs. Check the destination address and port. Make sure they look like they should reach your tablet.

Finally, I'm assuming again that you're not trying to multicast UDP since you've specified the tablet's IP address in your sender. If you are, you need to get a multicast lock with the WifiManager.

Upvotes: 0

Related Questions