title: Send Broadcast UDP but not receive it on other Android devices
body: <p>I am trying to develop an app that sends some broadcast messages and receives some answers from the other android devices. I am having some trouble receiving the UDP messages from the other devices. I should mention that this code worked on Gingerbread but on JellyBean it's not working anymore and I do not know what might be the problem.</p>
  
  <p>Here is where I send the broadcast message (I know the other devices listen on port 5000) : </p>
  
  <pre><code> private void sendUDPMessage(String msg) {
  
      try {
          DatagramSocket clientSocket = new DatagramSocket();
  
          clientSocket.setBroadcast(true);
          InetAddress address = InetAddress.getByName(Utils.getBroadcastAddress());
  
          byte[] sendData;
  
          sendData = msg.getBytes();
          DatagramPacket sendPacket = new DatagramPacket(sendData,
                  sendData.length, address, 5000);
          clientSocket.send(sendPacket);
  
          clientSocket.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>And here is where I receive it :</p>
  
  <pre><code>private void start_UDP()
  {
      try {
              serverSocketUDP = new DatagramSocket(5000);
          }
      catch (Exception e) {
  
          Log.i(LOGTAG, "Exception opening DatagramSocket UDP");
      }
  
      final byte[] receiveData = new byte[1024];
  
  
      while(runningUDP) {
          Log.d(LOGTAG, "Waiting for Broadcast request in ServerUDP.");
  
          final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  
          serverSocketUDP.receive(receivePacket);
  
  
                  byte[] sendData = new byte[1024];
                  InetAddress address = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  if(!receivePacket.getAddress().getHostAddress().equals(Utils.getLocalIpAddress()))
                  {
                      String req = new String(receivePacket.getData(), 0, receivePacket.getLength());
  
  
                      Log.d(LOGTAG, "Received UDP message : "+req+" from: "+receivePacket.getAddress().getHostAddress());
                  }
                        }// while ends
         }//method ends
  </code></pre>
  
  <p>I should mention that these 2 functions are separate in 2 different threads so I can send and receive simultaneously.</p>
  
  <p>I also acquire the following locks:</p>
  
  <pre><code>    powerManager =(PowerManager)context.getSystemService(Context.POWER_SERVICE);
      wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK ,LOGTAG); // PARTIAL_WAKE_LOCK Only keeps CPU on
      wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
      wifiLock = wifiManager.createWifiLock(3, LOGTAG);
      multicastLock = wifiManager.createMulticastLock(LOGTAG);
  
      wakeLock.acquire();
      multicastLock.acquire();
      wifiLock.acquire();
  </code></pre>
  
  <p>And the permissions on the Manifest file : </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_SETTINGS"/&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/&gt;
  </code></pre>
  
  <p>I have tested if the messages are sent using wireshark and tcpdump and they are sent. Moreover, what is even more strange, I receive the broadcast messages that I send (but I discard them because I dont need to process the messages sent from myself) but I dont receive the broadcast messages sent from the other devices (which should have the same format, only the source address would be different and the message contained, either way should not affect the broadcast message).</p>
  
  <p>Please let me know if you have any ideas because I really ran out of anything else I could try. Any help would be appreciated. Thanks!</p>
  
  <p>EDIT:
  I have made some tests and even if when I run on each of the phones ifconfig wlan0 and it says something like </p>
  
  <pre><code>  ifconfig wlan0
    wlan0: ip 169.254.17.28 mask 255.255.0.0 flags [up broadcast multicast]
  </code></pre>
  
  <p>which means that the interface is active and the IP is set and can receive broadcast messages and multicast msgs but when I use</p>
  
  <pre><code>                 InetAddress in=InetAddress.getByName("169.254.17.28");
              if (in.isReachable(1000))
                  Log.i(LOGTAG, "host is reachable");
              else
                  Log.i(LOGTAG, "host is not reachable");
  </code></pre>
  
  <p>It shows in the logs host is not reachable.</p>
  
  <p>This is where I turn on the Wi-fi </p>
  
  <pre><code>    private void startWifiAdhoc() {
  
      WifiManager wifiManager =     (WifiManager)SharingFileService.context.getSystemService(Context.WIFI_SERVICE);
      String command="";
      if (condWifiAdhoc == false) {
  
          condWifiAdhoc=true;
          wifiInterface = Utils.getWifiInterface();
  
  
          wifiManager.setWifiEnabled(true);
          localIP = Utils.getLinkLocalAddress();
      }
      else
      {
          wifiManager.setWifiEnabled(true);
          localIP = Utils.getLinkLocalAddress();
      }
          // Set wifi ad-hoc
          command = context.getFilesDir().getPath()
                  + "/iwconfig " + wifiInterface + " mode ad-hoc essid "
                  + "mcp" + " channel " + "1" + " commit\n";
  
          Log.i(LOGTAG, command);
          Utils.rootExec(command);
  
  
          Log.i(LOGTAG, "Ip address used :" + localIP);
          command = context.getFilesDir().getPath()
                  + "/ifconfig " + wifiInterface + " " + localIP
                  + " netmask 255.255.0.0 up\n";
  
  
  
          Log.i(LOGTAG, command);
          Utils.rootExec(command);
  
  }
  </code></pre>