I would like to be able to receive broadcast datagrams. I am using
the following code:
public void Receive()
{
EnableMulticast();
String text;
byte[] message = new byte[1500];
DatagramPacket p = new DatagramPacket(message,
message.length);
try {
DatagramSocket s = new DatagramSocket(45454);
s.receive(p);
text = new String(message, 0, p.getLength());
Log.d("GabySoft", "message:" + text);
s.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now if I send a datagram to the ip address of this device, it receives
the message. However, if I send a broadcast datagram, I do not get
it.
By doing web searches, I found that it appears that most phones have
the ability to receive broadcast datagrams turned off to conserve
power. I found the following code which supposedly turns that ability
back on, and I am calling it at the beginning of the 'Receive'
function:
private void EnableMulticast()
{
if (lock == null)
{
try
{
android.net.wifi.WifiManager wifi =
(android.net.wifi.WifiManager)
getSystemService(android.content.Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("HeeereDnssdLock");
lock.setReferenceCounted(true);
lock.acquire();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
However, even after calling this function (which succeeds), I still do
not receive broadcast datagrams.
Can anyone tell me how to make this work?
Thanks.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en