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

4

8

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

Duffey answered 29/5, 2019 at 16:38 Comment(4)
You cannot prevent them from turning stuff off, the only thing you can do is when your app loads tell the user you need these things on (and why) and not let them use the app until they doSubtype
you are going in wrong way (don't force users) . maybe you need to change your strategy . you can observe GPS status and doing proper action in each cases ...Siskin
It's a corporate app. So, the company may use a MDM to deploy the app and control devices. If it so, they probably can avoid some settings to be disabled.Nanceynanchang
For GPS you can try the solution mentioned in below link How can I enable or disable the GPS programmatically on Android?Deniable
S
6

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

Subito answered 29/5, 2019 at 16:55 Comment(4)
Oh, that's really going to be a problem for me. Can I do this at least if I have root access?Duffey
Yeah but that is not easy i believe. What is the usecase you are trying to solve?Subito
It is an application for corporative use and I need to prevent users from circumventing it.Duffey
Device owner lets your app control off/on when needed. But i don't think you can block users from manually turning it off. Do give it a try and let us know your findings.Subito
L
4

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

Literalism answered 6/6, 2019 at 14:52 Comment(0)
D
1

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

Delanty answered 7/6, 2019 at 10:6 Comment(0)
F
0

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

Fatling answered 5/6, 2019 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.