当不确定开发设备是否具备某个传感器的情况下,直接进行开发调试可能会导致以下情况:
开发前确认软件目标设备是否具备该传感器,来进行功能规划:

同一套代码可能涉及多种设备,开发中获取运行设备支持的传感器类型列表,根据SensorId判断是否具备某个传感器进行针对性开发:
query() {
// 获取传感器列表
try {
sensor.getSensorList((err: BusinessError, data: Array<sensor.Sensor>) => {
if (err) {
console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`);
return;
}
this.sensorList = data;
this.sensorListStr = [];
for (let i = 0; i < data.length; i++) {
this.sensorListStr.push(data[i].sensorId + ' ' + data[i].sensorName);
}
});
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
}
}
下面以重力传感器为例:
onDegree() {
try {
sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
this.degree = [];
this.degree.push(data.x.toString());
this.degree.push(data.y.toString());
this.degree.push(data.z.toString());
});
} catch (error) {
let e: BusinessError = error as BusinessError;
this.degree.push(e.code.toString());
this.degree.push(e.message);
console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
}
}
disDegree() {
try {
sensor.off(sensor.SensorId.GRAVITY);
this.degree = [];
} catch (error) {
let e: BusinessError = error as BusinessError;
this.degree.push(e.code.toString());
this.degree.push(e.message);
console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
}
}
import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
sensorList: Array<sensor.Sensor> = [];
@State sensorListStr: Array<string> = [];
@State degree: Array<string> = [];
query() {
// 获取传感器列表
try {
sensor.getSensorList((err: BusinessError, data: Array<sensor.Sensor>) => {
if (err) {
console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`);
return;
}
this.sensorList = data;
this.sensorListStr = [];
for (let i = 0; i < data.length; i++) {
this.sensorListStr.push(data[i].sensorId + ' ' + data[i].sensorName);
}
});
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
}
}
onDegree() {
try {
sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
this.degree = [];
this.degree.push(data.x.toString());
this.degree.push(data.y.toString());
this.degree.push(data.z.toString());
});
} catch (error) {
let e: BusinessError = error as BusinessError;
this.degree.push(e.code.toString());
this.degree.push(e.message);
console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
}
}
disDegree() {
try {
sensor.off(sensor.SensorId.GRAVITY);
this.degree = [];
} catch (error) {
let e: BusinessError = error as BusinessError;
this.degree.push(e.code.toString());
this.degree.push(e.message);
console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
}
}
build() {
Column({ space: 16 }) {
Button('查询支持的传感器列表')
.onClick(() => {
this.query();
});
Button('启动重力传感器')
.onClick(() => {
this.onDegree();
});
Button('关闭重力传感器')
.onClick(() => {
this.disDegree();
});
Text('重力传感器数据:');
Text(`${this.degree.join('\n')}`);
Text('传感器列表如下:\n ');
Text(`${this.sensorListStr.join('\n')}`);
}
.height('100%')
.width('100%');
}
}