我有一个设备,它在通过端口号 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;
}
如果您会看到,那么我已经打印了一些变量。