JDSlimz
JDSlimz

Reputation: 113

Failing to recieve UDP byte on Android app

I am programming an android app to receive a packet that is being broadcast on the broadcast address of a network (This has been tested and the packet does get broadcast and gets received in the "UDP Sender/Receiver" application as well.) I cannot get my app to pick it up and tell me that it exists. The devices are on the same network and the code for the sending device is working and proprietary. Here is the basic DatagramSocket code for the app.

    package com.ti.cc3x.android;

import java.io.IOException;
import java.net.*;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class buttonListener extends Activity {

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listener);
        final TextView txt = (TextView)findViewById(R.id.txt1);


     new Thread( new Runnable(){
         public void run(){


                    try {
                        String text = null;
                        int server_port = 12356;
                        byte[] message = new byte[66];
                        DatagramPacket p = new DatagramPacket(message, message.length);
                        DatagramSocket s = new DatagramSocket(server_port);


                        while(text == null){
                        s.receive(p);
                        text = new String(message, 0, p.getLength());
                        txt.setText("Messed up.");
                        }

                        if(text != null){
                        Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
                        txt.setText("Received");
                        s.close();
                        }

                    }
                    catch (SocketException se) {
                        se.printStackTrace();
                        Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
                        txt.setText("Socket Error");
                    }
                    catch (IOException ioe) {
                        ioe.printStackTrace();
                        Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
                        txt.setText("Network Error");
                    }

                 }
         }).start();

}}

Any help is appreciated, thank you!!

Updated code:

package com.ti.cc3x.android;

import java.io.IOException;
import java.net.*;
import java.util.Arrays;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class buttonListener extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listener);
        WifiManager wifi = (WifiManager)
        getSystemService(Context.WIFI_SERVICE);
        WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
        final TextView txt = (TextView) findViewById(R.id.txt1);
        lock.acquire();

    new Thread( new Runnable(){
        public void run(){


                   try {
                       String text = null;
                       int server_port = 12356;
                       byte[] message = new byte[66];
                       DatagramPacket p = new DatagramPacket(message, message.length);
                       DatagramSocket s = new DatagramSocket(server_port);


                       //while(text == null){
                       s.receive(p);
                       text = new String(message, 0, p.getLength());
                       txt.setText("Messed up.");
                       //}

                       //if(text != null){
                       Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
                       txt.setText("Received");
                       s.close();
                       //}

                   }
                   catch (SocketException se) {
                       se.printStackTrace();
                       Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
                       txt.setText("Socket Error");
                   }
                   catch (IOException ioe) {
                       ioe.printStackTrace();
                       Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
                       txt.setText("Network Error");
                   }

                }
        }).start();




    lock.release();
    }
    }

Upvotes: 0

Views: 200

Answers (1)

Willis
Willis

Reputation: 5336

In your answer you mention that the test packets you are sending are being broadcast. Have you tried to see if you can receive the packet if you send it directly to the IP address of your device instead of broadcasting it? There is a chance that your socket is working fine but just not receiving broadcast packets. By default, the Android Wi-Fi stack filters out multicast packets in order to conserve power. If you can receive a packet sent directly to your IP address then that means all you have to do is enable the receiving of multicast packets by acquiring a MulticastLock, which you can find more information on here: Android device not receiving multicast package

If you are still unable to receive a direct packet then there is probably another issue at play, but I would check this first.

Upvotes: 2

Related Questions