首页指南参考教程
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 51).

Expo Brightness iconExpo Brightness

A library that provides access to an API for getting and setting the screen brightness.

Android
iOS

An API to get and set screen brightness.

On Android, there is a global system-wide brightness setting, and each app has its own brightness setting that can optionally override the global setting. It is possible to set either of these values with this API. On iOS, the system brightness setting cannot be changed programmatically; instead, any changes to the screen brightness will persist until the device is locked or powered off.

Installation

Terminal
npx expo install expo-brightness

If you are installing this in an existing React Native app (bare workflow), start by installing expo in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.

Configuration

Are you using this library in a bare React Native app?

Learn how to configure the native projects in the installation instructions in the expo-brightness repository.

Usage

Basic Brightness Usage
import React, { useEffect } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import * as Brightness from 'expo-brightness';

export default function App() {
  useEffect(() => {
    (async () => {
      const { status } = await Brightness.requestPermissionsAsync();
      if (status === 'granted') {
        Brightness.setSystemBrightnessAsync(1);
      }
    })();
  }, []);

  return (
    <View style={styles.container}>
      <Text>Brightness Module Example</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

API

import * as Brightness from 'expo-brightness';

Hooks

Android
iOS

usePermissions(options)

NameType
options
(optional)
PermissionHookOptions<object>

Check or request permissions to modify the system brightness. This uses both requestPermissionAsync and getPermissionsAsync to interact with the permissions.

Returns:

[null | PermissionResponse, RequestPermissionMethod<PermissionResponse>, GetPermissionMethod<PermissionResponse>]

Example

const [permissionResponse, requestPermission] = Brightness.usePermissions();

Methods

Android
iOS

Brightness.getBrightnessAsync()

Gets the current brightness level of the device's main screen.

Returns:

Promise<number>

A Promise that fulfils with a number between 0 and 1, inclusive, representing the current screen brightness.

Android
iOS

Brightness.getPermissionsAsync()

Checks user's permissions for accessing system brightness.

Returns:

Promise<PermissionResponse>

A promise that fulfils with an object of type PermissionResponse.

Android

Brightness.getSystemBrightnessAsync()

Gets the global system screen brightness.

Returns:

Promise<number>

A Promise that is resolved with a number between 0 and 1, inclusive, representing the current system screen brightness.

Android

Brightness.getSystemBrightnessModeAsync()

Gets the system brightness mode (e.g. whether or not the OS will automatically adjust the screen brightness depending on ambient light).

Returns:

Promise<BrightnessMode>

A Promise that fulfils with a BrightnessMode. Requires SYSTEM_BRIGHTNESS permissions.

Android
iOS

Brightness.isAvailableAsync()

Returns whether the Brightness API is enabled on the current device. This does not check the app permissions.

Returns:

Promise<boolean>

Async boolean, indicating whether the Brightness API is available on the current device. Currently this resolves true on iOS and Android only.

Android

Brightness.isUsingSystemBrightnessAsync()

Returns a boolean specifying whether or not the current activity is using the system-wide brightness value.

Returns:

Promise<boolean>

A Promise that fulfils with true when the current activity is using the system-wide brightness value, and false otherwise.

Android
iOS

Brightness.requestPermissionsAsync()

Asks the user to grant permissions for accessing system brightness.

Returns:

Promise<PermissionResponse>

A promise that fulfils with an object of type PermissionResponse.

Android

Brightness.restoreSystemBrightnessAsync()

Resets the brightness setting of the current activity to use the system-wide brightness value rather than overriding it.

Returns:

Promise<void>

A Promise that fulfils when the setting has been successfully changed.

Android
iOS

Brightness.setBrightnessAsync(brightnessValue)

NameTypeDescription
brightnessValuenumber

A number between 0 and 1, inclusive, representing the desired screen brightness.


Sets the current screen brightness. On iOS, this setting will persist until the device is locked, after which the screen brightness will revert to the user's default setting. On Android, this setting only applies to the current activity; it will override the system brightness value whenever your app is in the foreground.

Returns:

Promise<void>

A Promise that fulfils when the brightness has been successfully set.

Experimental
 • 
Android

Brightness.setSystemBrightnessAsync(brightnessValue)

NameTypeDescription
brightnessValuenumber

A number between 0 and 1, inclusive, representing the desired screen brightness.


Sets the global system screen brightness and changes the brightness mode to MANUAL. Requires SYSTEM_BRIGHTNESS permissions.

Returns:

Promise<void>

A Promise that fulfils when the brightness has been successfully set.

Android

Brightness.setSystemBrightnessModeAsync(brightnessMode)

NameTypeDescription
brightnessModeBrightnessMode

One of BrightnessMode.MANUAL or BrightnessMode.AUTOMATIC. The system brightness mode cannot be set to BrightnessMode.UNKNOWN.


Sets the system brightness mode.

Returns:

Promise<void>

Deprecated Use restoreSystemBrightnessAsync method instead.

Android

Brightness.useSystemBrightnessAsync()

Returns:

Promise<void>

Event Subscriptions

iOS

Brightness.addBrightnessListener(listener)

NameTypeDescription
listener(event: BrightnessEvent) => void

A callback that is invoked when brightness (iOS) changes. The callback is provided a single argument that is an object with a brightness key.


Subscribe to brightness (iOS) updates. The event fires whenever the power mode is toggled.

On web and android the event never fires.

Returns:

EventSubscription

A Subscription object on which you can call remove() to unsubscribe from the listener.

Interfaces

Android
iOS

PermissionResponse

An object obtained by permissions get and request functions.

PermissionResponse Properties

NameTypeDescription
canAskAgainboolean

Indicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission.

expiresPermissionExpiration

Determines time when the permission expires.

grantedboolean

A convenience boolean that indicates if the permission is granted.

statusPermissionStatus

Determines the status of the permission.


Types

Android
iOS

BrightnessEvent

NameTypeDescription
brightnessnumber

A number between 0 and 1, inclusive, representing the current screen brightness.

Android
iOS

PermissionExpiration

Literal Type: multiple types

Permission expiration time. Currently, all permissions are granted permanently.

Acceptable values are: 'never' | number

Android
iOS

PermissionHookOptions

Literal Type: multiple types

Acceptable values are: PermissionHookBehavior | Options

Enums

Android
iOS

BrightnessMode

BrightnessMode Values

UNKNOWN

BrightnessMode.UNKNOWN = 0

Means that the current brightness mode cannot be determined.

AUTOMATIC

BrightnessMode.AUTOMATIC = 1

Mode in which the device OS will automatically adjust the screen brightness depending on the ambient light.

MANUAL

BrightnessMode.MANUAL = 2

Mode in which the screen brightness will remain constant and will not be adjusted by the OS.

Android
iOS

PermissionStatus

PermissionStatus Values

DENIED

PermissionStatus.DENIED = "denied"

User has denied the permission.

GRANTED

PermissionStatus.GRANTED = "granted"

User has granted the permission.

UNDETERMINED

PermissionStatus.UNDETERMINED = "undetermined"

User hasn't granted or denied the permission yet.

Error codes

ERR_BRIGHTNESS

An error occurred when getting or setting the app brightness.

ERR_BRIGHTNESS_MODE

An error occurred when getting or setting the system brightness mode. See the nativeError property of the thrown error for more information.

ERR_BRIGHTNESS_PERMISSIONS_DENIED

An attempt to set the system brightness was made without the proper permissions from the user. The user did not grant SYSTEM_BRIGHTNESS permissions.

ERR_BRIGHTNESS_SYSTEM

An error occurred when getting or setting the system brightness.

ERR_INVALID_ARGUMENT

An invalid argument was passed. Only BrightnessMode.MANUAL or BrightnessMode.AUTOMATIC are allowed.

Permissions

Android

You must add the following permissions to your app.json inside the expo.android.permissions array.

Android PermissionDescription

WRITE_SETTINGS

Allows an application to read or write the system settings.

iOS

No permissions required.

Expo 中文网 - 粤ICP备13048890号