Send Broadcast UDP but not receive it on other Android devices

后端 未结 2 690
臣服心动
臣服心动 2020-12-04 20:03

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 message

2条回答
  •  [愿得一人]
    2020-12-04 21:03

    I got this working by using a method described here to calculate the broadcast address: https://code.google.com/p/boxeeremote/wiki/AndroidUDP

    Here's my receiver:

    try {
      //Keep a socket open to listen to all the UDP trafic that is destined for this port
      socket = new DatagramSocket(Constants.PORT, InetAddress.getByName("0.0.0.0"));
      socket.setBroadcast(true);
    
      while (true) {
        Log.i(TAG,"Ready to receive broadcast packets!");
    
        //Receive a packet
        byte[] recvBuf = new byte[15000];
        DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
        socket.receive(packet);
    
        //Packet received
        Log.i(TAG, "Packet received from: " + packet.getAddress().getHostAddress());
        String data = new String(packet.getData()).trim();
        Log.i(TAG, "Packet received; data: " + data);
    
        // Send the packet data back to the UI thread
        Intent localIntent = new Intent(Constants.BROADCAST_ACTION)
                // Puts the data into the Intent
                .putExtra(Constants.EXTENDED_DATA_STATUS, data);
        // Broadcasts the Intent to receivers in this app.
        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
      }
    } catch (IOException ex) {
      Log.i(TAG, "Oops" + ex.getMessage());
    }
    

    And here's my sender:

        public void sendBroadcast(String messageStr) {
        // Hack Prevent crash (sending should be done using an async task)
        StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    
        try {
          //Open a random port to send the package
          DatagramSocket socket = new DatagramSocket();
          socket.setBroadcast(true);
          byte[] sendData = messageStr.getBytes();
          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, getBroadcastAddress(), Constants.PORT);
          socket.send(sendPacket);
          System.out.println(getClass().getName() + "Broadcast packet sent to: " + getBroadcastAddress().getHostAddress());
        } catch (IOException e) {
          Log.e(TAG, "IOException: " + e.getMessage());
        }
      }
    
      InetAddress getBroadcastAddress() throws IOException {
        WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifi.getDhcpInfo();
        // handle null somehow
    
        int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
          quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
        return InetAddress.getByAddress(quads);
      }
    

提交回复
热议问题