DEV Community

HarmonyOS
HarmonyOS

Posted on

๐ŸŒž Build a Smart Brightness Controller with HarmonyOS and Ambient Light Sensor (SmartLux)

Read the original article๏ผš๐ŸŒž Build a Smart Brightness Controller with HarmonyOS and Ambient Light Sensor (SmartLux)

SmartLux with ArkTS (This picture was generated by AI)

๐Ÿง  Introduction

Have you ever been blinded by your screen in a dark room, or struggled to see anything under sunlight? Manually adjusting screen brightness can be annoying and inconsistent. That's where SmartLux comes in โ€” a HarmonyOS wearable app that automatically adapts screen behavior to ambient light using ArkTS and sensor services.

In this article, we'll build a HarmonyOS app that reads real-time lux levels from the ambient light sensor and dynamically adjusts the UI theme and optionally screen brightness.

By the end, you'll have:

  • Hands-on experience with SensorServiceKit
  • A functional HarmonyOS UI that reacts to environmental light
  • (Bonus) Background task setup for persistent monitoring

Letโ€™s glow ๐ŸŸก

โš™๏ธ What We'll Use

What we'll use and why

๐Ÿ“ฆ Project Setup

  1. Create a HarmonyOS Stage project with ArkTS
  2. Set deviceType to wearable or phone
  3. Open module.json5 and add the following permissions:
"requestPermissions": [
  { "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" },
  { "name": "ohos.permission.RUNNING_LOCK" }
]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Setting Up Ambient Light Sensor

We'll use sensor.on to continuously monitor the ambient light levels.

sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data: sensor.LightResponse) => {
  const lux = data.intensity
  console.info('๐Ÿ’ก Lux value: ' + lux)
}, { interval: 100000000 })
Enter fullscreen mode Exit fullscreen mode

๐ŸŽจ Dynamic UI Theming Based on Lux

private getThemeColor(): Color {
  if (this.lux < 50) return Color.Black
  else if (this.lux < 500) return Color.Gray
  else return Color.White
}

private getTextMessage(): string {
  if (this.lux < 50) return '๐ŸŒ‘ Dim environment...'
  else if (this.lux < 500) return '๐ŸŒค๏ธ Light level is moderate.'
  else return 'โ˜€๏ธ Bright as daylight!'
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”† Brightness Calculation (Optional)

let brightness = Math.round((lux / 1000) * 100)
brightness = Math.max(10, Math.min(100, brightness))
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Limitation

@system.brightness.setValue() only works for system apps starting from **API 7**. In third-party apps, use it only for internal logic or visual feedback.
const info: wantAgent.WantAgentInfo = {
  wants: [{ bundleName: this.context.abilityInfo.bundleName,
  abilityName: this.context.abilityInfo.name }],
  operationType: wantAgent.OperationType.START_ABILITY,
  requestCode: 0,
  wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}

const agent = await wantAgent.getWantAgent(info)
await backgroundTaskManager.startBackgroundRunning(this.context,
backgroundTaskManager.BackgroundMode.DATA_TRANSFER, agent)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฌ Demo

๐Ÿ‘‹ Conclusion

SmartLux shows how easy it is to create responsive, environment-aware apps with HarmonyOS and ArkTS. With just a few APIs and sensors, you can make your UI adaptive, energy-efficient, and user-friendly.

References

Brightness

Written by Muhammet Cagri Yilmaz

Top comments (0)