App and system screen brightness
One.Brightness reads and writes the screen brightness: an app-level
override on both platforms, plus the system brightness and mode on
Android. Reach for it for video players, barcode scanners, and night
modes that need the screen brighter or dimmer than the user’s setting.
On iOS app brightness reads and writes UIScreen on the main thread. On
Android it sets the activity window attribute, while the system family
reads and writes Settings.System. On web reads resolve 1, effects do
nothing, permissions read denied, and listeners never fire.
import { useEffect } from 'react'import { One } from 'one'
function useFullBrightness() { useEffect(() => { async function boost() { const initial = await One.Brightness.get() await One.Brightness.set(1) return initial } const initial = boost() return () => { void initial.then((value) => One.Brightness.set(value)) } }, [])}Saving the starting value and restoring it on unmount leaves the user’s brightness exactly as it was. Out-of-range values throw synchronously.
get and set move a number from 0 to 1. On iOS the setting persists
until the device locks. On Android it applies to the current activity
while the app is in the foreground. addListener receives
{ brightness } and fires on iOS only; the iOS simulator never posts
brightness notifications for programmatic sets, so listeners stay quiet
there while get still reads back every set.
The getSystem family is Android only. getSystem reads the global
brightness, setSystem writes it and switches the mode to manual,
getSystemMode resolves unknown, automatic, or manual, setSystemMode
switches between manual and automatic, isUsingSystem reports whether
the activity follows the system value, and restoreSystem drops the app
override. On iOS these resolve the app value or the matching empty value
(unknown mode, always using system) and change nothing.
System writes need the Android write-settings grant. Permissions resolve
a granted, denied, or undetermined status with granted and can-ask-again
flags; on iOS they resolve granted with no prompt. requestPermissions
opens the system settings screen, so read the grant back when the app
resumes. The manifest must also declare WRITE_SETTINGS, which prebuild
does not stamp yet: grant it from the settings screen, or on dev builds
with adb. Without the declaration and the grant, setSystem and
setSystemMode reject with E_BRIGHTNESS_PERMISSIONS.
| Methods: app | Type | Description |
|---|---|---|
get() => Promise<number>The app brightness, 0 to 1. | () => Promise<number> | The app brightness, 0 to 1. |
set(value: number) => Promise<void>Set the app brightness. Out of range throws. | (value: number) => Promise<void> | Set the app brightness. Out of range throws. |
isUsingSystem() => Promise<boolean>Whether the app follows the system value. | () => Promise<boolean> | Whether the app follows the system value. |
restoreSystem() => Promise<void>Drop the app override. | () => Promise<void> | Drop the app override. |
isAvailable() => booleanWhether brightness reads work here. | () => boolean | Whether brightness reads work here. |
addListener(listener: (event: BrightnessEvent) => void) => BrightnessSubscriptionHear brightness changes. iOS only. | (listener: (event: BrightnessEvent) => void) => BrightnessSubscription | Hear brightness changes. iOS only. |
| Methods: system | Type | Description |
|---|---|---|
getSystem() => Promise<number>The global brightness. Android only. | () => Promise<number> | The global brightness. Android only. |
setSystem(value: number) => Promise<void>Write the global brightness, switching to manual. | (value: number) => Promise<void> | Write the global brightness, switching to manual. |
getSystemMode() => Promise<BrightnessMode>Unknown, automatic, or manual. | () => Promise<BrightnessMode> | Unknown, automatic, or manual. |
setSystemMode(mode: BrightnessMode) => Promise<void>Switch manual and automatic. | (mode: BrightnessMode) => Promise<void> | Switch manual and automatic. |
getPermissions() => Promise<BrightnessPermissionResponse>The write-settings grant, without prompting. | () => Promise<BrightnessPermissionResponse> | The write-settings grant, without prompting. |
requestPermissions() => Promise<BrightnessPermissionResponse>Open system settings for the grant. | () => Promise<BrightnessPermissionResponse> | Open system settings for the grant. |
| Permission | Type | Description |
|---|---|---|
statusBrightnessPermissionStatusGranted, denied, or undetermined. | BrightnessPermissionStatus | Granted, denied, or undetermined. |
grantedbooleanWhether system writes may run. | boolean | Whether system writes may run. |
canAskAgainbooleanWhether settings may still grant it. | boolean | Whether settings may still grant it. |
| Event | Type | Description |
|---|---|---|
brightnessnumberThe new app brightness, 0 to 1. | number | The new app brightness, 0 to 1. |
| Subscription | Type | Description |
|---|---|---|
remove() => voidStop this listener. | () => void | Stop this listener. |
| iOS | Android | Web | |
|---|---|---|---|
| App level | Screen API | Window value | Always 1 |
| System level | App value | Global value | Always 1 |
| Mode | Unknown | Auto or manual | Unknown |
| Events | Yes | Never | Never |
| Grant | Always yes | Settings page | Denied |
import { useEffect } from 'react'import { One } from 'one'
function useScannerBrightness() { useEffect(() => { async function boost() { const initial = await One.Brightness.get() await One.Brightness.set(1) return initial } const initial = boost() return () => { void initial.then((value) => One.Brightness.set(value)) } }, [])}Full brightness helps the camera freeze barcodes in dim aisles. The cleanup restores the exact prior value even when the screen unmounts mid-boost.
import { useEffect } from 'react'import { One } from 'one'
function useNightDim(night: boolean) { useEffect(() => { if (!night) return async function dim() { const initial = await One.Brightness.get() await One.Brightness.set(0.2) return initial } const initial = dim() return () => { void initial.then((value) => One.Brightness.set(value)) } }, [night])}Tying the effect to the night flag restores brightness the moment night mode turns off or the screen unmounts, whichever comes first.
Edit this page on GitHub.