npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-system-settings-ts

v1.7.9

Published

Fork of react-native-system-settings that provides some system setting APIs. Volume, brightness, wifi, location, bluetooth, airplane...

Downloads

44

Readme

react-native-system-setting-ts

System setting APIs for iOS and Android

This library is a fork of react-native-system-setting that addresses the new NativeEventEmitter() warning.

Support

  • Volume (with listener)
  • Brightness
  • Wi-Fi switch
  • Location
  • Bluetooth
  • Airplane

Note

Example only works on a real device

Change Log

Change Log

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

How it looks

Android Screenshot

Install

Using npm:

npm install react-native-system-setting --save

or using yarn:

yarn add react-native-system-setting

Link

If using react-native < 0.60, run react-native link to link this library.

iOS

todo: confirm this

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

If linking with react-native link did not work, you can do it manually.

Android

If linking with react-native link did not work, you can do it manually by following these steps:

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

At the top of the file, import react-native-system-setting:

import com.ninty.system.setting.SystemSettingPackage;

Add the SystemSettingPackage class to your list of exported packages.

class MainApplication extends Application implements ReactApplication {
    // ...
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>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

Wi-Fi

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 mode

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

App System Settings

// open app setting page
SystemSetting.openAppSystemSettings()

API

API

Run example

cd example
yarn
# for iOS
react-native run-ios
# for Android
react-native run-android

iOS

To be app store friendly, APIs for iOS are disabled since V1.7.0, You can enable it in a few steps by following instructions in iOS.md

Android permission

API

Default permissions were removed in 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 may be useful when you develop a system management app. 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 Android settings. 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. There is also a SystemSetting.checkWriteSettingsPermissions() API to check if the app has WRITE_SETTINGS permission.

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

Contributions are welcome

Feel free to open issues or pull requests

License

MIT