0

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();
    }
    }
JDSlimz
  • 113
  • 1
  • 9

1 Answers1

2

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.

Community
  • 1
  • 1
Willis
  • 5,308
  • 3
  • 32
  • 61
  • I tried a few things on the multicast side and ended up with "192.168.123.255 is no a multicast group" I am not doing a "multicast" I am having the other device "broadcast" on the 192.168.123.255 so that every device can see it. – JDSlimz Mar 03 '15 at 13:22
  • Broadcast is a type of multicast. It simply means sending from one to many. As I mentioned above, try sending a packet directly to your IP address. Go to your Wi-Fi settings, find your phone's IP address and see if you can receive a direct packet. – Willis Mar 03 '15 at 15:46
  • I must be doing something wrong. I integrated the multicast code into my app and tried to send on multicast address and even direct as you suggested and no dice. Added updated code to question. – JDSlimz Mar 04 '15 at 03:32
  • I changed the edited code because I realized that I could try another route. SO with the multicast group, when I transmit the app does nothing. With the current "Updated code" when I transmit the app crashes. Unfortunately with my phone in the state it is in I cannot usb debug to find out what is crashing it... – JDSlimz Mar 04 '15 at 03:42
  • Your information was spot on. I needed to put my work in a asynctask. – JDSlimz Mar 07 '15 at 04:06