Tung Le
Tung Le

Reputation: 41

After changing screen brightness manually, the brightness cannot be changed from app code

I'm using React Native with @adrianso/react-native-device-brightness library for this Imagine I'm in screen A, having device brightness about 0.5 When redirecting to screen B, Current brightness is saved, then I set brightness to 1 (maximum value). If I back to screen A, the brightness will be set back to saved value (0.5) This would work normally, until:

One of following scenarios would happen:

I've tried:

By any means, is this issue related to the thread blocking of iOS?

Upvotes: 1

Views: 136

Answers (1)

winniezz
winniezz

Reputation: 1

const [previousBrightness, setPreviousBrightness] = useState<number | null>(null);

const getBrightness = () => {
    Brightness.getBrightnessAsync().then((value) => {
      setPreviousBrightness(value);
    });
};

 const restoreBrightness = useCallback(() => {
    if (previousBrightness !== null) {
      Brightness.setBrightnessAsync(previousBrightness)
        .then(() => {
          console.info('Brightness restored');
        })
        .catch(() => {
          console.error('Error restoring brightness');
        });
    }
  }, [previousBrightness]);




useEffect(() => {
(async () => {
  getBrightness();

  const { status } = await Brightness.requestPermissionsAsync();
  if (status === 'granted') {
    Brightness.setBrightnessAsync(1)
      .then(() => {
        console.info('Brightness set to 1');
      })
      .catch(() => {
        console.error('Error setting brightness');
      });
  }
})();
return restoreBrightness;
 },[restoreBrightness]);
  1. store brightness at load
  2. restore orignal brightness at unload

Upvotes: 0

Related Questions