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?
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.
Related
I am hitting otp api's from Emulator its work but if I am using real device then getting failed to http connecting error found.
2020-11-06 13:33:23.603 24840-24840/a3.amp35.in E/Genrate: failed to connect to /192.168.132.101 (port 80) from /26.81.35.26 (port 39596) after 100000ms
need help
Your device is outside of your local network. Api is on 192.168.132.101 while device is out given net 26.81.35.26
Either expose your API to the outside world (eg. port forward it or host in on globally accessible device) or connect your device to your local network (eg, use the same WIFI, or via VPN)
Also firewall settings on the 192.168.132.101 might block the incoming connections from different subnets.
And probably many more possible causes...
This requirement for automation devices.
Hello, I want Send - Receive data using Static IPAddress and Port with WiFi connection. (It's working fine for me using TCPIP Socket Programming.)
Now, My phone connects with router wifi, but here Internet connection not available. Then after I want to do the above process between mobile devices and automation devices.
Is there any possible way to connect mobile devices and automation devices using only wifi connection (Wifi Internet connection not necessary in this case.)
I am using Android Studio. I want to write code to connect router hardware. Does anybody can help? I have written code to connect router then I am facing error following.
E/debug: failed to connect to /192.168.0.101 (port 60123): connect failed: ETIMEDOUT
Your question is very generic. There are multiple approaches when you are talking to connect to a router.
If you want to perform operations as connecting to a router,listing all available networks available,creating your own network(hotspot) You should checkout the official documentation of android on WifiManager -https://developer.android.com/reference/android/net/wifi/WifiManager.html
Or You can see this example -> how to get available wifi networks and display them in a list in android after you get the basic undestanding.
If you want to connect to other devices through router you should study sockets and TCP/IP protocols,Socket implementation on JAVA.
Hope this might help.
Peace
I am following this tutorial to implement this: http://www.veereshr.com/Android/AndroidToServlet
I got my server up and running and I am able to open its localhost link in my browser. However, when I run the Android app on my Nexus 5, I get this error:
11-26 01:34:46.501 1092-1340/com.example.nav.wificlient D/Exception:java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: EHOSTUNREACH (No route to host)
11-26 01:35:29.171 1092-1557/com.example.nav.wificlient D/Exception: java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
Both my laptop (which is running the server program) and my phone are connected to the same network (called eduroam) on a college campus. I made sure that my firewall settings are not blocking incoming connections on my laptop.
I'm wondering if maybe the campus network settings are blocking this somehow...but I'm not sure. Any ideas on how I can narrow down what may be going wrong?
10.0.2.2 is a unique address directly interpreted by the emulator to reference the machine running the emulator. This is useful as "localhost" would simply reference the emulator itself.
As you are not running your android application on an emulator, but rather an actual device, you need to change your application so that, instead of connecting to 10.0.2.2, it connects to the local IP of the computer hosting the server.
See here for more information.
I need to connect an android device to my java program via socket connection. The device is working as server. The problem is that while trying out socket connection I need to give IP address and port number of server.
Is there something like a static IP address for an Android device to connect? If not, is there any alternative way to establish a socket connection between the device and my program?
As the device has to have a connection to the same network as the computer, it has to have an IP address configured. So you have to use that one.
The used port is defined by the server application running on your device.
If you have the problem that anyhow a normal network connection is not possible, but you have an ADB connection, you can forward local pots to the device and let your server listen:
http://developer.android.com/tools/help/adb.html#forwardports
Over a mobile connection (GPRS, EDGE, UMTS, HSDPA) there will be no practicable way to establish a connection from somewhere to your device (instead you have to do it the other way, while canceling the whole client-server directive), in cause of the used IP sharing. In that case you have a problem of which I'm not aware if it's possible in general, not to mention how you could achieve this.
Otherwise you simply have to configure a static IP for your device when connected to your local network, or you have to evaluate the actual IP of your device every most of the time, while using it with this configuration:
Resolve it by the device itself
Resolve InetAdress from DhcpInfo
Documentation for DhcpInfo
Resolve by using ADB command