tradle / react-native-udp

node's dgram for react-native
MIT License
340 stars 155 forks source link

Works on Debug mode but not in Release mode Android #252

Closed gibo77 closed 11 months ago

gibo77 commented 11 months ago

Description

Works on Debug mode but not in Release mode Android.

Steps to reproduce

Steps to reproduce the behavior: npx react-native run-android ---mode release --deviceId Android The App loaded, but the data did not came in on Release mode.

npx react-native run-android --deviceId Android Debug mode will work. Data is being collected from UDP.

I run the Android Studio in the background while I run the App in Release build. I looked at the Logical from the Android Studio. It says that the bind did not happen. There was no binding at all! No multicast happening. It seems that there is some authorization that needs to be done.

Troubleshooting done: I updated the AndroidManifest.xml in android/app/src/main by adding below. I found this in the example code in GitHub of this tradle/react-natie-udp.

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

same thing. It works in Debug build but not in Release build.

Or code:


//**************
//This function will send UDP message to devices and catch the message they emit.
//***************
let Send_UDP_Multicast = async () => {

  const message = Buffer.from('StarApple,'); //StarApple is the first Identifier with comma separation
  const socket = dgram.createSocket('udp4');

  let RawMessageUDP = [];
  let countMessage = 0;
  let ParsedMessage = [];

 // socket.bind(62777, '239.255.255.250'); //Android don't need this for UDP to work. iOS requires this.

 // socket.bind(62777); //Android don't need this for UDP to work. iOS requires this.

  socket.bind(62777, err => {
    if (err) throw console.log(err);
  });

  /* socket.once('listening', () => {
    console.log('listening');
  }) */

  /* socket.on('error', err => {
    console.log(err.stack);
    socket.close();
  }); */
  //socket.send(message, 0, message.length, err => {
  socket.send(message, 0, message.length, 62777, '239.255.255.250', err => {
    if (err) {
      console.log(err);
      socket.close();
    }
    else console.log('Message sent UDP');
  });

   socket.on('message', (data, rinfo) => {
    //Console: socket-x, bound to address: 0.0.0.0, port: 65000 max
      console.log('On Message: ');

  });
socket.close();

}; //Send_UDP_Multicast

Current behavior

The UDP data did not came in when in Release build. Debug build it will work.

I get this in Android Studio logcat. { message: 'Socket is not bound.', key: 'sendError' }

Expected behavior

I should see the binding. But no binding happened.

socket-0 binding, address: 0.0.0.0 port: 62777 socket-0 bound to address: 0.0.0.0 port: 62777

Screenshots

Relevant information

{ "name": "smartdimmer_ios", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "lint": "eslint .", "start": "react-native start", "test": "jest" }, "dependencies": { "@react-native-community/slider": "^4.4.2", "@react-navigation/native": "^6.1.7", "@react-navigation/native-stack": "^6.9.13", "babel": "^6.23.0", "base64-js": "^1.5.1", "buffer": "^6.0.3", "react": "18.2.0", "react-native": "0.72.4", "react-native-gesture-handler": "^2.12.1", "react-native-reanimated": "^3.5.1", "react-native-safe-area-context": "^4.7.2", "react-native-screens": "^3.25.0", "react-native-size-matters": "^0.4.0", "react-native-splash-screen": "^3.3.0", "react-native-udp": "^4.1.7", "react-native-vector-icons": "^10.0.0", "upgrade": "^1.1.0" }, "devDependencies": { "@babel/core": "^7.21.4", "@babel/runtime": "^7.21.0", "@tsconfig/react-native": "^3.0.0", "@types/jest": "^29.5.1", "@types/react": "^18.0.38", "@types/react-test-renderer": "^18.0.0", "babel-jest": "^29.5.0", "eslint": "8.39.0", "jest": "^29.5.0", "metro-react-native-babel-preset": "^0.76.2", "prettier": "^2.8.8", "react-native-clean-project": "^4.0.1", "react-test-renderer": "18.2.0", "typescript": "^5.0.4" }, "jest": { "preset": "react-native" }, "browser": { "dgram": "react-native-udp" }, "resolutions": { "react-native-gesture-handler": "2.12.1" } }

gibo77 commented 11 months ago

I was able to make some headway and the step is a bit weird. I added debug=true in the create socket as suggested in previous issues to troubleshoot. But I was able to make a bind on the port. I think what may have happened is the debug=true introduced some delay and somehow helped the bind process. I could be wrong. I will expound on this idea and I think I have the tool to make that happen. UDP process is truly an Asynchronous process and has to be treated as such.

Update: I did make it stable with the idea from above.