Skip to content

Instantly share code, notes, and snippets.

@starry-shivam
Last active October 12, 2024 08:36
Show Gist options
  • Save starry-shivam/901267c26eb030eb3faf1ccd4d2bdd32 to your computer and use it in GitHub Desktop.
Save starry-shivam/901267c26eb030eb3faf1ccd4d2bdd32 to your computer and use it in GitHub Desktop.
Check if the device is running either on MIUI or HyperOS
object MiuiCheck {
/**
* Check if the device is running on MIUI.
*
* By default, HyperOS is excluded from the check.
* If you want to include HyperOS in the check, set excludeHyperOS to false.
*
* @param excludeHyperOS Whether to exclude HyperOS
* @return True if the device is running on MIUI, false otherwise
*/
fun isMiui(excludeHyperOS: Boolean = true): Boolean {
// Check if the device is manufactured by Xiaomi, Redmi, or POCO.
val brand = Build.BRAND.lowercase()
if (!setOf("xiaomi", "redmi", "poco").contains(brand)) return false
// This property is present in both MIUI and HyperOS.
val isMiui = !getProperty("ro.miui.ui.version.name").isNullOrBlank()
// This property is exclusive to HyperOS only and isn't present in MIUI.
val isHyperOS = !getProperty("ro.mi.os.version.name").isNullOrBlank()
return isMiui && (!excludeHyperOS || !isHyperOS)
}
// Private function to get the property value from build.prop.
private fun getProperty(property: String): String? {
return try {
Runtime.getRuntime().exec("getprop $property").inputStream.use { input ->
BufferedReader(InputStreamReader(input), 1024).readLine()
}
} catch (e: IOException) {
e.printStackTrace()
null
}
}
}
@Ronjar
Copy link

Ronjar commented Oct 12, 2024

In my case because HyperOS doesn't grant BLUETOOTH_SCAN, ADVERTISE and CONNECT, when permission NEARBY_DEVICES is granted through their dialog. But when granted through App Permission Settings (The ones made by Google), they are granted as well. Frustrating, took me some time to find out. Also for other permissions "Open new windows while running in background" which is granted differently in stock Android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment