0

我正在broadcast message从我的 android 手机向网络中的设备发送一个消息,并等待他们回复。

网络中有n许多设备。

现在我需要创建发送回复的设备列表。

我正在使用Runnable接口并接收回复数据包。

如何准备设备列表?

因为线程不会返回任何值。我想准备一个随机列表。类似于蓝牙搜索。

我尝试使用synchronized方法

public static synchronized void addDevice(DeviceDetails device) throws IOException
{
    listIsChanged=true; //if the list changes that means if there is a device then this method will be called.
    deviceList = device;
    Log.v("device added with 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 to 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 to 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");
        }
        finally
        {
            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;
    }
}
4

1 回答 1

0

最后,我使用AsyncTask登陆,代码如下:

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()
    {
        ....
        ....
        Some Changes that I need to do before calling Network Threads
        ....
        ....
    }
    @Override
    protected Void doInBackground(Void... arg0)
    {
        new Thread()
        {
            public void run()
            {
            ....
            ....//Network Thread
            ....

            publishProgress(device);//call to onProgressUpdate() method
            }
        }.start();
    return null;
    }
    @Override
    protected void onProgressUpdate(DeviceInformation... d)
    {
        super.onProgressUpdate(d[0]);
        /***
           * Changes in User Interface
           **/
    }
}

这将对用户界面进行更改。

于 2013-05-14T12:45:45.140 回答