Reputation: 747
I have an OpenPicus Flypport
Device which sends JSON
String when queried with proper string over Port No. 50000 (UDP).
Well thats not mandatory just for an information
What I am doing is I query the device with proper json, and the device replies back. But my Android code do not receives the reply. I have taken all the required WIFIManager Code
, and Wifi Permissions
.
To check weather the Android is broadcasting or not, I wrote a java program running on my laptop. There I found that both the Android and Flyport Device are communicating.
I have written following code:
SENDING FROM ANDROID
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++;
RECEIVING ON ANDROID
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;
}
If you will see then I have printed some variables.
Upvotes: 0
Views: 95
Reputation: 20196
Yes, as EJP says if you are using multicast then you need to join a multicast group. And folr clarity you should show your use of the MulticastLock and the Android permissions you have requested.
Having said that appears that Multicast support in Android is not as solid as some of use might hope. See http://codeisland.org/2012/udp-multicast-on-android/
I originally thought whether it actually works out or may be device dependent. It was not working on my Nexus5. See https://code.google.com/p/android/issues/detail?id=51195
But then I found that resetting my router made the Android devices start picking up the multicast posts. I suspect there is some edge case here that Android devices are regularly hitting.
Upvotes: 1