Android Q: Get list of configured network - android

The method to getConfiguredNetworks has now been deprecated in Android Q. I was using this feature in order to determine whether a Wifi network has any security such as WifiConfiguration.KeyMgmt.WPA_EAP. All of these are now deprecated and i am not sure whether there is any alternative. Google suggests to use WifiNetworkSpecifier.Builder#build() but this is for connecting to new network, not for my use case.

WiFi scanning allows you to get the list of networks in range.
Then you can parse ScanResult.capabilities, which should - among others - contain the encryption info of the network (WPA/WPA2/etc).
Granted, it would be more convenient to have a proper object rather than a string, but here we are.

Related

Change Android Networkscore / force Android to choose certain network

as far as I understood Android uses the NetworkScore class to select between available networks. Every available network is assigned with various flags and the Connectivity module in AOSP uses the policy logic for network selection found in the NetworkRanker class to determine from those flags, which network to choose.
I also got that it is not possible to directly select the network operator from API (for security reasons).
Would it be possible to change those flags programmatically (after set by NetworkScore) to "force" a certain network to be selected by the system?
Would it be possible to change the policy logic in the NetworkRanker class to block Network operators of a certain country?

How to get the physical channel configuration of the cellphone via programming

I want to get the PCI of both primary serving cell and secondary serving cell in 5G NSA, but it seems that I cannot get what I want using getAllCellInfo(). It seems that I need to parse the physical channel configuration as below:
{{mConnectionStatus=PrimaryServing,...,mRat=LTE,...,mPhysicalCellId=123},
{mConnectionStatus=SecondaryServing,...,mRat=NR,...}
Does anyone know how to get the (real-time) configuration? Or can I get the PCIs in another way? Thanks very much!
About PhysicalChannelConfig
The only possible way how to obtain PhysicalChannelConfig is via TelephonyManager.registerTelephonyCallback method. You can for example pass an instance of TelephonyCallback.PhysicalChannelConfigListener and you'll start obtaining what you need.
Please note that permission Manifest.permission.READ_PRECISE_PHONE_STATE is required, so your app needs to be a system app of carrier-privileged app.
There were some attempts to adjust this protection level, current status can be seen here. But as of Android 12 there's no way how to get PhysicalChannelConfig if you are a regular developer.
About PCIs
You can sometimes get PCIs of serving LTE and NR NSA cells via getAllCellInfo() as you mentioned. Sometimes there's one instance of CellInfoNr with PCI is present. This behaviour is device-specific.
Generally speaking - Android does not provide any official API you request.

What's the difference between determining if a network is metered using NetworkCapabilities and ConnectivityManagerCompat?

I have found that there are two ways to detect if the current network is metered:
With NetworkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED) NetworkCapabilities
With ConnectivityManagerCompat.isActiveNetworkMetered ConnectivityManagerCompat
So, what's the difference between these methods? And when to use each?
First of all: ConnectivityManagerCompat.isActiveNetworkMetered is just the androidx backport of the framework's ConnectivityManager.isActiveNetworkMetered.
On platforms that have that method (API 16+), the compat version just calls the framework method directly. Otherwise, it tries to guess based on the connection type: Wi-Fi, Bluetooth, and Ethernet connections are assumed to be non-metered, while anything else is assumed to be metered. This guess isn't necessarily correct (Wi-Fi networks can be metered, for instance), but it's the best guess you can make on that API level. You should use this in the (unlikely these days) case where you need to target API 15 or below.
As far as the differences between ConnectivityManager.isActiveNetworkMetered and NetworkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED) go: for the active data network, they are exactly the same, as the implementation simply calls that exact method.
However, because hasCapability can be called on any network, it provides you more flexibility if you want the capabilities of networks other than the active data network.

Alternative for deprecated WifiManager.getConfiguredNetworks and WifiManager.removeNetwork methods on Android Q?

For a UiTest class setup, I need to begin with no remembered Wifi Networks.
To do this I use a combination of the WifiManager.getConfiguredNetworks() and WifiManager.removeNetwork(), both of which are now restricted and deprecated for Android Q.
Now, since I am running the tests in privileged mode (super user), the getConfiguredNetworks() method actually returns a full list of the networks, and the removeNetwork() removes networks I pass by returned WifiConfiguration's Ids, even though I run this code on an Android Q device.
TLDR:
My issue is more about these two methods being deprecated without being properly replaced, not that they now are worthless to use. At least not to my knowing, which is why I am asking; are there any way I, with my testapplication run as Super-user, can:
Retrieve all Wifi Configurations
Delete them one by one
In the documentation, one can read:
getConfiguredNetworks()
This method was deprecated in API level 29. a) See WifiNetworkSpecifier.Builder#build() for new mechanism to trigger connection to a Wi-Fi network. b) See addNetworkSuggestions(java.util.List), removeNetworkSuggestions(java.util.List) for new API to add Wi-Fi networks for consideration when auto-connecting to wifi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will return an empty list.
The solutions they've linked seem to be for the action of connecting to wifi networks, but that isn't what I am looking for.
PS, I would be at least as happy by executing adb shell commands (in superuser-mode if needed), to do the same thing. Already tried using wpa_cli list_networks to at least list all networks, but doesn't seem to be available.
Also have tried to remove data/misc/wifi/wpa_supplicant and data/misc_ce/0/wifi/WifiConfigStore.xml, to no avail.

Get the current wifi security used on Android

I'm developping an app which may send sensitive data and I want to be sure that i don't send them on a public or weak protected network.
That's why I'd like to get the current security used on wifi network on Android.
I found this post but I'm not sure of the accuracy of the solution.
Indeed, the allowedKeyManagement method seems to return the supported protocols, but it's not explicitely said to return the current active protocol beeing used.
Is there a sure way to get the effective protection used on the cirrent wifi network ?
Thanks
I can suggest one method
Get list of all configured network using getConfiguredNetworks API
Loop through all entries and find the current Network using WifiConfiguration.status API. The status should be CURRENT for current network
For that current network, get the allowedKeyManagement and check that it is not NONE.

Categories

Resources