Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-system-setting-update

AwesomeAndrewMK288MIT1.7.6TypeScript support: included

provide some system setting APIs. Volume, brightness, wifi, location, bluetooth, airplane...

react-native, system, setting, volume, wifi, brightness, location, bluetooth, airplane

readme

react-native-system-setting

It provides some system setting APIs for you. Support iOS and Android both.

Support

  • Volume ( with listener)
  • Brightness
  • Wifi switch
  • Location
  • Bluetooth
  • Airplane

Note

Example only work in the real device

IMPORTANT! This version of package is a fork of the original one: react-native-system-setting, which includes a fix for Android brightness

Change Log

Change Log

breaking change for permission since V1.5.0, see Android Permission

Look like

I really want to show the .gif, while it has no difference with .jpg for some system limit.

I strongly recommend you to run the example in real device to see how it works.

   

Install

Run npm i -S react-native-system-setting

Note: if your project was created by Create React Native App, you should Eject before link it.

iOS

Run react-native link to link this library.

Or add pod 'RCTSystemSetting', :path => '../node_modules/react-native-system-setting' in Podfile for Cocoapods.

If link does not work, you can do it manually.

Android

Run react-native link to link this library.

That's all.

If link does not work, you can do it manually. Just follow this way:

android/settings.gradle

include ':react-native-system-setting'
project(':react-native-system-setting').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-system-setting/android')

android/app/build.gradle

dependencies {
   ...
   compile project(':react-native-system-setting')
}

android/app/src/main/java/..packageName../MainApplication.java

On top, where imports are:

import com.ninty.system.setting.SystemSettingPackage;

Add the SystemSettingPackage class to your list of exported packages.

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.asList(
            new MainReactPackage(),
            new SystemSettingPackage()
    );
}

Usage

Common import

import SystemSetting from 'react-native-system-setting'

volume

//get the current volume
SystemSetting.getVolume().then((volume)=>{
    console.log('Current volume is ' + volume);
});

// change the volume
SystemSetting.setVolume(0.5);

// listen the volume changing if you need
const volumeListener = SystemSetting.addVolumeListener((data) => {
    const volume = data.value;
    console.log(volume);
});

//remove listener when you need it no more
SystemSetting.removeVolumeListener(volumeListener)       

setVolume can do more, more detail

brightness

//get the current brightness
SystemSetting.getBrightness().then((brightness)=>{
    console.log('Current brightness is ' + brightness);
});

//change the brightness & check permission
SystemSetting.setBrightnessForce(0.5).then((success)=>{
    !success && Alert.alert('Permission Deny', 'You have no permission changing settings',[
       {'text': 'Ok', style: 'cancel'},
       {'text': 'Open Setting', onPress:()=>SystemSetting.grantWriteSettingPermission()}
    ])
});

// save the value of brightness and screen mode.
SystemSetting.saveBrightness();
// restore the brightness and screen mode. you can get the old brightness value.
SystemSetting.restoreBrightness().then((oldVal)=>{
    //if you need
})

// change app's brightness without any permission.
SystemSetting.setAppBrightness(0.5);
SystemSetting.getAppBrightness().then((brightness)=>{
    console.log('Current app brightness is ' + brightness);
})

setBrightness() & saveBrightness() need permission for Android

Wifi

SystemSetting.isWifiEnabled().then((enable)=>{
    const state = enable ? 'On' : 'Off';
    console.log('Current wifi is ' + state);
})

SystemSetting.switchWifi(()=>{
    console.log('switch wifi successfully');
})

isWifiEnabled() need permission for Android

switchWifi() is disabled by default for iOS since V1.7.0, enable it

Location

SystemSetting.isLocationEnabled().then((enable)=>{
    const state = enable ? 'On' : 'Off';
    console.log('Current location is ' + state);
})

SystemSetting.switchLocation(()=>{
    console.log('switch location successfully');
})

switchLocation() is disabled by default for iOS since V1.7.0, enable it

Bluetooth

SystemSetting.isBluetoothEnabled().then((enable)=>{
    const state = enable ? 'On' : 'Off';
    console.log('Current bluetooth is ' + state);
})

SystemSetting.switchBluetooth(()=>{
    console.log('switch bluetooth successfully');
})

isBluetoothEnabled() need permission for Android

All bluetooth-function are disabled by default for iOS since V1.7.0, enable it

Airplane

SystemSetting.isAirplaneEnabled().then((enable)=>{
    const state = enable ? 'On' : 'Off';
    console.log('Current airplane is ' + state);
})

SystemSetting.switchAirplane(()=>{
    console.log('switch airplane successfully');
})

isAirplaneEnabled() will always return true for iOS if your device has no SIM card, see detail

switchAirplane() is disabled by default for iOS since V1.7.0, enable it

Other

// open app setting page
SystemSetting.openAppSystemSettings()

API

API

Run example

$ cd example/SystemSettingExample
$ npm install
// if android
$ react-native run-android
// else
$ react-native run-ios

iOS

To be more friendly to app store, I disable some APIs for iOS since V1.7.0, You can enable it in a few steps.

Android permission

API

