Networking in Android - java

I know in Android you can do networking and I've done it but I've only been able to do it when the phone is connected to a WiFi network. I'm wondering if is there a way for my Activity to connect to my server without having an active WiFi connection.
It would kind of be a pain to market my game if you must be connected to a WiFi network in order to play with other people. When I tried connecting to my server without an active WiFi connection i get this error:
java.net.SocketException: No route to host
All answers are welcome. Thanks in advance!

In order to make network calls and use network resources, you need to be connected to a network: either with "cellular" data, such as 3G or EDGE, or WiFi.
However, Android should handle most this behavior pretty well behind the scenes.
For more information, check out Android's Connectivity Manager, Content Provider, and Java's Sockets.

Related

Accessing android app through TCP/IP by PC connected to android hotspot

I'm creating an Android app which is Mod-Bus TCP slave simulator and I have a problem when trying to connect from pc to it while sharing a connection via wifi hotspot.
I tried using gateway IP but it does not work. When phone and pc are connected to the same wifi (other than themselves) everything is working fine but I need to make it also work with hotspot running and pc connected to it. Is it possible? How?
App is using modbus4j lib.
Actually, I don't think so that Modbus TCP works on mobile hotspot too because recently when i was working on MB TCP I found this. I tried to connect with the ip and gateway of my mobile hotspot but it didn't work this could happen because our mobile hotspot works on DHCP so the gateway is not static. But Modbus TCP works very fine on Wifi router ip and gateway.
Your problem has nothing to do with java or android or Modbus, but it is a problem of understanding how routing works on the internet and on internal networks (your WiFi network is an internal network)
The problem is that the ports waiting for connections on the internal network, like your android device, are not accessible from the internet.
To make them accessible, one or more NAT rules must be defined in the WiFi router. But this will only work if your internet provider assigns you a public IP, which is less and less frequent due to the shortage of public IPs.

How to use android to connect to an ESP8266 access point without internet in a stable way?

I have a Wifi Micro controller ESP8266 which also has an access point. I have referred to other blogs which talk about programmatically connecting to an access point and tried both the way
Using the exposed standard APIS
wifiManager.enableNetwork(netId, true);
wifiManager.saveConfiguration();
wifiManager.reconnect();
Using the APIs which have # hide on them
wifiManager.connect(netId, ActionListener)
The problem I am facing is that some time after i connect to the access point its getting disconnected from the esp8266 access point and connecting back to my router in both the above mentioned methods.
If i connected via the top bar or through the settings app its connecting in a stable way and never disconnects and I even get the notification
Wi-Fi has no internet access
I don't get the no internet access notification when i connect programmatically. How is the settings app able to connect to the access point in a stable way while my App is not able to.
I am running Androidn 6.0.1 API 23
If you specifically request using that Network once before it disconnects, it will stay connected indefinitely:
ConnectivityManager.bindProcessToNetwork(Network)
Network.bindSocket(Socket)
Network.openConnection(URL)
Also see my answer on how to synchronize the timing between network connection and access: https://stackoverflow.com/a/52304308/4969370
I figured 2 main issues with Android connecting to ESP8266 or ESP32 hotspot. I was using AP-STA dual mode. I read up that since ESP8266 and ESP32 have single radio handling AP and STA the connection will be fragile and not very stable which is why my android device was getting disconnected from AP during configuration.
The solve for this is keep the ESP* device in SOFT_AP mode during configuration and STA mode when its connected to the Access point and never in AP-STA dual mode. This completely solve my stability issues.

Accessing remote device in android

I want to access remote device(PC) connected in LAN or wi-fi in android device,using ip address device username and password.is there any way for doing this?
what will be the right way for starting this.
Thanks in advance.
You can simply use Socket to do that, just set up a ServerSocket on your PC, let the android open a TCP Socket connection with your PC and sending data vice versa. One thing, this will only apply in the SAME wifi or network, you can't do that on 3G due to the NAT restriction.
Check out this link: http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/, you can set up your Server part on Java, not neccesary to be Android. Hope this helps.

Cant identify WiFi Direct peer when device is connected to a WiFi Interface

I am working on WiFi Direct in android Jelly bean 4.1 with samsung(google) nexus device. I am able to discover the peer and connect with it when I am purely on wifi direct and when the devices are not connected to any other external interfaces.
But as a test I connected both devices to my Router, then started wifi direct on one device and created a group. After that I started next device and when I call discover peers its a success.
Immedialtely I call requestPeers and its returning an empty list of peers. But the same is working when both devices are not connected to any other interface.
How can I do this properly?
Thanks
Why do you want to connect to a router in addition to your two devices?
My devices require that I disable regular WiFi when using WiFiDirect which leads me to believe they cannot be used at the same time.

Get IP of Android for P2P connection

Would it be possible to get the IP of Android Phone for P2P connection, Actually i want to connect two android Phones via Sockets ?
I believe you'll need a central server which helps with peer discovery. That's not all even getting IP might not be enough you'll have to work around issues like Net traversal ..this may help Android: NAT Traversal?

Categories