Reading WiFi properties from APs without getting connected - android

I would like to know if there is any way in Android to read some configurable property from a WiFi access points without getting connected/authenticated to the network. Basically I would like to list only the networks that implements/advertises a specific web service I am working on.
Thanks,
- Rafael

You can retrieve WifiManager instance:
WifiManager wifiManager = ( WifiManager ) mContext.getSystemService ( mContext.WIFI_SERVICE ) ;
You can also get NetworkInfo object:
ConnectivityManager connManager = ( ConnectivityManager ) context
.getSystemService ( Context.CONNECTIVITY_SERVICE ) ;
NetworkInfo mWifi = connManager.getNetworkInfo ( ConnectivityManager.TYPE_WIFI ) ;

You can use the WifiManager.
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.
This is the link:
http://developer.android.com/reference/android/net/wifi/WifiManager.html

Related

How to get details about disconnected network?

is there a way to get details about disconnected network using android broadcast receiver or networkcallback ?
EXTRA_NETWORK_INFO, getNetworkInfo(NetworkType) are deprecated.
getAllNetworks() Returns an array of all Network currently tracked by the framework but not the disconnected network.
yes, first register Broadcast receiver like this:
final IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(receiver, intentFilter);
when receiver is:
new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// this part is trigered when there is some network changes, do checks here
}
};
also don't forget to unregister it.
Yes. NetworkInfo class is deprecated from API Level 29 . Instead, the ConnectivityManager class can be helpful here. It helps in monitoring the network connections, and notify the application when network connectivity changes or when connectivity to a network is lost. It also facilitates by allowing applications to know the state of the available networks and allows applications to request and select networks for their data traffic.
However, the below list of methods are deprecated in ConnectvityManager class:
getNetworkInfo returns connection status about a particular
network type.
getAllNetworkInfo returns connection status information of all network types supported by device.
getActiveNetworkInfo returns currently active default data network
network.
The ConnectivityManager.NetworkCallback is currently actively available to determine the network change status. This base class for NetworkRequest callbacks is used for notifications about network changes which can be extended by applications that are in need of network change notifications.
The below list of methods are supported :
getAllNetworks returns an array of all networks currently
tracked.
getActiveNetwork returns a network object
of currently active default data network.
The NetworkCapabilities class shall help in representation of the capabilities of an active network.
In general, whenever the system invokes onAvailable(Network), it shall pass the network that is available and whenever the system invokes onLost(Network), it shall pass the network that was lost which refers to the particular network that was lost (disconnected) and the argument tells you which network got lost (disconnected).
The onCapabilitiesChanged gets invoked immediately after onAvailable and this can help in determining capabilities of the available network like whether it is cellular network or a WiFi network by querying the NetworkCapabilities with hasTransport() and the appropriate transport constant like TRANSPORT_CELLULAR or TRANSPORT_WIFI (network of interest).
The below snippet takes into consideration the above information and helps in determining whether you have cellular or wifi network connectivity which in turn enables one to confirm whether the disconnected network(lost) is indeed not in use.
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI));
}

what does "isRoaming" in the Wifip2p networkinfo mean?

What is the meaning of isRoaming in Wifip2p network info mean?
[http://developer.android.com/reference/android/net/NetworkInfo.html#isRoaming()]
It tells that usage of data on that network may incur extra charges? Which data is it refering to and how the usage will lead to extra charges?
Well, if you're outside your home country/state and you turn on the phone it will automatically establish a GSM connection to the roaming partner network -> TelephonyManager.isNetworkRoaming() will return true. If data roaming is disabled or 'use only 2g' is enabled NetworkInfo.isRoaming() will return false, because no data connection is established. If you switch data services on NetworkInfo.isRoaming() will return true as well, since now both (GSM and data connection) are established and in roaming mode.
Hence, setting data-roaming on/off will make NetworkInfo.isRoaming() return true/false.
UPD: It is used when one wants to know whether the user has enabled Data on Roaming while on 2G/3G network.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
//[edit: check for null]
if(ni != null) {
//...Here check if this is connected and available...
if (ni.isRoaming())
{
// user has enabled data even while he is on Roaming!!
}
}
Since he asked specifically about Wifi P2P the correct answer that this will be an unused field as roaming only applied to cellular network and does not apply to Wifi P2P network information.
The only fields of networkinfo you should be using concerning wifi P2P is: isConnected (after which you normally request the connection info from the wifip2pmanager).

How do I send data only by WiFi

In my downloader I have to let the user choose whether he wants to download by WiFi or by using network connectivity. I know I can do this via:
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
and then use the isConnected() method to see if I have WiFi connectivity. I also have another class named Sharable, not an Activity, which is supposed to hold all the data that is to be shared across Activities. These 'sharable' things include connections to database and so on.
How can I get this WiFi information in this Sharable class ?
Can this not be achieved without it extending Activity ?
If not, where am I supposed to instantiate the various variables ? In the onCreate?
Just pass Application context to Sharable.

List all networks in android?

I am gonna to implement a application, to list all the networks and manually select one of them( similar to the stock one ), can some one what kind of api can I use or what documentation should I refer to , or any reference sites ?
Thanks in advance !
You have to use ConnectivityManager
The primary responsibilities of this
class are to:
Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
Send broadcast intents when network connectivity changes
Attempt to "fail over" to another network when connectivity to a network
is lost
Provide an API that allows applications to query the
coarse-grained or fine-grained state
of the available networks
Get an instance of this class by calling:
Context.getSystemService(Context.CONNECTIVITY_SERVICE).
Maybe you will have a look at NetworkInterface and its enumeration , too

Google Android - how to figure out if 3g and 2g is turned on

I'm developing a simple application for the Google Android to turn on and off the wifi or 3g or 2g.
I see http://developer.android.com/reference/android/net/wifi/WifiManager.html#isWifiEnabled()
that you can see if the wifi is enabled or disabled and also us
http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
to turn on and off the wifi.
I'm wondering if it's possible to do the same for 3G and for 2G/GPRS?
I know it's possible because you can turn off 3G and left 2G on.
2G/3G
To determine your network type use:
TelephonyManager.getNetworkType();
here's some example code:
bool is3G = (manager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
Docs for the class can be found at: TelephonyManager
On/Off
To check if your telephone radio is on or off use:
ServiceState.getState();
To set it use:
ServiceState.setState(STATE_POWER_OFF);
It's unclear whether the setState method exists on all devices and functions in all states. There is no documentation for this method. Documentation for the class can be found at: ServiceState
This issue might also be relevant: http://code.google.com/p/android/issues/detail?id=1065
You can also use ConnectivityManager. Something like that:
ConnectivityManager connectivityManager =(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

Categories

Resources