Android Device State & Security Snippets
How to detect Active USB Connection, Work Profile, Device Encryption Status and more.
Below is a quick-reference guide for the most commonly requested Android device property and security checks.
Active USB Connection
If you want to know if a USB device (like a flash drive, keyboard) is plugged into the Android device (Android as Host), or if the Android device is plugged into a specific piece of hardware designed for it (Android as Accessory), you use the official UsbManager API.
val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
// 1. Android as Host: Checks if external USB devices are plugged INTO the phone
val isUsbDeviceConnected = usbManager.deviceList.isNotEmpty()
// 2. Android as Accessory: Checks if the phone is plugged into a USB accessory (like a car dock)
val isUsbAccessoryConnected = usbManager.accessoryList?.isNotEmpty() == trueWork Profile Detection
Checks if the application executing the code is running inside a managed Work Profile.
Note: Checking if the device has a work profile from a personal profile generally requires elevated permissions, so checking the current profile's state is the standard approach.
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
// Returns true if the app is currently running inside a managed profile
val isRunningInWorkProfile = userManager.isManagedProfileDevice Encryption Status
Checks if the device storage is currently encrypted. Modern Android devices (Android 10+) are encrypted by default out-of-the-box.
Unknown Sources Enabled
Checks if the user has allowed the installation of apps from outside the Google Play Store. The method changed significantly in Android 8.0 (API 26) from a global setting to a per-app permission.
Bootloader Unlock Status
The simplest way to check this without setting up complex cryptographic attestation is by reading system properties via the command line.
Warning: This method is simple but can be spoofed by tools like Magisk on rooted devices. For strict enterprise security, you must use hardware-backed KeyStore Attestation (SafetyNet / Play Integrity API).
Last Security Patch Date
Retrieves the date of the last installed Android security patch. This is returned as a highly readable string in a YYYY-MM-DD format.
Disable WebView Debugging
Leaving WebView.setWebContentsDebuggingEnabled(true) active in a release build makes any WebView content in the app inspectable over USB/ADB via Chrome DevTools — exposing JavaScript bridges, tokens sitting in local storage/cookies, and internal API traffic to anyone with physical access and a cable. This is a standard Android build/manifest setting, and it is the app's responsibility to control. The safe pattern is to gate the call behind the build's debug flag so the line compiles out of release artifacts entirely.
Last updated
Was this helpful?

