Difference between NetworkInfo.isRoaming() and TelephonyManager.isNetworkRoaming() - android

I'm trying to detect wether the device is roaming or not in my app and I've found these two methods:
NetworkInfo.isRoaming()
TelephonyManager.isNetworkRoaming()
Is there an essential difference between them or can I just choose one?

I think TelephonyManager.isNetworkRoaming()is 2G-related, whether NetworkInfo.isRoaming() is 3G-related - setting data-roaming on/off is a distinct option in the android-settings, so if it's off you still may get truefor GSM (TelephonyManager.isNetworkRoaming())

Related

Bluetooth Classic on Android. How implement a filter for scanning?

In my application, I implemented the classic Bluetooth according to the official documentation: https://developer.android.com/guide/topics/connectivity/bluetooth.
By default startDiscovery() will scan for all the nearby bluetooth devices. But in many situations user/developer already knows the desired category of devices. In my case, you need to find a specific device that implements the method:
bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(SERVICE_NAME, MY_UUID_INSECURE);
Ideally, you need to implement the search and connect two devices in one click. How can I select the desired device from the list of found devices? This is for the convenience of using the application, so you do not have to choose from a variety of unnecessary devices. It will be a kind of filter like in BLE. But we are talking about Bluetooth Classic. How can I use SERVICE_NAME, MY_UUID_INSECURE, BluetoothClass to accomplish this task?
EDIT:
I found several solutions to this problem. But they're all not perfect. For now, I just exclude from the list of devices those that have a device.getType() == 2 (DEVICE_TYPE_LE).
Option 1
Search for a device that broadcasts the service with the same UUID as mine. To do this, use the fetchUuidsWithSdp() method for the found devices. Example implementation: https://stackoverflow.com/a/37070600
The disadvantage of this method. Time. You should wait until you receive BluetoothAdapter.ACTION_DISCOVERY_FINISHED before you make any calls to fetchUuidsWithSdp(). It will take 12-18 seconds. In addition to this one must wait for each subsequent call to fetchuuidsWithSdp() to complete, and then give a call to this method for another device. It will take about 3 seconds per device. In total, it can take a very long time to find the right device.
Option 2
Change the device name to a private key or a special name that the client can use to identify the device.
bluetoothAdapter.setName(name);
The main thing is not to forget to return the device name to the original.
saveName = bluetoothAdapter.getName();
Example implementation: https://stackoverflow.com/a/40138077/4716092
The disadvantage of this method. Changing the device name is not so easy in reality. If you do not return the old name of the device, the user may be upset.

How to prevent users from turning off GPS, Wifi and Bluetooth?

I'm developing a tracking application and I need to prevent users from turning off the basic sensors used to determine the location. I can not modify the devices ROM or have root access (or at least it would be very desirable to had not), but I thought of using the Device Administration API to perform these functions through the Profile Owner or Device Owner modes. I'm basically looking for a method to block these functions in Android settings.
I'm unsure about whether this is possible and how to do it, I have not found examples in GitHub for applications that have implemented this. Could anyone give me a light, some example or specific documentation?
I tried to follow these three documentations, without success in finding a solution to this specific feature:
https://source.android.com/devices/tech/admin
https://developer.android.com/guide/topics/admin/device-admin
https://developers.google.com/android/management/introduction
This is an excerpt from what I've been trying:
setUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, true);
setUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, active);
setUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH, active);
private void setUserRestriction(String restriction, boolean disallow){
if (disallow) {
mDevicePolicyManager.addUserRestriction(mAdminComponentName, restriction);
} else {
mDevicePolicyManager.clearUserRestriction(mAdminComponentName,
restriction);
}
}
DISALLOW_CONFIG_BLUETOOTH
Added in API level 18
public static final String DISALLOW_CONFIG_BLUETOOTH
Specifies if a user is disallowed from configuring bluetooth. This does not restrict the user from turning bluetooth on or off. The default value is false.
This restriction doesn't prevent the user from using bluetooth. For disallowing usage of bluetooth completely on the device, use DISALLOW_BLUETOOTH.
This restriction has no effect in a managed profile.
Key for user restrictions.
Type: Boolean
You cannot prevent them from turning GPS, WIFI and Bluetooth off. What you can do is have an implementation as below or use this library.
https://github.com/KI-labs/gps-permission-checks-livedata
You can't, obviously for security reasons. If you want to achive something like that you'll probably need to modify the devices ROM. You should create a BroadcastReceiver and keep tracking Internet and Bluetooth connection changes, than you can properly handle it when user disconnect them pausing the service, showing a dialog, finishing the application or whatever you need to do.
It would be pretty weird if an app could have some control of user settings, imagine if you install an app, then suddently you can't disable wi-fi anymore until you unistall it. You can't do that for a good reason
Preventing bluetooth/wifi disconnection will also prevent usage of aircraft mode, that is a security issue bounded in the ROM and not overridable.
As suggested above your option is to monitor for wifi/bluetooth/gps deactivations and prompt the user with an alert.
By the way, GPS is not affected by aircraft mode, as it's a pure receiver and doesn't make active transmissions. In that case GPS will be always active and collecting informations (if active and the phone is not in power save mode, aka relying on wifi location). I suggest you to check if the user activated aircraft mode, in order to be less annoying with your alerts (air mode is mandatory in same situations, and should be considered "legal" by your application, and maybe less critical than an user voluntary disconnection
In simple words, You cannot, but you can listen to when wifi is enabled/connected, and you can prompt a dialog stating the reason.
This way it gives the user a more concise grip on what needs to be done.
Just a suggestion

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)

