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