Android: how to write code to connect router hardware - java

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

Related

Connect Android App to IoT device using TCP through Wi-Fi

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.

PC to Android communication over USB cable

I need to make communication between PC software (written in java) and android app over usb cable. The PC software will send some data (Strings) to Android app witch will accept those strings and do some action depending on the message received.
What is the best way to do this?
I am looking all over the internet and can't find any solution to this. If someone has example of this communication i would be very grateful.
I already done this communication over sockets, put i need to cover situation if there is no router or internet connection.
Thank you
Check following article. I guess similar question to your on SO.
If you want to communicate between Android USB device and some USB host you need to use the accessory mode (https://developer.android.com/guide/topics/connectivity/usb/). But this mode requires special driver support on the USB host side (which is your PC).

Android 9: Ethernet and Wifi simulataneously

I have a scenario where I want to use wifi to connect to the internet (comfortably via androids settings) and ethernet for accessing local network which is not connected to the internet.
Thus I want to have both ethernet and wifi up simultaneously and use the routing table to send all the local requests to the ethernet and the default to wifi.
I found out android disables wifi when ethernet is enabled...
There has been a fix for an old android version on https://android.googlesource.com/platform/frameworks/base/+/refs/heads/pie-release/services/core/java/com/android/server/ConnectivityService.java
Problem: In Android 9 the file looks different and I cannot find the lines mentioned...
I am using a Khadas Vim3 and their android build.
Can you please help me, which lines need to be changed?
This is the file:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android10-c2f2-release/services/core/java/com/android/server/ConnectivityService.java
Thanks in advance

RXTX issue with Bluetooth COM Port

I have an application made of a device I made to connect through USB emulating a Serial port. Now, I need to connect the same device via bluetooth so I'm using a RN-42 (Bluetooth to serial port) in my device to send the data over RF.
Then I pair my device in Windows with the laptop's internal BT module which creates to COM ports a Outgoing and a Incoming one. Then if I connect using Br#ay's terminal I receive the expected data when I connect to the Outgoing COM port.
The problem is that when I use my Java app where I am using the last RXTX libraries, I can discover the COM port (which is the problem that everyone has, at least thats all what i found in forums) and it seems to connect but then I don't receive no data. Using the same exact program but connecting using a cable it works just fine as the terminal does.
I also tried to use the BlueCove libraries, I can find the bluetooth devices but I don't really know how to discover it services and then connect to it using RFComm. I can't find a complete example of how to do that.
Any help will be appreciated!
I'm getting similar problem with default Windows Bluetooth stack.
With Toshiba stack communication is ok.

Android WiFi sending and receiving information

Sorry for kind of a dummy question, but I'm wondering how to work with WiFi connection via android. My application needs to transfer some data (both receiving and sending) with PC or with other android device using WiFi. How should I organize the process? I mean, how should I establish connection, then should I open a socket for data transfering, etc. Is there any way to do it without internet connection, smth like bluetooth, except wifi.p2p (which is only available for android 4.0)? Would be grateful for as detailed manual as possible.
Every network connection even if only in the local network or localhost reqieres the INTERNET permission.
Bluetooth and NFC might be an option, but I have no experiance with that.

Categories

Resources