WifiManager.getScanResults() - clarifications (automatic scans, sleep etc)

I would like some clarifications on the behavior of WifiManager.getScanResults(), namely :
When wireless is enabled
Does android scan for access points on a fixed interval ? Can one query/change the interval ? Can one query the time of the last scan ? For a discussion see this answer
What happens when the wireless radio is turned off (sleeps) - while wifi is still enabled - will getScanResults() go on returning the last scan results ? How would one know if it's time for startScan()?
When wireless is disabled
getScanResults() would return the last scan results ? Or an empty List ? Or null (it does return null at times and this is not handled by 90% of the snippets posted in SO) ?
The answer to 1 would be true even after a reboot (the phone booting with wifi off) ?
The reason I ask is I need to get the list of the available access points periodically and I'd rather call getScanResults() at once, than WifiManager.startScan() and then getScanResults() if I really do not have to - as this involves acquiring wifi locks and the like and is rather subtle. I am not also sure how much do the API level and phone hardware come into play.
For a good discussion of difficulties see this answer
When wireless is disabled
getScanResults() will return null...
...even after a reboot (that's to be expected - this would be disputed if it did not return null in the first place)
When wireless is enabled
2.Surprisingly enough getScanResults() will return null after some time (on a Nexus One, Android 2.3.7, Cyanogen mod - but I suspect this is not really relevant). So yes, apparently one has to initiate a scan irrespective of the wireless status
I guess this is true also if the state is WIFI_STATE_DISABLING
I am still interested in the answer in the case wireless is enabled - in particular question 2, if the device is asleep. Google groups won't let me post (hint)
public List getScanResults ()
Added in API LEVEL 1
Return the results of the latest access point scan.
Returns
the list of access points found in the most recent scan. An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get valid results.

Change network selection mode programmatically

I'm trying (wondering if it's even possible) to write an app, that would change the network selection mode automatically, based on some criteria. E.g. change the network operator from Vodafone to T-Mobile (assuming that the SIM card registration will succeed, but I'm not worried about it atm)
Unfortunately, I can't seem to find any way in the API to do it. Anyone has any idea?
I assume, since it's not in the public APIs, there might still be a way to do it, if the phone is rooted. Is that true? If so, where should I look?
Thanks in advance
Sorry but you can't.
You can have a look into the TelephonyManager .
You can know the current operator: getSimOperator(Name) / getNetworkOperator(Name).
You can also check this thread saying "I learn that for the sake of security there aren't any public APIs to manage this so the only option is to send the user to the system PreferenceScreen within my app."
How about using android.telephony.CarrierConfigManager? I read about it on https://developer.android.com/reference/android/telephony/CarrierConfigManager.html and it seems to allow you to change alot of carrier-specific parameters, although the app must be signed with the certificate that has a matching signature to one on the SIM, so it can usually only be implemented by the carrier issuing the SIM. See also https://source.android.com/devices/tech/config/carrier.
I havent found an actual method to actively switch carrier, but if anywhere, I'd expect it to be there.

Categories

Resources