tradle / react-native-udp

node's dgram for react-native
MIT License
341 stars 154 forks source link

Bug trying to opening the UDP Socket after WiFi connection in Android #243

Closed daaanigm closed 1 year ago

daaanigm commented 1 year ago

Description

Android bug. After a new Wi-Fi connection with react-native-wifi-reborn, if you open a new udp socket all seems to work but the method onMessage doesn't receive data.

NOTE: If you open the udp socket before the Wi-Fi connection it works as expected. I suspect it may be related to the fact that the connection wifi-reborn makes is an "app-wifi" and not a "system-wifi" connection when using WifiNetworkSpecifier class.

Could help

Opening the udp socket once we create a Wifi (peer to peer) connection with wifiNetworkSpecifier (react-native-wifi-reborn) we can see this log in Android debug console:

WifiService setWifiEnabled not allowed for uid=10350

Opening the udp socket before Wifi connection with wifiNetworkSpecifier (react-native-wifi-reborn) we don't get this error.

Steps to reproduce

Steps to reproduce the behavior:

  1. Use react-native-wifi-reborn and connect to a Wi-Fi network (which has service announcement udp socket)
  2. After that, create a new udp socket connection
  3. See how onMessage method is not receiving packages...

Or code:

const connectAndOpenUDPSocket = async () => {
  await WifiManager.connectToProtectedSSID(
    networkDetails.ssid,
    networkDetails.passphrase,
    false,
  ).then(
    () => {
      // success
    },
    () => {
      // failed
      throw new Error('Not connected')
    }
  );

  try {
    nsdSocket = dgram.createSocket({ type: 'udp4', reusePort: true});
    nsdSocket.bind({ port: MULTICAST_PORT });
    nsdSocket.once('listening', () => {
      nsdSocket?.addMembership(MULTICAST_GROUP_ADDRESS);
    });

    nsdSocket.on('error', error => {
      // The network was lost.
      if (nsdSocket) {
        nsdSocket.close();
      }
    });

    nsdSocket.on('close', () => {
      // The network was lost.
      nsdSocket = undefined;
    });

    nsdSocket.on('message', async msg => {
      // Do something
      // In Android this method is not receiving data if we open the udp socket after wifi connection
    });
  } catch (error) {
    console.log(error);
  }
};

Current behavior

Android: Opening UDP Socket before wifi connection all works. Opening UDP Socket after wifi connection all seems to work but onMessage don't receive data.

iOS: (iOS works in both cases)

Expected behavior

If we open a UDP Socket after wifi connection it should work.

Relevant information

OS Android
react-native 0.64
react-native-udp 4.1.7
github-actions[bot] commented 1 year ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.