0

I have an Android App and an IoT device where the connectivity and the communication will be through Wi-Fi. The IoT device will provide an hotspot (without Internet) so that we can connect our Mobile Wi-Fi with that device hotspot. After connecting the Mobile with Wi-Fi, when we open the app, the app will try to find the device using TCP socket connection to connect and communicate to the IoT device.

The App works fine and communicates with the device successfully. But the problem here is, the connection and communication is not happening if the Mobile Data is turned ON. The app works only with the Mobile Data turned OFF.

I guess the App is searching for the IoT device through Mobile Data since the Mobile data has an active internet connection and the Wi-Fi does not. I want the App to look for the IoT device only through Wi-Fi even if Mobile Data is ON.

I have a similar App in React Native which has the same functionality and had the same problem. I fixed it with using react-native-tcp-socket package which has an option to make the app search through a particular interface (which has the options wifi, ethernet and cellular).

Both the Apps have an hardcoded IP Address for connecting to the IoT device. This works only when Mobile Data is turned Off. In the React Native App, I fixed it using that package mentioned previously with the Dynamic IP address by getting it from the WifiManager. When I tried the same with the Android App it did not work.

I looked into the package's native code and tried to replicate the same, but getting the below error:

java.net.ConnectException: failed to connect to /192.168.1.2 (port 6378) from /:: (port 39495): connect failed: ECONNREFUSED (Connection refused)
 Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)

Any Suggestions?

Is any other way of implementation possible to achieve the output?

Joshua
  • 1,167
  • 1
  • 14
  • 32
  • 1) Is the Android device rooted and/or controlled by Device Management? 2) Have you seen [this answer to enabling and disabling mobile data programmatically](https://stackoverflow.com/a/53058929/295004) – Morrison Chang Feb 23 '21 at 07:24
  • @MorrisonChang, Android device is neither root nor controlled by Device Management. The answer which you have suggested is not related to my issue. I need to make the Mobile App find and connect to the IoT device even if the Mobile Data is ON. – Joshua Feb 23 '21 at 08:11
  • Then you should show code of what change you've tried that didn't work. And if you want to link to what section of `react-native-tcp-socket` was used might be useful. – Morrison Chang Feb 23 '21 at 08:19
  • I feel that adding the code here will be too large. So I have updated the question with more information. – Joshua Feb 23 '21 at 10:03

1 Answers1

0

The solution is to bind the socket to the Wi-Fi network. We must create a listener and get the Network from the listener's result.

So, Whenever we open or reopen a socket we must bind that socket to Wi-Fi network. This is what I was missing. After adding this, it worked fine.

network.bindSocket(socket);

And, another update is, in my case, The dynamic IP address didn't work but the pre-defined hardcoded IP address works and I am unable to find the reason, unlike my previous case which was vice-versa.

Joshua
  • 1,167
  • 1
  • 14
  • 32