Default permissions are removed since V1.5.0, see this PR. You need to declare the corresponding permissions in your app's AndroidManifest.xml, see example AndroidManifest.xml

android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YourPackageName"
    android:versionCode="1"
    android:versionName="1.0">

    <!-- setBrightness() & setScreenMode() & saveBrightness() -->
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <!-- isWifiEnabled() -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <!-- isBluetoothEnabled() -->
    <uses-permission android:name="android.permission.BLUETOOTH"/>

    <!-- * switchWifiSilence() -->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <!-- * switchBluetoothSilence() -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    ...

</manifest>

There are some different APIs that end with silence. They can do the job programmatically without direct user consent. These APIs maybe useful when you develop a system management app. Or, you should call switchWifi() & switchBluetooth() to get a better user experience

Do Not Disturb

setVolume() may cause a crash: Not allowed to change Do Not Disturb state. See detail.

Runtime permission for Android 6+

Change brightness and screen mode need android.permission.WRITE_SETTINGS which user can disable it in phone Setting. When you call setScreenMode(), setBrightness() or setBrightnessForce() , it will return false if the app has no permission, and you can call SystemSetting.grantWriteSettingPermission() to guide user to app setting page. see example

If you just want to change app's brightness, you can call setAppBrightness(val), and it doesn't require any permission. see API

In the end

Feel free to open issue or pull request

License

MIT

changelog

V1.7.6

2020-10-11

fix TypeScript definitions

V1.7.5

2020-09-02

adds TypeScript definitions, see detail

V1.7.4

2020-01-30

typo: grantWriteSettingPremission() -> grantWriteSettingPermission, see detail

V1.7.3

2019-10-03

fix bug: setBrightness() will cause a crash in iOS 13, see detail

V1.7.2

2019-02-14

new API: addLocationModeListener(), only works in Android

V1.7.1

2018-12-30

new API: openAppSystemSettings will open app setting page

V1.7.0

2018-09-30

add preprocessor PRIVATE_API which can enable switch*() function, see detail

setAppStore() is deprecated.

breaking change: remove BLUETOOTH by default.

V1.6.0

2018-09-19

bluetooth issues for App Store, see detail

V1.5.3

2018-08-20

fix bug: setVolume() may cause a crash, see detail

V1.5.2

2018-08-05

setVolume() may crash in >= Android M, see detail

RN 0.56.+ compatible, Use project-wide properties and new dependency

V1.5.1

2018-08-02

show System Volume UI by default for iOS, see detail

V1.5.0

2018-07-10

breaking change: remove default Android permission, see detail

V1.4.6

2018-06-20

fix bug: switchAirplane() will open Bluetooth Setting Page for iOS.

V1.4.5

2018-06-07

fix bug: Exception handling for Volume Event, see detail

V1.4.4

2018-05-27

new API: getLocationMode(), see detail

fix bug: all listener may get null sometime

V1.4.3

2018-05-21

for iOS - override +(BOOL)requiresMainQueueSetup to remove warning

V1.4.2

2018-05-07

new API: addLocationListener(), only works in Android

new API: addAirplaneListener(), only works in Android

V1.4.1

2018-04-23

new API: addWifiListener(), only works in Android

new API: addBluetoothListener(), Android & iOS

V1.4.0

2018-04-21

new API: setAppStore(), it will avoid some troubles when you submit app to App Store, see detail

V1.3.0

2018-04-15

new API: switchBluetoothSilence(), see detail

breaking change: remove android.permission.CHANGE_WIFI_STATE from AndroidManifest.xml. If you want to call switchWifiSilence(), you should declare the permission in your Androidmanifest.xml, see detail

V1.2.5

2018-04-07

fix bug - setVolume(val, config) will cause a crash when type is null, see detail

fix bug - grantWriteSettingPermission() navigates to the wrong page, see detail

V1.2.4

2018-03-14

fix bug - isLocationEnabled return false when locate by network only, see detail

V1.2.3

2018-02-26

fix bug - setVolume will show the MPVolumeView in the upper left corner, see detail

V1.2.2

2018-02-25

use MPVolumeView to get/set volume for iOS.

change API setVolume(val, type) to setVolume(val, config), see detail

V1.2.1

2018-01-21

Supported Cocoapods, see detail

V1.2.0

2018-01-07

new API: isAirplaneEnabled() & switchAirplane(complete), see detail

V1.1.1

2018-01-03

Supported >= iOS 8.0.

new API: setAppBrightness(val:float) & getAppBrightness(), see detail

V1.1.0

2017-12-14

Check permission when write setting, see detail

V1.0.8

2017-11-27

Now you can change volume by type (Android only), see detail

V1.0.7

2017-08-19

support RN 0.47, see detail

V1.0.6

2017-07-23

support switching bluetooth state

V1.0.5

2017-07-15

support switching location state

API change : switchWifi() and switchWifiSilence(), see detail

V1.0.4

2017-06-16

support switching wifi state

V1.0.3

2017-06-05

Save & Restore for brightness

V1.0.2

2017-06-03

Add listener for volume

V1.0.1

2017-05-31

Add screen mode (Android only)

V1.0.0

2017-05-30

Support volume and brightness