2

I am working on an Android app that will receive multicast packets from a network that already outputs reliable multicast data on 239.255.x.x . I have verified that my device can receive multicast with another application. I'm new to Java and Android but I did confirm that my original code to gather the Multicast info worked in a java application and have been struggling to get everything working in the Android side of things. I have permissions set in the manifest,

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

the networking portion of code runs in an Asynctask as to not crash the app but my Multicast.receive() calls all result in a time out.Is there something else I am missing or something that prevents multicast sockets from working in the asynctask class?

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          new getACN.execute();
        }
    });


}

 public class getACN extends AsyncTask<Void, Void, String> {



    @Override
    public String doInBackground(Void... Void) {
        byte[] buf = new byte[1000];


            try {
                WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                MulticastLock multicastLock = wifiManager.createMulticastLock("sACN");

                multicastLock.acquire();




                InetAddress group = InetAddress.getByName("239.255.0.3");
                DatagramPacket recv = new DatagramPacket(buf,buf.length);

                MulticastSocket sock = new MulticastSocket(5568);

                sock.joinGroup(group);

                sock.setSoTimeout(1000);
                sock.receive(recv);
                sock.leaveGroup(group);

            } catch (IOException e) {


                return e.getMessage();


            } catch (SecurityException e) {
                return  e.getMessage();


            }
            return Arrays.toString(buf);

    }

    @Override
    protected void onPostExecute(String result){
        TextView textView = (TextView) findViewById(R.id.text01);
        textView.setText(result);
    }

}

}

jmartin
  • 21
  • 1

0 Answers0