I'm using the following code to set the screen brightness, which works fine on most phones:
protected fun setBrightness(value: Float) {
//Set the system brightness using the brightness variable value
Settings.System.putInt(contentResolver, Settings.System
.SCREEN_BRIGHTNESS, (value * 255).toInt())
//Get the current window attributes
val layoutpars = window.getAttributes()
//Set the brightness of this window
layoutpars.screenBrightness = value
//Apply attribute changes to this window
window.setAttributes(layoutpars)
}
When I pass a value of 1, meaning maximum value, this gets converted to 255 which is said to be the greatest value to set the screen brightness. However, on a Xiaomi Mi8 setting the value to 255 won't set the brightness to the full range, as seen in this screenshot:
After printing some debug values and experimenting, it looks like on the Xiaomi Mi8 the maximum brightness value is actually 1024 (or at least, multiplying 1 by that value sets the full brightness bar).
It seems that different android devices might have different brightness scales. Is there some API to get the maximum value in brightness so I don't need to hardcode different constants?
It is specific for some Xiaomi devices. For example Xiaomi Redmi Note 7 has 0-4000 range.
Official documentation defines SCREEN_BRIGHTNESS range as 0-255. So, I think there are no API to get the maximum value in brightness.
On some (not all) devices there is a file "/sys/class/leds/lcd-backlight/max_brightness" that can contain max value.