HarmonyOS NEXT Development Case: Light Intensity Meter
The following example demonstrates how to create a light intensity meter application using HarmonyOS NEXT's sensor capabilities. This application monitors ambient light levels through the device's light sensor and provides corresponding environmental descriptions and activity recommendations. Implementation Code import { sensor } from '@kit.SensorServiceKit'; // Import sensor service kit import { BusinessError } from '@kit.BasicServicesKit'; // Import business error classes // Class representing light intensity range information class LightIntensityItem { luxStart: number; // Starting lux value of the range luxEnd: number; // Ending lux value of the range type: string; // Intensity classification type description: string; // Environmental description recommendation: string; // Activity recommendation // Constructor to initialize object properties constructor(luxStart: number, luxEnd: number, type: string, description: string, recommendation: string) { this.luxStart = luxStart; this.luxEnd = luxEnd; this.type = type; this.description = description; this.recommendation = recommendation; } } // Light intensity meter component using decorators @Entry @Component struct LightIntensityMeter { @State currentType: string = ""; // Current light intensity type @State currentIntensity: number = 0; // Current light intensity value @State lightIntensityList: LightIntensityItem[] = [ // Light intensity range configuration new LightIntensityItem(0, 1, 'Extreme Dark', 'Outdoor night with minimal light', 'Avoid all activities, rest recommended'), new LightIntensityItem(1, 10, 'Very Dark', 'Indoor night with weak lighting', 'Suitable for sleep, avoid electronics'), new LightIntensityItem(10, 50, 'Dark', 'Dawn/dusk natural light', 'Casual relaxation, avoid prolonged reading'), new LightIntensityItem(50, 100, 'Dim', 'Cloudy day indoor light', 'Daily activities, short reading sessions'), new LightIntensityItem(100, 300, 'Moderate', 'Cloudy day indoor light', 'Work/study, moderate reading'), new LightIntensityItem(300, 500, 'Bright', 'Sunny day indoor light', 'Extended reading/work'), new LightIntensityItem(500, 1000, 'Very Bright', 'Outdoor cloudy light', 'Outdoor activities, sun protection'), new LightIntensityItem(1000, 100000, 'Overload', 'Direct noon sunlight', 'Avoid sun exposure, use sunglasses'), ]; // Component lifecycle method - before appearance aboutToAppear(): void { sensor.getSensorList((error: BusinessError) => { // Get sensor list if (error) { console.error('Sensor list fetch failed', error); return; } this.startLightIntensityUpdates(); // Start light sensor monitoring }); } // Start light sensor updates private startLightIntensityUpdates(): void { sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data) => { // Ambient light sensor listener console.info(`Current intensity: ${data.intensity}`); this.currentIntensity = data.intensity; for (const item of this.lightIntensityList) { if (data.intensity >= item.luxStart && data.intensity 1000 ? 1000 : this.currentIntensity, min: 0, max: 1000 }) { Column() { Text(`${Math.floor(this.currentIntensity)}`) .fontSize(25) .fontWeight(FontWeight.Medium) .fontColor("#323232") .height('30%') .textAlign(TextAlign.Center) .margin({ top: '22.2%' }) .textOverflow({ overflow: TextOverflow.Ellipsis }) .maxLines(1); Text(`${this.currentType}`) .fontSize(16) .fontColor("#848484") .fontWeight(FontWeight.Regular) .width('47.4%') .height('15%') .textAlign(TextAlign.Center) .backgroundColor("#e4e4e4") .borderRadius(5); }.width('100%'); } .startAngle(225) .endAngle(135) .height(250) .strokeWidth(18) .description(null) .trackShadow({ radius: 7, offsetX: 7, offsetY: 7 }) .padding({ top: 30 }); }.width('100%').justifyContent(FlexAlign.Center); Column() { // Information list ForEach(this.lightIntensityList, (item: LightIntensityItem, index: number) => { Row() { Text(`${item.luxStart}~${item.luxEnd}Lux `) .fontSize('25lpx') .textAlign(TextAlign.Start) .fontColor("#3d3d3d") .width('220lpx') Text(`${item.description}\n${item.recommendation}`) .fontSize('23lpx') .textAlign(TextAlign.Start) .fontColor("#3d3d3d") .layoutWeight(1) }.width('660lpx') .padding({ bottom: 10, top: 10 }) .borderWidth({ bottom: 1 }) .borderColor("#737977"); }); }.width('100%');

