With a beacon close to an Android phone, is it possible to lock the phone's screen if the beacon is moved away from it? Does the Android system allow this? I've tried the tasker app but it seems it needs some plugins and I didn't find it.
With a beacon close to an Android phone, is it possible to lock the phone's screen if the beacon is moved away from it? Does the Android system allow this? I've tried the tasker app but it seems it needs some plugins and I didn't find it.
Share Improve this question asked Jan 20 at 12:33 HogHog 91 bronze badge 1- Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Jan 21 at 3:56
1 Answer
Reset to default 0Yes, it is possible to lock an Android phone when a Bluetooth beacon goes out of range.
The basic steps are:
Scan for Bluetooth Beacons using a Foreground Service. The foreground service is necessary to keep the app active in the background so it can lock the phone quickly if the beacon is no longer seen by the scan. There is no other viable way for third party apps to do this due to background Bluetooth scanning restrictions in Android. You cannot tell if a beacon disappears unless you constantly scan for it.
Watch for the scanned beacons to not be seen for several seconds, and when that happens trigger a screen lock. Using the Android Beacon Library, you can get notifications of when you "exit a beacon region", meaning it has not been seen in at least 30 seconds.
Use the Android
DevicePolicyManager
API to get permission from the user to manage the device and lock the screen. This is a bit tricky to set up and get permission from the user. You have to add some entries to your AndroidManifest.xml, and prompt the user to allow this on the first time launch. But once you do so, you can use it like this:
devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
devicePolicyManager.lockNow()
- Hook in the beacon region exit code to lock the screen.
I have a sample app on Github that does this using the Android Beacon Library for scanning. It shows you how to set up the beacon scanning and to set up the DevicePolicyManager
.
https://github.com/davidgyoung/android-lock