0

我有一个设备,它在通过端口号 50000 ( UDP )使用正确的字符串进行查询时OpenPicus Flypport发送字符串。JSON

好吧,这不是强制性的,只是为了提供信息

我正在做的是使用正确的 json 查询设备,然后设备回复。但我的 Android 代码没有收到回复。我已经采取了所有必需的WIFIManager Code, 和Wifi Permissions.

为了检查 Android 是否正在广播天气,我编写了一个在笔记本电脑上运行的 java 程序。在那里我发现 Android 和 Flyport 设备都在通信。

我写了以下代码:

从安卓发送

        System.out.println("Sending Flyport");
        FlyportDriver flyDriver = new FlyportDriver();
        sendMulticastFlyport = new Thread(new FlyportSender(set.getFlyportIP() , flyDriver.getDeviceDiscovery()));
        //flyport.getDeviceDiscovery is JSON String. Sending is fine. No problem
        sendMulticastFlyport.start();
        System.out.println("Flyport Send: "+flyDriver.getDeviceDiscovery());
        deviceStartCounter++;

在安卓上接收

private class DeviceSearcher extends AsyncTask<Void, DeviceInformation ,Void>
{
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        //TableLayout deviceTable = (TableLayout) findViewById(951357);
        TableRow tr = new TableRow(getApplicationContext());
        LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        tr.setLayoutParams(layout);
        TextView tv = new TextView(getApplicationContext());
        tv.setText("Searching...");
        tv.setLayoutParams(layoutParam);
        tv.setTextColor(Color.WHITE);
        tv.setVisibility(View.VISIBLE);
        tr.addView(tv);
        deviceTable.addView(tr);
    }
    @Override
    protected Void doInBackground(Void... arg0)
    {
            new Thread()
            {
                public void run()
                {
                    MulticastSocket socketFlyport=null;
                    try
                    {
                        socketFlyport = new MulticastSocket(50000);
                        socketFlyport.setSoTimeout(1*20*1000);
                        byte[] inBufFlyport = new byte[1024];
                        DatagramPacket inPacketFlyport = new DatagramPacket(inBufFlyport, inBufFlyport.length);

                        while(true)
                        {
                            System.out.println("Listening..."); //Printing
                            socketFlyport.receive(inPacketFlyport); //not receiving

                            String msg = new String(inBufFlyport, 0, inPacketFlyport.getLength());
                            Log.v("Received:","Received Flyport From :" + inPacketFlyport.getAddress() + " Msg : " + msg);
                            String IP = inPacketFlyport.getAddress().toString();
                            System.out.println(Utils.getIPAddress(true));
                            try
                            {
                                System.out.println("Received");
                                DeviceInformation device = new DeviceInformation(3, IP.substring(1), msg, getApplicationContext());
                                publishProgress(device);
                            }
                            catch(Exception e)
                            {
                                System.out.println(e.toString());
                            }
                            Log.v("Received:","Received Flyport From :" + inPacketFlyport.getAddress() + " Msg : " + msg);
                            System.out.println();
                            Thread.sleep(2500);
                        }
                    }
                    catch(Exception e)
                    {
                        Log.v("Exception:","During Receiving Flyport: "+e.toString());
                        publishProgress((DeviceInformation)null);
                    }
                    finally
                    {
                        socketFlyport.close();
                    }
                }
            }.start();
        return null;
    }

如果您会看到,那么我已经打印了一些变量。

4

1 回答 1

1

是的,正如 EJP 所说,如果您使用多播,那么您需要加入多播组。为了清楚起见,您应该显示您对 MulticastLock 的使用以及您请求的 Android 权限。

话虽如此,Android 中的多播支持并不像某些用户希望的那样可靠。见http://codeisland.org/2012/udp-multicast-on-android/

我最初认为它是否真的有效或可能取决于设备。它不适用于我的 Nexus5。请参阅https://code.google.com/p/android/issues/detail?id=51195

但后来我发现重置我的路由器使 Android 设备开始接收多播帖子。我怀疑这里有一些 Android 设备经常出现的边缘情况。

于 2014-01-27T03:14:06.570 回答