文档管理中心
FAQ系统开发硬件传感器(Sensor Service)如何确认硬件是否具备某个传感器

如何确认硬件是否具备某个传感器

问题现象

当不确定开发设备是否具备某个传感器的情况下,直接进行开发调试可能会导致以下情况:

  • 执行sensor.on(),sensor.off()会崩溃报错:The parameter invalid.;
  • 某些传感器不具备但可以执行,导致获取传感器结果异常,结果可能为传感器型号(固定值),这种情况下会导致排查困难。

背景知识

传感器类型:列举了目前支持的传感器类型。

SensorId:传感器类型对应的id值。

解决方案

  • 开发前查询

    开发前确认软件目标设备是否具备该传感器,来进行功能规划:

    1. 连接设备,在IDE终端使用hdc shell命令;
    2. 输入指令:hidumper -s 3601 -a -l可查看设备具备的所有传感器,根据设备具备的传感器进行开发。
  • 使用前查询

    同一套代码可能涉及多种设备,开发中获取运行设备支持的传感器类型列表,根据SensorId判断是否具备某个传感器进行针对性开发:

    收起
    自动换行
    深色代码主题
    复制
    1. query() {
    2. // 获取传感器列表
    3. try {
    4. sensor.getSensorList((err: BusinessError, data: Array<sensor.Sensor>) => {
    5. if (err) {
    6. console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`);
    7. return;
    8. }
    9. this.sensorList = data;
    10. this.sensorListStr = [];
    11. for (let i = 0; i < data.length; i++) {
    12. this.sensorListStr.push(data[i].sensorId + ' ' + data[i].sensorName);
    13. }
    14. });
    15. } catch (error) {
    16. let e: BusinessError = error as BusinessError;
    17. console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
    18. }
    19. }
  • 使用try catch保证不崩溃

    下面以重力传感器为例:

    收起
    自动换行
    深色代码主题
    复制
    1. onDegree() {
    2. try {
    3. sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
    4. this.degree = [];
    5. this.degree.push(data.x.toString());
    6. this.degree.push(data.y.toString());
    7. this.degree.push(data.z.toString());
    8. });
    9. } catch (error) {
    10. let e: BusinessError = error as BusinessError;
    11. this.degree.push(e.code.toString());
    12. this.degree.push(e.message);
    13. console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
    14. }
    15. }
    16. disDegree() {
    17. try {
    18. sensor.off(sensor.SensorId.GRAVITY);
    19. this.degree = [];
    20. } catch (error) {
    21. let e: BusinessError = error as BusinessError;
    22. this.degree.push(e.code.toString());
    23. this.degree.push(e.message);
    24. console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
    25. }
    26. }
  • 完整示例代码如下:
    收起
    自动换行
    深色代码主题
    复制
    1. import { sensor } from '@kit.SensorServiceKit';
    2. import { BusinessError } from '@kit.BasicServicesKit';
    3. @Entry
    4. @Component
    5. struct Index {
    6. sensorList: Array<sensor.Sensor> = [];
    7. @State sensorListStr: Array<string> = [];
    8. @State degree: Array<string> = [];
    9. query() {
    10. // 获取传感器列表
    11. try {
    12. sensor.getSensorList((err: BusinessError, data: Array<sensor.Sensor>) => {
    13. if (err) {
    14. console.error(`Failed to get sensorList. Code: ${err.code}, message: ${err.message}`);
    15. return;
    16. }
    17. this.sensorList = data;
    18. this.sensorListStr = [];
    19. for (let i = 0; i < data.length; i++) {
    20. this.sensorListStr.push(data[i].sensorId + ' ' + data[i].sensorName);
    21. }
    22. });
    23. } catch (error) {
    24. let e: BusinessError = error as BusinessError;
    25. console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
    26. }
    27. }
    28. onDegree() {
    29. try {
    30. sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
    31. this.degree = [];
    32. this.degree.push(data.x.toString());
    33. this.degree.push(data.y.toString());
    34. this.degree.push(data.z.toString());
    35. });
    36. } catch (error) {
    37. let e: BusinessError = error as BusinessError;
    38. this.degree.push(e.code.toString());
    39. this.degree.push(e.message);
    40. console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
    41. }
    42. }
    43. disDegree() {
    44. try {
    45. sensor.off(sensor.SensorId.GRAVITY);
    46. this.degree = [];
    47. } catch (error) {
    48. let e: BusinessError = error as BusinessError;
    49. this.degree.push(e.code.toString());
    50. this.degree.push(e.message);
    51. console.error(`Failed to get sensorList. Code: ${e.code}, message: ${e.message}`);
    52. }
    53. }
    54. build() {
    55. Column({ space: 16 }) {
    56. Button('查询支持的传感器列表')
    57. .onClick(() => {
    58. this.query();
    59. });
    60. Button('启动重力传感器')
    61. .onClick(() => {
    62. this.onDegree();
    63. });
    64. Button('关闭重力传感器')
    65. .onClick(() => {
    66. this.disDegree();
    67. });
    68. Text('重力传感器数据:');
    69. Text(`${this.degree.join('\n')}`);
    70. Text('传感器列表如下:\n ');
    71. Text(`${this.sensorListStr.join('\n')}`);
    72. }
    73. .height('100%')
    74. .width('100%');
    75. }
    76. }

总结

涉及传感器接口时:建议加try catch避免代码崩溃。

在 FAQ 中进行搜索
请输入您想要搜索的关键词