12

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:

enter image description here

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?

Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78

3 Answers3

7

Use this method to get max value for brightness

public int getMaxBrightness(Context context, int defaultValue){

    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if(powerManager != null) {
        Field[] fields = powerManager.getClass().getDeclaredFields();
        for (Field field: fields) {

            //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/PowerManager.java

            if(field.getName().equals("BRIGHTNESS_ON")) {
                field.setAccessible(true);
                try {
                    return (int) field.get(powerManager);
                } catch (IllegalAccessException e) {
                    return defaultValue;
                }
            }
        }
    }
    return defaultValue;
}

I have tested this on a couple of devices (mostly android 9, 10 devices, including some xiaomi devices, which usually set brightness higher than the usual 255), and looks like this is working.

Google does not promote accessing hidden fields/methods using reflection. Any android update could potentially break this solution.

Another possible solution would be accessing getMaximumScreenBrightnessSetting() method in PowerManager class using reflection. But I haven't tested it and therefore cannot confirm the results.

NB : If you are using this value to set brightness percentage, keep this in mind : From android 9 onwards, brightness is set logarithmically. So setting brightness percentage might not look like the percentage shown in brightness slider (Lower percentages might look higher than set value on brightness slider), but the physical brightness will be set right.

Aswin P Ashok
  • 702
  • 8
  • 27
5

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.

Smak
  • 104
  • 4
  • Is the file "/sys/class/leds/lcd-backlight/max_brightness" in every device, or is it just for Xiaomi? I cannot find it in my device. Do I have to be root to access it? – Ton Nov 22 '19 at 19:29
  • **_some_** devices. It's undocumented, so it is not reliable. It is not Xiaomi specific. – Smak Nov 23 '19 at 07:19
  • At least affects OnePlus and Xiaomi. With different range! Aswin answer seems to do the trick though. – 3c71 Sep 06 '20 at 10:34
  • I'm experiencing similar situation. On Huawei, Xiaomi devices on Android 10, brightness works incorrectly. – Halil Ozel Feb 08 '22 at 13:24
1

Instead of you set 255 as the max value. I prefer to use this way to set brightness to max. Just passing 1F to the screenBrightness.

WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);

It's working fine on different android devices.

Rico Ardiansyah
  • 274
  • 2
  • 4
  • Doesn't work on OnePlus (need to set it to 4F) and doesn't work on POCO F2 Pro either where it needs to be set to 8F. – 3c71 Sep 06 '20 at 10:25