Control Device Brightness from React Native App

Control Screen Brightness

This is an Example to Control Device Brightness from React Native App using react-native-screen-brightness for Android and IOS. You generally do it in your day to day life but what if you can control the brightness from your own application? Yes, you can do that with the help of the react-native-screen-brightness library.

You can see the use of a screen control feature if you have ever used any UPI or payment transaction app, where you need to scan the QR code for the payment. While scanning the QR code the brightness should be high so that QR code will become more clear other to scan. In this case, you need a screen brightness control feature to provide a better user experience.

To Set the Brightness of the Screen

While using react-native-screen-brightness you can change the screen brightness using

ScreenBrightness.setBrightness(0.2);

Note:

  1. In the case of IOS, Min brightness value is 0 and Max brightness value is 1.
  2. In the case of Android, Min brightness value is 0 and Max brightness value is 255.

To Get the Brightness of the Device

To get the Screen brightness at any point you can use

ScreenBrightness.getBrightness().then(brightness => {
    alert('brightness ' + brightness);
});

For this example, We are going to use a Slider component which will change the brightness value and a button to get the brightness value. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use Slider and ScreenBrightness component we need to install react-native-community/slider and react-native-screen-brightness dependencies.
To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install --save react-native-screen-brightness
npm install --save @react-native-community/slider

This command will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods and to install cocoa pods use

cd ios && pod install && cd ..

Hurree!! it is ready for IOS but for Android devices, you need to add some additional permission.

Android Runtime Permission

Open the manifest file of your Android project and add

<uses-permission
  android:name="android.permission.WRITE_SETTINGS"
  tools:ignore="ProtectedPermissions"
/>

Also update you manifest params and include xmlns:tools

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.baseproject"
  xmlns:tools="http://schemas.android.com/tools">

Code

Now Open App.js in any code editor and replace the code with the following code

App.js

// Control Device Brightness from React Native App
// https://aboutreact.com/react-native-screen-brightness/

// import React in our code
import React, {useState, useEffect} from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
} from 'react-native';

//Import Screen brightness control library
import ScreenBrightness from 'react-native-screen-brightness';

//Import Slider to change the brightness
import Slider from '@react-native-community/slider';

const App = () => {
  const [brightness, setBrightness] = useState(0.2);

  useEffect(() => {
    ScreenBrightness.setBrightness(brightness);
  }, []);

  const getBrightness = () => {
    ScreenBrightness.getBrightness().then((brightness) => {
      alert('brightness ' + brightness);
    });
  };

  return (
    <SafeAreaView style={styles.container}>
      <View>
        <Text style={styles.titleText}>
          Control Device Brightness from React Native App
        </Text>
        <Text style={styles.textStyle}>
          Current Brightness: {'\n'} {brightness}
        </Text>
        {/*Slider with max, min, step and initial value*/}
        <Slider
          maximumValue={1}
          minimumValue={0}
          minimumTrackTintColor="#307ecc"
          maximumTrackTintColor="#000000"
          step={0.1}
          value={brightness}
          onValueChange={(brightness) => {
            setBrightness(brightness);
            ScreenBrightness.setBrightness(brightness);
          }}
        />
        <TouchableOpacity
          activeOpacity={0.7}
          style={styles.buttonStyle}
          onPress={getBrightness}>
          <Text style={styles.buttonTextStyle}>
            Get Current Brightness Value
          </Text>
        </TouchableOpacity>
      </View>
    </SafeAreaView>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
  },
  titleText: {
    fontSize: 22,
    textAlign: 'center',
    fontWeight: 'bold',
  },
  textStyle: {
    fontSize: 16,
    color: 'black',
    padding: 10,
  },
  buttonStyle: {
    justifyContent: 'center',
    marginTop: 15,
    padding: 10,
    backgroundColor: '#8ad24e',
    marginRight: 2,
    marginLeft: 2,
  },
  buttonTextStyle: {
    color: '#fff',
    textAlign: 'center',
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots

IOS

                        

Android

                        

Known issue with Android devices

If you run the app in your android device you will see this yellow box.

which shows “Unable to set system brightness” because you have no permission to change the system settings. To solve this issue Open Settings option of your device > Apps & Notifications > App > Search for the YourApplicationName and open the app info by clicking on it.

      

In the last of the Info, you will see Write system settings click on it and switch it on. Now you are good to go. Open your App again and try, It should work now. 🙂

This is how you can Control Device Brightness from React Native App using react-native-screen-brightness for Android and IOS. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.