How to enable HSDPA programmatically in Android? - android

Is it possible to enable or disable HSDPA connection programatically in Android?
If so, how to do that? I didn't find any API in TelephonyManager.

It is not possible to enable Network(CDMA, 3G or HSDPS) programmatically as of now. As we know we can enable WiFi programatically.
But we can set the preferable network among Mobile / WiFi.
ConnectionManager.setNetworkPreference(ConnectionManager.TYPE_MOBILE/TYPE_WIFI);
Unfortunately, it is not working for me. When I tried to set TYPE_MOBILE, I am getting Security Exception even if I had required permission in my manifest.
When I tried to set TYPE_WIFI, It is not throwing exception, but while getting network preference, I am getting "-1", means there is no preferred network(No Documentation, its my prediction).

Related

WiFi suggestion API fail on Android

I've been trying to use the new Android WiFi Suggestion API with the code taken exactly from the example, but each time I try to connect to a network, when I call wifiManager.addNetworkSuggestions(suggestionsList) I get status 3 in response, which (according to this site) means IP provision failure. What does that mean? How can I handle that?
My app has all the required permissions and even some more (CHANGE_WIFI_STATE, ACCESS_WIFI_STATE, INTERNET, ACCESS_FINE_LOCATION, CHANGE_NETWORK_STATE, ACCESS_NETWORK_STATE), and the location is turned on. I tried with a few WiFi networks (all are visible in the WiFi settings), but still got the same IP provisioning error.
Had the same issue and found it quite confusing because status = 3 can mean two things: STATUS_SUGGESTION_APPROVAL_REJECTED_BY_USER or STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE.
In my case it was the latter. Basically the suggestion is remembered once submitted. I solved this by removing my suggestion before it's added:
wifiManager.removeNetworkSuggestions(suggestions)
val status = wifiManager.addNetworkSuggestions(suggestions)

Set preferred network type

I want Android source code to set preferred network type from my android application.
I can read preferred network by following code.
Settings.Global.getString(context.getContentResolver(),Settings.Global.NETWORK_PREFERENCE)
Because of user privacy and security, apps are not allowed to change the preferred network type
check out this answer: Change mobile network mode (gsm, wcdma, auto)
When you'll scroll the above link, you'll find this answer. But when you'll go to your AndroidStudio and start adding the permissions to your AndroidManifest.xml file, you'll come across this :
Which says that the permission is granted only to the system apps.

Read Preferred Network Type Setting

I am trying to read the Preferred network type setting from the device. But nowhere android API's are available.
Use case:
Trying to read the Preferred network type and connected network type so that if the device has LTE enabled and the user is forcefully switched back to the lower network(3G,2G); then there should be a notification sent to the user.
I have checked the system setting code, But it's deprecated.
Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.System.NETWORK_PREFERENCE);
Is there any alternate way to read the system secured settings(By reflection?).
And Also is it possible to write back the setting with the user permission?
Help is much appreciated.
I think the right code is:
Settings.Global.getString(context.getContentResolver(),Settings.Global.NETWORK_PREFERENCE)

Android Preferred Network List

I was under the impression that wifiManager.getConfiguredNetworks() returns the PNL of the current device, however when I click on - forget network, wifiManager.getConfiguredNetworks() still keeps that network but just removes the connection details(like security, password etc.)
Is there anyway knowing which SSID(or BSSID) is actually in PNL? (i.e my device can automatically connect to it)
I found out that this problem appeared only on specifics networks, while in others clicking on forget network actually removes the network from wifiManager.getConfiguredNetworks().
I also found out that the only things different are allowedKeyManagement.cardinality and LinkProperties (which is hidden on Android), so I used an if statement on the WifiConfiguration(=configuredNetwork):
if(configuredNetwork.allowedKeyManagement.cardinality()==1)
wifiManager.removeNetwork(configuredNetwork.networkId);
wifiManager.saveConfiguration();
break;
And for some reason it worked. If anyone have an idea whats cardinality stands for or have a better way of doing this please share.

Alternative to enabling data connection

On android 2.3 the permission <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> is not allowed anymore for non-system applications. Is there an alternative to enable/disable data connection, besides that one where you modify APN name? I found Data Enabler Widget on Android Market that does that, but I can't seem to understand how. Can anyone help me?
Thanks! - Alex Ady
I don't know how to change data connection (3g, 2g, etc), but you can enable/disable wifi connection through this:
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(boolean enabled);
I found a solution to my problem, so I'm closing this question. The alternative is to simply display the mobile settings activity if a level 10 API or higher is detected, or continue with direct enable from code otherwise. I keep the android.permission.MODIFY_PHONE_STATE, but only use it the API is under level 10.

Categories

Resources