java - Android - Listen to devices over Socket and Create their List -


i sending broadcast message devices in network android phone , waiting them reply.

there n number of devices in network.

now need create list of devices sending reply.

i using runnable interface , receiving packets of reply.

how prepare list of devices?

as thread not return value. , want random list prepared. similar of bluetooth search.

i tried usingsynchronizedmethod

public static synchronized void adddevice(devicedetails device) throws ioexception {     listischanged=true; //if list changes means if there device method called.     devicelist = device;     log.v("device added ip:",device.getdeviceip()); } 

multicastreceiver.java

public class multicastreceiver implements runnable {     datagramsocket socket = null;     datagrampacket inpacket = null;     byte[] inbuf;     public multicastreceiver()     {         try         {             socket = new datagramsocket(wificonstants.port_no_recv);             inbuf = new byte[wificonstants.dgram_len];             inpacket = new datagrampacket(inbuf, inbuf.length);             socket.setsotimeout(2*60*1000);         }         catch(exception ioe)         {             system.out.println(ioe);         }     }     @override     public void run()     {            //system.out.println("listening");         try         {             for(long stop=system.nanotime()+timeunit.seconds.tonanos(2); stop>system.nanotime();)             {                 log.v("yoyo","yoyo honey singh");                 socket.receive(inpacket);                 log.v("checking receive","received");                 string msg = new string(inbuf, 0, inpacket.getlength());                 log.v("received: ","from :" + inpacket.getaddress() + " msg : " + msg);                 devicedetails device = getdevicefromstring(msg);                 devicemanagerwindow.adddevice(device);                 thread.sleep(1000);             }         }          /**try         {             socket.setsotimeout(5000);             socket.receive(inpacket);             log.v("checking receive","received");             string msg = new string(inbuf, 0, inpacket.getlength());             log.v("received: ","from :" + inpacket.getaddress() + " msg : " + msg);             devicedetails device = getdevicefromstring(msg);             devicemanagerwindow.adddevice(device);             thread.sleep(1000);         }**/         catch(exception e)         {             log.v("receiving error: ",e.tostring()+" no packets received");         }                 {             socket.close();         }     }     public devicedetails getdevicefromstring(string str)     {         string type;         string ip;             type=str.substring(0,str.indexof('`'));             str = str.substring(str.indexof('`')+1);             ip=str;         devicedetails device = new devicedetails(type,ip);         return device;     } } 

devicedetails.java

package com.example.devicecontrolpanel;  public class devicedetails {     private string devicetype;     private string ipaddr;     public devicedetails(string type, string ip)     {         this.devicetype=type;         this.ipaddr=ip;     }     public string getdevicetype()     {         return this.devicetype;     }     public string getdeviceip()     {         return this.ipaddr;     } } 

finally this, landed using asynctask , code follows here:

public class testactivity extends activity{ protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_test);     new devicesearcher().execute(); }  private class devicesearcher extends asynctask<void, deviceinformation ,void> {     @override     protected void onpreexecute()     {         ....         ....         changes need before calling network threads         ....         ....     }     @override     protected void doinbackground(void... arg0)     {         new thread()         {             public void run()             {             ....             ....//network thread             ....              publishprogress(device);//call onprogressupdate() method             }         }.start();     return null;     }     @override     protected void onprogressupdate(deviceinformation... d)     {         super.onprogressupdate(d[0]);         /***            * changes in user interface            **/     } } 

this make changes user interface.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -