1# Sensor服务使用实例 2 3 4 使用实例以sensorTypeId为0的传感器为例,其他类型的传感器使用方式类似。 5 6```c 7#include "sensor_agent.h" 8#include "sensor_agent_type.h" 9#include "stdio.h" 10 11void SensorDataCallbackImpl(SensorEvent *event) 12{ 13 if(event == NULL){ 14 return; 15 } 16 float *sensorData=(float *)event->data; 17 for(int32_t i = 0; i < (int32_t)(event->dataLen / sizeof(uint8_t *)); i++){ 18 printf("SensorDataCallbackImpl data: %f", *(sensorData + i)); 19 } 20} 21 22/* 测试用例函数 */ 23static int32_t TestSensorService(void) 24{ 25 SensorUser sensorUser; 26 sensorUser.callback = SensorDataCallbackImpl; 27 SensorInfo *sensorInfo = (SensorInfo *)NULL; 28 int32_t count = 0; 29 // 获取设备的sensor列表 30 int32_t ret = GetAllSensors(&sensorInfo, &count); 31 if (ret != 0) { 32 printf("GetAllSensors failed! ret: %d", ret); 33 return ret; 34 } 35 // 使能传感器 36 ret = ActivateSensor(0, &sensorUser); 37 if (ret != 0) { 38 printf("ActivateSensorfailed! ret: %d", ret); 39 return ret; 40 } 41 // 订阅传感器数据 42 ret = SubscribeSensor(0, &sensorUser); 43 if (ret != 0) { 44 printf("SubscribeSensor! ret: %d", ret); 45 return ret; 46 } 47 sleep(10); 48 // 取消传感器数据订阅 49 ret = UnsubscribeSensor(0, &sensorUser); 50 if (ret != 0) { 51 printf("UnsubscribeSensor! ret: %d", ret); 52 return ret; 53 } 54 // 去使能传感器 55 ret = DeactivateSensor(0, &sensorUser); 56 if (ret != 0) { 57 printf("DeactivateSensor! ret: %d", ret); 58 return ret; 59 } 60} 61``` 62