The following example demonstrates how to create a light intensity meter application using HarmonyOS NEXT's sensor capabilities. This application monitors ambient light levels through the device's light sensor and provides corresponding environmental descriptions and activity recommendations.
Implementation Code
import { sensor } from '@kit.SensorServiceKit'; // Import sensor service kit
import { BusinessError } from '@kit.BasicServicesKit'; // Import business error classes
// Class representing light intensity range information
class LightIntensityItem {
luxStart: number; // Starting lux value of the range
luxEnd: number; // Ending lux value of the range
type: string; // Intensity classification type
description: string; // Environmental description
recommendation: string; // Activity recommendation
// Constructor to initialize object properties
constructor(luxStart: number, luxEnd: number, type: string, description: string, recommendation: string) {
this.luxStart = luxStart;
this.luxEnd = luxEnd;
this.type = type;
this.description = description;
this.recommendation = recommendation;
}
}
// Light intensity meter component using decorators
@Entry
@Component
struct LightIntensityMeter {
@State currentType: string = ""; // Current light intensity type
@State currentIntensity: number = 0; // Current light intensity value
@State lightIntensityList: LightIntensityItem[] = [ // Light intensity range configuration
new LightIntensityItem(0, 1, 'Extreme Dark', 'Outdoor night with minimal light', 'Avoid all activities, rest recommended'),
new LightIntensityItem(1, 10, 'Very Dark', 'Indoor night with weak lighting', 'Suitable for sleep, avoid electronics'),
new LightIntensityItem(10, 50, 'Dark', 'Dawn/dusk natural light', 'Casual relaxation, avoid prolonged reading'),
new LightIntensityItem(50, 100, 'Dim', 'Cloudy day indoor light', 'Daily activities, short reading sessions'),
new LightIntensityItem(100, 300, 'Moderate', 'Cloudy day indoor light', 'Work/study, moderate reading'),
new LightIntensityItem(300, 500, 'Bright', 'Sunny day indoor light', 'Extended reading/work'),
new LightIntensityItem(500, 1000, 'Very Bright', 'Outdoor cloudy light', 'Outdoor activities, sun protection'),
new LightIntensityItem(1000, 100000, 'Overload', 'Direct noon sunlight',
'Avoid sun exposure, use sunglasses'),
];
// Component lifecycle method - before appearance
aboutToAppear(): void {
sensor.getSensorList((error: BusinessError) => { // Get sensor list
if (error) {
console.error('Sensor list fetch failed', error);
return;
}
this.startLightIntensityUpdates(); // Start light sensor monitoring
});
}
// Start light sensor updates
private startLightIntensityUpdates(): void {
sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data) => { // Ambient light sensor listener
console.info(`Current intensity: ${data.intensity}`);
this.currentIntensity = data.intensity;
for (const item of this.lightIntensityList) {
if (data.intensity >= item.luxStart && data.intensity <= item.luxEnd) {
this.currentType = item.type;
break;
}
}
}, { interval: 10000000 }); // 1-second interval (nanoseconds)
}
// UI construction
build() {
Column() { // Main vertical layout
Text("Light Intensity Meter")
.width('100%')
.height(44)
.backgroundColor("#fe9900")
.textAlign(TextAlign.Center)
.fontColor(Color.White);
Row() { // Gauge display area
Gauge({
value: this.currentIntensity > 1000 ? 1000 : this.currentIntensity,
min: 0,
max: 1000
}) {
Column() {
Text(`${Math.floor(this.currentIntensity)}`)
.fontSize(25)
.fontWeight(FontWeight.Medium)
.fontColor("#323232")
.height('30%')
.textAlign(TextAlign.Center)
.margin({ top: '22.2%' })
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(1);
Text(`${this.currentType}`)
.fontSize(16)
.fontColor("#848484")
.fontWeight(FontWeight.Regular)
.width('47.4%')
.height('15%')
.textAlign(TextAlign.Center)
.backgroundColor("#e4e4e4")
.borderRadius(5);
}.width('100%');
}
.startAngle(225)
.endAngle(135)
.height(250)
.strokeWidth(18)
.description(null)
.trackShadow({ radius: 7, offsetX: 7, offsetY: 7 })
.padding({ top: 30 });
}.width('100%').justifyContent(FlexAlign.Center);
Column() { // Information list
ForEach(this.lightIntensityList, (item: LightIntensityItem, index: number) => {
Row() {
Text(`${item.luxStart}~${item.luxEnd}Lux `)
.fontSize('25lpx')
.textAlign(TextAlign.Start)
.fontColor("#3d3d3d")
.width('220lpx')
Text(`${item.description}\n${item.recommendation}`)
.fontSize('23lpx')
.textAlign(TextAlign.Start)
.fontColor("#3d3d3d")
.layoutWeight(1)
}.width('660lpx')
.padding({ bottom: 10, top: 10 })
.borderWidth({ bottom: 1 })
.borderColor("#737977");
});
}.width('100%');
}
.height('100%')
.width('100%');
}
}
Key Features
-
Sensor Integration:
- Uses
@kit.SensorServiceKit
to access ambient light sensor data - Implements 1-second update interval (10,000,000 nanoseconds)
- Uses
-
Data Modeling:
-
LightIntensityItem
class defines light range characteristics - Contains type, description, and activity recommendations
-
-
Reactive UI:
-
@State
decorators enable automatic UI updates - Gauge component dynamically displays current intensity
- Real-time type classification updates
-
-
Information Display:
- Circular gauge with custom angle range (225°-135°)
- Scrollable list of light intensity parameters
- Adaptive text formatting and layout
Usage Scenarios
This solution is ideal for:
- Environmental monitoring applications
- Smart home automation systems
- Health and wellness apps with light sensitivity features
- IoT devices requiring ambient light adaptation
The implementation demonstrates HarmonyOS NEXT's capabilities in sensor integration and reactive UI development, providing a foundation for building context-aware applications.