Android Wi-Fi Direct: re-connect to a device without re-Discovery - java

I know how Wi-Fi Direct works and what is the Discovery phase, because i read the entire Wi-Fi Direct specification v1.1.
When i want to connect to a device in Android, i must start the discovery phase. When onPeersAvailable in triggered, i can connect to one of these peers.
Now i want to disconnect and re-connect quickly to the same peer, without to re-execute the discovery.
This scenario is possible? For example saving channel information and using java reflection to set the channel and start quickly the connection?
I know, it's a strage question :)

its valid question, and it appears that when starting the connection, the connected device must be on current discovered peers list, and it is is not, then the connection requests will fail. Thus you can do the connection only after proper discovery.

Related

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.

Create bluetooth NAP and PANU in PAN for Android and communicate with each other on basis of IP

I am new to Android and Bluetooth. I am trying to set up a PAN network with PANU and NAP(GN) in the same using BNEP protocol of Bluetooth. My main intention is to make the a device make a NAP so that it can connect to TCP-IP and the other PANUs in piconet
will interact with the device in the NAP. I can use the Java reflection to support this. I am using android.bluetooth.BluetoothPan class for the connection as PAN.
So I followed the steps in following sequence for set up:
a) I called the constructor for BluetoothPan and created the instance. [Object creation happens successfully with onServiceConnected() handler receiving profile value of 5]
b) I set the setBluetoothTethering() to true [After setting I check the status using isTetheringOn() function and the state is true]
So I think now this app will make the device work like a PANU ? Am I correct ? If no, what else I need to do ?
Now my question is how to make this application support NAP ?
As Bluetooth Tethering is on, so is it already in NAP ?
If so, can I see the IP or port number for communication allocated to device when it is made to NAP. I cannot find a way for this.
My main intention is make a server device act like a NAP with some IP. and make some client devices with PANU, Now based on IP of NAP, I can run service discover in PANU side based on IP and interact.
Before, I had already set up communication between service and client using listenUsingRfcommWithServiceRecord() [Server] and listenUsingRfcommToServiceRecord() [Client] and many references in developer.android for setting up communication using this methods. That is very easy way to set up client-server communication and uses SDP. I am not using that here.
I want to do it through BNEP only
Note: After checking through ADB debugging I found that when it is connected to bluetooth tethering, the IP allocated can be seen by "adb shell ip route" and you can see the allocated IP for NAP in BT tethering. Now how to get this IP is my question or next task
After I set up the tethering setBluetoothTethering() and make the PAN network using the connect() apis I was able to set up the PAN.
After that as TCPIP communication was already happening, I checked the NetworkInterface and called getHostAddress() to get the IP info.

Will http resume after network down?

I am testing Http connection and I found that the behavior is weird. I want to test whether the http connect will continue after network is enabled/disabled. The only way for me to test this is to disable the network adapter.
For example, before I press the button to startup a http connection, I disable the network adapter first, after the code perform http.connect(), I re-enable the network adapter back (within the allowed timeout), but at last, the timeout still thrown, I thought the connect will be still valid before timeout ?
How do you all handle this issue since nowadays mobile app (android, IOS) will need to overcome a lot of network down when 3G is not available for a short-while.
Yes and no.
Actually this depends on so many different components that an answer is very hard.
One working example: Consider a network connection is established, then it goes down and is restarted. Still there are packets on the way and reach the target after restart. Now it is possible that the error correction systems in TCP simply catches up and connection goes on. Remove Ethernet cable from a computer and reconnect it some seconds later and you will see this works. BUT: Usually mobile devices get a new IP for reconnects which makes that impossible.
In between your device and the network are many components including routers, transparent proxies, firewalls and so on. All of these have different timeouts, some sent messages which can stop or drop connections, some don't. Some even block such messages. So while it is possible it is not reliable.
Your example: When you do a connect() when the network is down, I would expect an immediate refusal usually. But maybe DNS causes a delay here, so you get a timeout instead. I doubt there are cases where a connect() during a downtime will continue by itself when line is online again.

How do I connect two android devices with a central server?

I'd like to know how a central server works in connecting two devices. I'm assuming that when the device application starts up, it should register its IP address and other pertinent information (username) with the server. When it wants to connect to another device, it should look to find the address of another device on the server, maybe with a get request. Then set up a to connect to a socket. If the device application closes, it should unregister from the server. Is this correct?
That's pretty much correct.
Because one or both devices are probably behind firewalls (including NAT), you have to assume they won't actually be able to connect directly to each other, so it won't be as simple as opening a socket to the other device once you find out its registered address. You will either have to try firewall-traversal techniques (which will usually be successful with UDP but not with TCP) or have a helper that isn't behind a firewall (which could be the same as the registration server or something else) carry all the data between the devices that wish to communicate.
Also, you will want to have the registration server time out registrations and the clients periodically refresh them, because clients won't always have a chance to deregister themselves on the server when they terminate or lose access to the network.

Networking in Android

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.

Categories