3

I recently tried to port my application that requires receiving (udp) broadcast messages. My udp receiving code works on pc (windows/linux), but with android there seems to be some problem receiving broadcast messages, however, i seem to be able to send broadcast messages with android and receive acknowledgment packet from pc version.

Tried to find solution but anything close to the answer is that it MIGHT not work on my device and there was really any info on how i can be sure that this is the case, so felt that there should be thread for making sure it is indeed hardware/firmware problem and not problem in my code, like if there is something android specific that i forgot to enable? Tested with galaxy S (kitkat) and Galaxy s3 mini (kitkat) and both have same problem

(under onCreate)

    WifiManager wifi;
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiManager.MulticastLock wifilock =                             wifi.createMulticastLock("compvter.qft");
    Log.i("wifilock", "multicastLock.isHeld() = " +wifilock.isHeld());
    Log.i("wifilock", "multicastLock.toString() = " +wifilock.toString());
    wifilock.acquire();
    Log.i("wifilock", "multicastLock.isHeld() = " + wifilock.isHeld());
    Log.i("wifilock", "multicastLock.toString() = " + wifilock.toString());
    if (Settings.socket==null) 
    {
        initsocket();
    }

     UdpServer udpserver = new UdpServer();
       udpserver.start();

Under udpserver class

public volatile boolean keepServerRunning=true;

public void run() 
{
    boolean none=true;  
    try {

        byte[] buffer = new byte[Settings.udppacketsize];
        while (keepServerRunning==true)
        {

            DatagramPacket inpacket = new DatagramPacket(buffer,buffer.length);
            Settings.socket.setSoTimeout(1000);

            try 
            {
            Settings.socket.receive(inpacket);              
            none=false;
            }
            catch (SocketTimeoutException e)
            {
            none=true;
            }

            if (none==false) 
            {
            handler handler = new handler(Settings.socket, inpacket);
            handler.start();
            }
        }
        Settings.socket.close();

    } catch (IOException e) 
    {           
    }
}

Logging for wifilock gives:

02-01 16:34:07.516  17365-17365/com.test.oappi.qft I/Wifilock﹕ multicastLock.isHeld() = false
02-01 16:34:07.516  17365-17365/com.test.oappi.qft I/wifilock﹕ multicastLock.toString() = MulticastLock{ 425b3ff8; refcounted: refcount = 0 }
02-01 16:34:07.516  17365-17365/com.test.oappi.qft I/wifilock﹕ multicastLock.isHeld() = true
02-01 16:34:07.516  17365-17365/com.test.oappi.qft I/wifilock﹕ multicastLock.toString() = MulticastLock{ 425b3ff8; held; refcounted: refcount = 1 }

Manifest permissions

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

Since the wifi state definitely changes, does this mean that i really have two android phones unable to receive broadcast messages or does API version i try to develop matter since i am trying to develop to Api lvl 10. If someone has idea how to check devices broadcast receiving capability please tell it.

Compvter
  • 27
  • 5

0 Answers0