Control Device Brightness from React Native App

Control Device Brightness from React Native App

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 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 Device

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

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

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
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

Run the following command to install

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

Linking of Dependencies

After the installation of the dependencies, you need to link the dependencies using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Hurre!! it is ready for IOS but for Android devices, you need to add some additional permission for that so open the manifest file of your Android project and add

 

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

App.js

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
react-native run-ios (macOS only).

If you are facing any issue while running the project in Android device then please scroll down for the solution.

IOS

         
         

Android

            
         

The problem for Android devices:

If you are facing the issue in android devices then please follow the below steps:

1. You need to open the project in Android Studio. If you have no idea how to do it then open Android Studio and click the open project option and select the YourProjectName>android directory and click open. Probably you will see the following error.

2. Now Open Project Level build.gradle and make sure it is updated like this.

3. Update the build.gradle file of Module:react-native-screen-brightness to the same compileSdkVersion, buildToolsVersion, minSdkVersion, targetSdkVersion as mentioned in your Project Level build.gradle. Here is mine

4. Now You can see react-native-screen-brightness directory in the left navigation. Click to expand it and under that expand java and then expand package mentioned. you will find two java files ScreenBrightnessModule and ScreenBrightnessPackage files their.

5. Please replace the content of both java files with the attached java files. Both files have some changes which is very hard to explain one by one as we have to replace some method so instead of making the changes override the code with this attached Java files.

a) ScreenBrightnessModule.java (Please remove .zip in the file name after download)

b) ScreenBrightnessPackage.java (Please remove .zip in the file name after download)

6. If you have linked your project properly then you have seen an additional line new ScreenBrightnessPackage in MainApplication. Here you can see the red underline. Now to resolve this just pass a random number in it.

7. Now Run your project again I hope everything will work well.

8. As you all know Android above API 23 needs runtime permission but in the case of android.permission.WRITE_SETTINGS, I am still struggling to set up runtime permission as it is returning null while asking runtime permission. If I got any solution related to that I’ll surely update the post.

9. Meanwhile, 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.

      

10. 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.
11. 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 doubt 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. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Comment

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

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

About React will use the information you provide on this form to be in touch with you and to provide updates and marketing.