I have an Android device that has a WIFI adapter but no screen and no input devices. I want to use it as a silent server in my local network that runs / offers some services and will eventually be configurable via the internet. The service app will be pre-installed and launch upon boot. The problem is the initial configuration (mainly join the local WIFI network).
My idea is: Develop a Java GUI app that is used for the initial setup via USB.
Let's assume I get the desktop app to find and communicate with the Android service via the ADK (I will be happy to share here once I succeed). That way I will be able to use my screen & keyboard to configure it. Now the problem is:
How can I (on the Android device) obtain a list of WIFI netorks that the Android device finds and how can I tell it to connect to a selected network (with a provided password)?
Is there maybe a better way to achieve what I am trying to?
The WifiManager class should be helpful:
This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It deals with several categories of items:
The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
Results of access point scans, containing enough information to make decisions about what access point to connect to.
It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.
This is the API to use when performing Wi-Fi specific operations. To perform operations that pertain to network connectivity at an abstract level, use ConnectivityManager.
Related
I'm developing a mobile app that uses WiFi without internet connection (in order to communicate to an IOT device) and it uses cellular data as well (see TL;DR section for more).
The problem -> some devices don't automatically switch their default route to cellular (rmnet interface) while others are capable.
The workaround -> create a *"local" VPN in order to manage and re-routing network traffic via cellular interface (there are apps like Speedify that can do this by assigning priority to interfaces)
The question -> there is a simple way to accomplish this or it's pretty tricky? When I say "simple" I mean the calls of several well documented API and when I say "tricky" I mean to starting read and parse routing table with customized rules depending on every vendors?
"*local" = a VPN without web server or VPN gateway because I'm not actually interested to do the VPN's job (I seem to have understood that I need a VPN level to manage network traffic of others app but I'm pretty newbie of VPN topic)
TL;DR
My goal is to remaining connected to my access point (in order to call its APIs) AND use cellular data for all others requests I.e: login via my backend, use Google Maps and so on.
Handle this scenario it's quite easy because combining network objects, socketFactory and bindProcessToNetwork I'm able to do this.
Troubles come for others app like YouTube, WhatsApp that stop working because as default network they are using my access point without internet (Android doesn't switch default interface to a cellular)
I'm going crazy because seems that each vendor (or Android OS version?) handles "WiFi without connectivity" with cellular active in a different way.
There are certain devices that all work fine (similar to iOS) but there are others that it's a nightmare.
See this question for further details
Thanks for reading, I really love Android but those issues make me sadder every day more and I'm thinking to pass to the Dark Side of mobile development (iOS).
You've the chance to change my mind ;)
The Problem
Is it possible to have active open sockets on multiple network interfaces simultaneously on Android? I want to send/receive datagrams on wifi and mobile data networks simultaneously (obviously on different sockets). Theoretically nothing should prevent this, IP allows multihomed hosts and from what I see there is at least one App which proves this is possible in practice on Android: http://speedify.com/
Things I tried so far
Attempt 1
Use ConnectivityManager.getAllNetworks() to enumerate all networks and then use Network.bindSocket() to bind my socket to a specific network I need. Unfortunately ConnectivityManager.getAllNetworks() only returns an array with one single entry. If wifi is active this entry corresponds to wifi network, if wifi is inactive this entry corresponds to mobile data network. Looks like this function is broken and simply does the same thing as ConnectivityManager.getActiveNetwork().
Attempt 2
Use ConnectivityManager.getAllNetworkInfo() to enumerate all network infos. This correctly returns an array of NetworkInfo which includes wifi and mobile data. Unfortunately I cannot see a way to go from NetworkInfo to Network so that I can then call Network.bindSocket(). Only the reverse mapping is possible from Network to NetworkInfo which is of no use. The documentation also says getAllNetworkInfo() is deprecated in API level 23 and recommends to use getAllNetworks() instead, which unfortunately is not a proper substitute (see Attempt 1 for explanation).
Attempt 3
Use NetworkInterface.getNetworkInterfaces() to enumerate all interfaces, then enumerate the local IP addresses so that I can then use InetAddress, create InetSocketAddress and call DatagramSocket.bind(). Again the problem is the enumeration only returns 1 IPv4 address. Depending on whether wifi is active or no I either get my wifi IPv4 address or my mobile data IPv4 address but never both at the same time.
I am stuck and don’t know what else to try. I am sure this must be possible since Speedify obviously does it but I cannot find a way.
Why I Need This
The purpose is to access servers which are only accessible via one of the networks but not the other and my tasks does require active simultaneous connections so suggestion like “don’t do it” really won’t help.
[Update]
I am not asking about multipath TCP. All I need is host multihoming which is a simpler use-case that allows to have several different TCP connections (or UDP datagrams) on different network interfaces, but each connection strictly working through one network interface only.
I want to build a data sharing application on android using wi-fi like "SHAREit" but I did not get where I start from.
So if any clear me the technical concept of "SHAREit" it was very much efficient to understand.
When we want to send a file we saw the available receiver device with name.
Is the name displayed as SSID or other?
How we broadcast name and other info to display on sender device?
When I select a device to send a file then what happens?
What API actually I can use to and what for please explain me simply.
Please all resources and links that I could run to test.
I've made an attempt to write a library called SHAREthem to simulate how SHAREit works.
Library facilitates P2P file sharing and transfers between devices using WiFi Hotspot. It also supports app to web sharing if receiver has no app installed. Hope it helps to you understand technicals involved in file sharing using WiFi Hotspot.
Since there are many moving parts to this library, i made a blog with implementation details. Will try to cover a few components here:
HotspotController
HC uses Java Reflection since there are NO APIs available on Android for enabling/disabling Hotspots. Functionalities include:
Controller creates an OPEN Wifi hotspot configuration with an SSID which can intercepted by Receivers to recognize SHAREthem senders including port and sender names.
Restores user Hotspot-Configuration when Share mode is disabled
Provides a list of connected WiFi clients.
SHAREthem Server
A tiny HTTP server extended from NanoHttpd, serves the sender data to receivers using IP address as hostname and works on port assigned by user or system by default.
SHAREthem Service
Android service which manages lifecycle of SHAREthem-server and also handles foreground notification with stop action.
UI (Activities)
Android activities to handle share/receive actions
Receiver - provides UI to list the files available to download. Posts
a download request to Android Download Manager to start file
downloads.
Sender - displays IP, Port & connected clients info along with file
transfer status for each connected client(Receiver).
Use the Android Wi-Fi P2P libaries, and start with the doc on this page. It tells you how use Wi-Fi P2P for service discovery, which takes care of item 1 in your requirements. Basically, you have each device transmit a DNS-SD TXT that can contain user ID info, etc. Devices can see its contents without having to establish a socket, which is what you will later do to accomplish item 2 in your requirements.
I have two+ Android devices. I would like the devices to send instructions to the other devices using Wi-Fi Direct, while at the same time being able to access the internet via their cellular data connection. It seems that using the Wi-Fi Direct interface is being treated as the primary connection, disabling the other connections, just like how Wi-Fi disables the cellular data connection.
How can I programmatically allow both to operate at the same time?
Currently the Wi-Fi Direct receiver is implemented according to Google's Android Developer documents. I was under the assumption that I could use Wi-Fi Direct much like I can use Bluetooth alongside other connections.
Yes it is possible to do that, you can check some articles over here . One of the developers in XDA has developed an application to do that called Super Download Lite and paid version of Super Download - Fast! . You can get details from here and here or you can do some hacking Here is the source code of application which connect to 3G and WiFi to share the connection of the internet (But I am not sure how much it will help you). or try decompiling or revers engineering the Super Download app
I'm doing a network App using tun as virtual network device on Android. I close other available interfaces such as eth0 (for WLAN) and rmnet0 (for 3G/GPRS) but keep my tun0 device alive and add a rule in route table
ip route add default dev tun0
so I can redirect all internet transfer into tun device (what I'm doing here is the same as OpenVPN). And then I saw the data transferring when I ping other IP. Browser and some other Apps work fine.
But some of Android Apps will check the Connectivity by ConnectivityManager before accessing netwrok. Since I close WIFI & 3G/GPRS, ConnectivityManager considers there is no connection, those Apps won't access network anymore.
So my question is:
How to CHEAT android ConnectiviyManager, make it believe that there is still an available connection (for any type)?
I don't think you can do this, and you wouldn't want to, it's a system-wide service. Imagine if your app relied on internet connectivity, but some malicious app was able to tell you there was and make your app unable to complete it's actions. It could cause a serious problem.
If your app needs to believe there is a connection in some states, I suggest you interface the connectivity / network classes, you can read the true state but then return whichever value you deem fit to your app