Setting screen brightness usually fails

asked 2021-04-30T14:04:27.107+00:00
Ben-Richard Ebbesvik 1 Reputation point

My app contains a page with a QR code, which is going to be read by a QR code reader. The QR code reader is placed outdoor and the brightness of the phone screen needs to be set to the most brightest for the QR code reader to read the code easily in a sunny environment.

Setting the brightness is easy from the Xamarin.Forms project. I can easily make a service in Xamarion.IOS which sets UIScreen.MainScreen.Brightness = 1f; and then inject the service into my Xamarin.Forms project. This procedure is described here

But when the app goes to the background, the user turns down the brightness and then brings the app back into the foreground, the app should make the screen brighter again. I have tried to solve this in the Xamarin.IOS project, as Android and IOS have different lifecycles.

In the AppDelegate.cs in Xamarin.IOS:

public override void WillEnterForeground(UIApplication uiApplication)  
            UIScreen.MainScreen.Brightness = 1f;  

WillEnterForeground triggers every time the app is brought to the foreground. However, UIScreen.MainScreen.Brightness only sometimes get set. No exceptions are thrown. The app either turns up the brightness or it don't. Usually don't. I have tried in Xamarin.Forms 4.4, 4.5, 4.6 and 5.0 on a physical device (Iphone 6s).

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,803 questions
{count} votes