1# Sensor 2 3 4## 概述 5 6提供API来使用常见的传感器特性。例如,调用API来获取传感器和订阅或取消订阅传感器数据。 7 8提供标准的开放api,定义常用传感器属性。 9 10**起始版本:** 11 11 12 13## 汇总 14 15 16### 文件 17 18| 名称 | 描述 | 19| -------- | -------- | 20| [oh_sensor.h](oh_sensor_8h.md) | 声明操作传感器的API,包括获取传感器信息和订阅取消订阅传感器数据。 | 21| [oh_sensor_type.h](oh_sensor_type_8h.md) | 定义常用传感器属性。 | 22 23 24### 类型定义 25 26| 名称 | 描述 | 27| -------- | -------- | 28| [Sensor_Type](#sensor_type) | 枚举传感器类型。 | 29| [Sensor_Result](#sensor_result) | 定义传感器错误码。 | 30| [Sensor_Accuracy](#sensor_accuracy) | 枚举传感器报告的数据的精度级别。 | 31| [Sensor_Info](#sensor_info) | 定义传感器信息。 | 32| [Sensor_Event](#sensor_event) | 定义传感器数据信息。 | 33| [Sensor_SubscriptionId](#sensor_subscriptionid) | 定义传感器订阅ID,唯一标识传感器。 | 34| [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) | 定义传感器订阅属性。 | 35| void ([*Sensor_EventCallback](#sensor_eventcallback)) ([Sensor_Event](#sensor_event) \*event) | 定义用于报告传感器数据的回调函数。 | 36| [Sensor_Subscriber](#sensor_subscriber) | 定义传感器订阅者信息。 | 37 38 39### 枚举 40 41| 名称 | 描述 | 42| -------- | -------- | 43| [Sensor_Type](#sensor_type) {<br/>SENSOR_TYPE_ACCELEROMETER = 1, <br/>SENSOR_TYPE_GYROSCOPE = 2,<br/> SENSOR_TYPE_AMBIENT_LIGHT = 5, <br/>SENSOR_TYPE_MAGNETIC_FIELD = 6,<br/>SENSOR_TYPE_BAROMETER = 8,<br/> SENSOR_TYPE_HALL = 10, <br/>SENSOR_TYPE_PROXIMITY = 12,<br/> SENSOR_TYPE_ORIENTATION = 256,<br/>SENSOR_TYPE_GRAVITY = 257, <br/>SENSOR_TYPE_ROTATION_VECTOR = 259, ,<br/>SENSOR_TYPE_PEDOMETER_DETECTION = 265,<br/> SENSOR_TYPE_PEDOMETER = 266,<br/>SENSOR_TYPE_HEART_RATE = 278<br/>} | 枚举传感器类型。 | 44| [Sensor_Result](#sensor_result) { <br/>SENSOR_SUCCESS = 0,<br/> SENSOR_PERMISSION_DENIED = 201, <br/>SENSOR_PARAMETER_ERROR = 401,<br/> SENSOR_SERVICE_EXCEPTION = 14500101<br/> } | 定义传感器错误码。 | 45| [Sensor_Accuracy](#sensor_accuracy) { <br/>SENSOR_ACCURACY_UNRELIABLE = 0, <br/>SENSOR_ACCURACY_LOW = 1,<br/> SENSOR_ACCURACY_MEDIUM = 2, <br/>SENSOR_ACCURACY_HIGH = 3 <br/>} | 枚举传感器报告的数据的精度级别。 | 46 47 48### 函数 49 50| 名称 | 描述 | 51| -------- | -------- | 52| [OH_Sensor_GetInfos](#oh_sensor_getinfos)([Sensor_Info](#sensor_info) \*\*infos, uint32_t *count) | 获取设备上所有传感器的信息。 | 53| [OH_Sensor_Subscribe](#oh_sensor_subscribe)(const [Sensor_SubscriptionId](#sensor_subscriptionid) \*id, const [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) \*attribute, const [Sensor_Subscriber](#sensor_subscriber) \*subscriber) | 订阅传感器数据。系统将以指定的频率向用户报告传感器数据。<br/>订阅加速度传感器,需要申请ohos.permission.ACCELEROMETER权限;<br/>订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限;<br/>订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限;<br/>订阅与健康相关的传感器时,比如心率传感器需要申请ohos.permission.READ_HEALTH_DATA权限,否则订阅失败;<br/>订阅其余传感器不需要申请权限。 | 54| [OH_Sensor_Unsubscribe](#oh_sensor_unsubscribe)(const [Sensor_SubscriptionId](#sensor_subscriptionid) \*id, const [Sensor_Subscriber](#sensor_subscriber) \*subscriber) | 取消订阅传感器数据。<br/>取消订阅加速度计传感器,需要申请ohos.permission.ACCELEROMETER权限; <br/>取消订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; <br/>取消订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限;<br/>取消订阅与健康相关的传感器时,需要申请ohos.permission.READ_HEALTH_DATA权限,否则取消订阅失败。 <br/>取消订阅其余传感器不需要申请权限。 | 55| [OH_Sensor_CreateInfos](#oh_sensor_createinfos)(uint32_t count) | 用给定的数字创建一个实例数组,请参考 [Sensor_Info](#sensor_info)。 | 56| [OH_Sensor_DestroyInfos](#oh_sensor_destroyinfos)([Sensor_Info](#sensor_info) \*\*sensors, uint32_t count) | 销毁实例数组并回收内存,请参考 [Sensor_Info](#sensor_info)。 | 57| [OH_SensorInfo_GetName](#oh_sensorinfo_getname)([Sensor_Info](#sensor_info) \*sensor, char \*sensorName, uint32_t \*length) | 获取传感器名称。 | 58| [OH_SensorInfo_GetVendorName](#oh_sensorinfo_getvendorname)([Sensor_Info](#sensor_info)* sensor, char \*vendorName, uint32_t \*length) | 获取传感器的厂商名称。 | 59| [OH_SensorInfo_GetType](#oh_sensorinfo_gettype)([Sensor_Info](#sensor_info)* sensor, [Sensor_Type](#sensor_type) \*sensorType) | 获取传感器类型。 | 60| [OH_SensorInfo_GetResolution](#oh_sensorinfo_getresolution)([Sensor_Info](#sensor_info)* sensor, float \*resolution) | 获取传感器分辨率。 | 61| [OH_SensorInfo_GetMinSamplingInterval](#oh_sensorinfo_getminsamplinginterval)([Sensor_Info](#sensor_info)* sensor, int64_t \*minSamplingInterval) | 获取传感器的最小数据上报间隔。 | 62| [OH_SensorInfo_GetMaxSamplingInterval](#oh_sensorinfo_getmaxsamplinginterval)([Sensor_Info](#sensor_info)* sensor, int64_t \*maxSamplingInterval) | 获取传感器的最大数据上报间隔时间。 | 63| [OH_SensorEvent_GetType](#oh_sensorevent_gettype)([Sensor_Event](#sensor_event)* sensorEvent, [Sensor_Type](#sensor_type) \*sensorType) | 获取传感器类型。 | 64| [OH_SensorEvent_GetTimestamp](#oh_sensorevent_gettimestamp)([Sensor_Event](#sensor_event)* sensorEvent, int64_t \*timestamp) | 获取传感器数据的时间戳。 | 65| [OH_SensorEvent_GetAccuracy](#oh_sensorevent_getaccuracy)([Sensor_Event](#sensor_event)* sensorEvent, Sensor_Accuracy *accuracy) | 获取传感器数据的精度。 | 66| [OH_SensorEvent_GetData](#oh_sensorevent_getdata)([Sensor_Event](#sensor_event)* sensorEvent, float \*\*data, uint32_t \*length) | 获取传感器数据。<br/>数据的长度和内容依赖于监听的传感器类型,传感器上报的数据格式如下:SENSOR_TYPE_ACCELEROMETER:data[0]、data[1]、data[2]分别表示设备x、y、z轴的加速度分量,单位m/s²;<br/>SENSOR_TYPE_GYROSCOPE:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角速度,单位弧度/s;<br/>SENSOR_TYPE_AMBIENT_LIGHT:data[0]表示环境光强度,in lux;<br/> SENSOR_TYPE_MAGNETIC_FIELD:data[0]、data[1]、data[2]分别表示设备x、y、z轴的地磁分量,单位微特斯拉; <br/>SENSOR_TYPE_BAROMETER:data[0]表示气压值,单位hPa;<br/>SENSOR_TYPE_HALL:data[0]表示皮套吸合状态,0表示打开,大于0表示吸附;<br/>SENSOR_TYPE_PROXIMITY:data[0]表示接近状态,0表示接近,大于0表示远离;<br/>SENSOR_TYPE_ORIENTATION:data[0]、data[1]、data[2]分别表示设备绕z、x、y轴的角度,单位度;<br/>SENSOR_TYPE_GRAVITY:data[0]、data[1]、data[2]分别表示设备x、y、z轴的重力加速度分量,单位m/s²;<br/>SENSOR_TYPE_ROTATION_VECTOR:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角度,单位度,data[3]表示旋转向量元素;<br/>SENSOR_TYPE_PEDOMETER_DETECTION:data[0]表示计步检测状态,1表示检测到了步数变化;<br/>SENSOR_TYPE_PEDOMETER:data[0]表示步数;<br/>SENSOR_TYPE_HEART_RATE:data[0]表示心率数值。 | 67| [OH_Sensor_CreateSubscriptionId](#oh_sensor_createsubscriptionid)(void) | 创建一个 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例。 | 68| [OH_Sensor_DestroySubscriptionId](#oh_sensor_destroysubscriptionid)([Sensor_SubscriptionId](#sensor_subscriptionid) \*id) | 销毁 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例并回收内存。 | 69| [OH_SensorSubscriptionId_GetType](#oh_sensorsubscriptionid_gettype)([Sensor_SubscriptionId](#sensor_subscriptionid) \*id, [Sensor_Type](#sensor_type) \*sensorType) | 获取传感器类型。 | 70| [OH_SensorSubscriptionId_SetType](#oh_sensorsubscriptionid_settype)([Sensor_SubscriptionId](#sensor_subscriptionid)* id, const [Sensor_Type](#sensor_type) sensorType) | 设置传感器类型。 | 71| [OH_Sensor_CreateSubscriptionAttribute](#oh_sensor_createsubscriptionattribute)(void) | 创建 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例。 | 72| [OH_Sensor_DestroySubscriptionAttribute](#oh_sensor_destroysubscriptionattribute)([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) *attribute) | 销毁 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例并回收内存。 | 73| [OH_SensorSubscriptionAttribute_SetSamplingInterval](#oh_sensorsubscriptionattribute_setsamplinginterval)([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, const int64_t samplingInterval) | 设置传感器数据报告间隔。 | 74| [OH_SensorSubscriptionAttribute_GetSamplingInterval](#oh_sensorsubscriptionattribute_getsamplinginterval) ([Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)* attribute, int64_t \*samplingInterval) | 获取传感器数据报告间隔。 | 75| [OH_Sensor_CreateSubscriber](#oh_sensor_createsubscriber)(void) | 创建一个 [Sensor_Subscriber](#sensor_subscriber) 实例。 | 76| [OH_Sensor_DestroySubscriber](#oh_sensor_destroysubscriber)([Sensor_Subscriber](#sensor_subscriber) \*subscriber) | 销毁 [Sensor_Subscriber](#sensor_subscriber) 实例并回收内存。 | 77| [OH_SensorSubscriber_SetCallback](#oh_sensorsubscriber_setcallback)([Sensor_Subscriber](#sensor_subscriber)* subscriber, const [Sensor_EventCallback](#sensor_eventcallback) callback) | 设置一个回调函数来报告传感器数据。 | 78| [OH_SensorSubscriber_GetCallback](#oh_sensorsubscriber_getcallback)([Sensor_Subscriber](#sensor_subscriber)* subscriber, [Sensor_EventCallback](#sensor_eventcallback) \*callback) | 获取用于报告传感器数据的回调函数。 | 79 80 81## 类型定义说明 82 83 84### Sensor_Accuracy 85 86``` 87typedef enum Sensor_Accuracy Sensor_Accuracy 88``` 89**描述:** 90 91枚举传感器报告的数据的精度级别。 92 93**起始版本:** 11 94 95 96### Sensor_Event 97 98``` 99typedef struct Sensor_Event Sensor_Event 100``` 101**描述:** 102 103定义传感器数据信息。 104 105**起始版本:** 11 106 107 108### Sensor_EventCallback 109 110``` 111typedef void(*Sensor_EventCallback)(Sensor_Event *event) 112``` 113**描述:** 114 115定义用于报告传感器数据的回调函数。 116 117**起始版本:** 11 118 119 120### Sensor_Info 121 122``` 123typedef struct Sensor_Info Sensor_Info 124``` 125**描述:** 126 127定义传感器信息。 128 129**起始版本:** 11 130 131 132### Sensor_Result 133 134``` 135typedef enum Sensor_Result Sensor_Result 136``` 137**描述:** 138 139定义传感器错误码。 140 141**起始版本:** 11 142 143 144### Sensor_Subscriber 145 146``` 147typedef struct Sensor_Subscriber Sensor_Subscriber 148``` 149**描述:** 150 151定义传感器订阅者信息。 152 153**起始版本:** 11 154 155 156### Sensor_SubscriptionAttribute 157 158``` 159typedef struct Sensor_SubscriptionAttribute Sensor_SubscriptionAttribute 160``` 161**描述:** 162 163定义传感器订阅属性。 164 165**起始版本:** 11 166 167 168### Sensor_SubscriptionId 169 170``` 171typedef struct Sensor_SubscriptionId Sensor_SubscriptionId 172``` 173**描述:** 174 175定义传感器订阅ID,唯一标识传感器。 176 177**起始版本:** 11 178 179 180### Sensor_Type 181 182``` 183typedef enum Sensor_Type Sensor_Type 184``` 185**描述:** 186 187枚举传感器类型。 188 189**起始版本:** 11 190 191 192## 枚举类型说明 193 194 195### Sensor_Accuracy 196 197``` 198enum Sensor_Accuracy 199``` 200**描述:** 201 202枚举传感器报告的数据的精度级别。 203 204**起始版本:** 11 205 206| 枚举值 | 描述 | 207| -------- | -------- | 208| SENSOR_ACCURACY_UNRELIABLE | 传感器数据不可靠。有可能传感器不与设备接触而进行测量。 | 209| SENSOR_ACCURACY_LOW | 传感器数据精度较低。数据在使用前必须根据环境进行校准。 | 210| SENSOR_ACCURACY_MEDIUM | 传感器数据处于中等精度水平。建议用户在使用前根据实际环境进行数据校准。 | 211| SENSOR_ACCURACY_HIGH | 传感器数据具有很高的精度。数据可以直接使用。 | 212 213 214### Sensor_Result 215 216``` 217enum Sensor_Result 218``` 219**描述:** 220 221定义传感器错误码。 222 223**起始版本:** 11 224 225| 枚举值 | 描述 | 226| -------- | -------- | 227| SENSOR_SUCCESS | 操作成功。 | 228| SENSOR_PERMISSION_DENIED | 权限验证失败。 | 229| SENSOR_PARAMETER_ERROR | 参数检查失败。例如,没有传入强制参数,或者传入的参数类型不正确。 | 230| SENSOR_SERVICE_EXCEPTION | 传感器服务异常。 | 231 232 233### Sensor_Type 234 235``` 236enum Sensor_Type 237``` 238**描述:** 239 240枚举传感器类型。 241 242**起始版本:** 11 243 244| 枚举值 | 描述 | 245| -------- | -------- | 246| SENSOR_TYPE_ACCELEROMETER | 加速度传感器。 | 247| SENSOR_TYPE_GYROSCOPE | 陀螺仪传感器。 | 248| SENSOR_TYPE_AMBIENT_LIGHT | 环境光传感器。 | 249| SENSOR_TYPE_MAGNETIC_FIELD | 地磁传感器。 | 250| SENSOR_TYPE_BAROMETER | 气压传感器。 | 251| SENSOR_TYPE_HALL | 霍尔传感器。 | 252| SENSOR_TYPE_PROXIMITY | 接近光传感器。 | 253| SENSOR_TYPE_ORIENTATION | 方向传感器。 | 254| SENSOR_TYPE_GRAVITY | 重力传感器。 | 255| SENSOR_TYPE_ROTATION_VECTOR | 旋转矢量传感器。 | 256| SENSOR_TYPE_PEDOMETER_DETECTION | 计步器检测传感器。 | 257| SENSOR_TYPE_PEDOMETER | 计步器传感器。 | 258| SENSOR_TYPE_HEART_RATE | 心率传感器。 | 259 260 261## 函数说明 262 263 264### OH_Sensor_CreateInfos() 265 266``` 267Sensor_Info **OH_Sensor_CreateInfos(uint32_t count) 268``` 269**描述:** 270 271用给定的数字创建一个实例数组,请参考 [Sensor_Info](#sensor_info)。 272 273**起始版本:** 11 274 275**参数:** 276 277| 名称 | 描述 | 278| -------- | -------- | 279| count | 要创建的实例的数量,请参考 [Sensor_Info](#sensor_info)。 | 280 281**返回:** 282 283如果操作成功,返回指向 [Sensor_Info](#sensor_info) 实例数组的双指针;否则返回**NULL**。 284 285 286### OH_Sensor_CreateSubscriber() 287 288``` 289Sensor_Subscriber *OH_Sensor_CreateSubscriber(void) 290``` 291**描述:** 292 293创建一个 [Sensor_Subscriber ](#sensor_subscriber)实例。 294 295**起始版本:** 11 296 297**返回:** 298 299如果操作成功,返回指向 [Sensor_Subscriber](#sensor_subscriber) 实例的指针;否则返回**NULL**。 300 301 302### OH_Sensor_CreateSubscriptionAttribute() 303 304``` 305Sensor_SubscriptionAttribute *OH_Sensor_CreateSubscriptionAttribute(void) 306``` 307**描述:** 308 309创建 [Sensor_SubscriptionAttribute ](#sensor_subscriptionattribute)实例。 310 311**起始版本:** 11 312 313**返回:** 314 315如果操作成功,返回指向 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例的指针;否则返回**NULL**。 316 317 318### OH_Sensor_CreateSubscriptionId() 319 320``` 321Sensor_SubscriptionId *OH_Sensor_CreateSubscriptionId(void) 322``` 323**描述:** 324 325创建一个 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例。 326 327**起始版本:** 11 328 329**返回:** 330 331如果操作成功,返回指向 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例的指针;否则返回**NULL**。 332 333 334### OH_Sensor_DestroyInfos() 335 336``` 337int32_t OH_Sensor_DestroyInfos(Sensor_Info **sensors, uint32_t count) 338``` 339**描述:** 340 341销毁实例数组并回收内存,请参考 [Sensor_Info](#sensor_info)。 342 343**起始版本:** 11 344 345**参数:** 346 347| 名称 | 描述 | 348| -------- | -------- | 349| sensors | 指向 [Sensor_Info](#sensor_info) 实例数组的双指针。 | 350| count | 要销毁的 [Sensor_Info](#sensor_info) 实例的数量。 | 351 352**返回:** 353 354如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 355 356 357### OH_Sensor_DestroySubscriber() 358 359``` 360int32_t OH_Sensor_DestroySubscriber(Sensor_Subscriber *subscriber) 361``` 362**描述:** 363 364销毁 [Sensor_Subscriber ](#sensor_subscriber)实例并回收内存。 365 366**起始版本:** 11 367 368**参数:** 369 370| 名称 | 描述 | 371| -------- | -------- | 372| subscriber | 指向 [Sensor_Subscriber](#sensor_subscriber) 实例的指针。 | 373 374**返回:** 375 376如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 377 378 379### OH_Sensor_DestroySubscriptionAttribute() 380 381``` 382int32_t OH_Sensor_DestroySubscriptionAttribute(Sensor_SubscriptionAttribute *attribute) 383``` 384**描述:** 385 386销毁 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例并回收内存。 387 388**起始版本:** 11 389 390**参数:** 391 392| 名称 | 描述 | 393| -------- | -------- | 394| attribute | 指向 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute) 实例的指针。 | 395 396**返回:** 397 398如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 399 400 401### OH_Sensor_DestroySubscriptionId() 402 403``` 404int32_t OH_Sensor_DestroySubscriptionId(Sensor_SubscriptionId *id) 405``` 406**描述:** 407 408销毁 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例并回收内存。 409 410**起始版本:** 11 411 412**参数:** 413 414| 名称 | 描述 | 415| -------- | -------- | 416| id | 指向 [Sensor_SubscriptionId](#sensor_subscriptionid) 实例的指针。 | 417 418**返回:** 419 420如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 421 422 423### OH_Sensor_GetInfos() 424 425``` 426Sensor_Result OH_Sensor_GetInfos(Sensor_Info **infos, uint32_t *count) 427``` 428**描述:** 429 430获取设备上所有传感器的信息。 431 432**起始版本:** 11 433 434**参数:** 435 436| 名称 | 描述 | 437| -------- | -------- | 438| infos | 双指针指向设备上所有传感器的信息。请参考 [Sensor_Info](#sensor_info)。 | 439| count | 指向设备上传感器数量的指针。 | 440 441**返回:** 442 443如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 444 445 446### OH_Sensor_Subscribe() 447 448``` 449Sensor_Result OH_Sensor_Subscribe(const Sensor_SubscriptionId *id, const Sensor_SubscriptionAttribute *attribute, const Sensor_Subscriber *subscriber) 450``` 451**描述:** 452 453订阅传感器数据。系统将以指定的频率向用户报告传感器数据。 订阅加速度传感器,需要申请ohos.permission.ACCELEROMETER权限; 订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; 订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限; 订阅与健康相关的传感器时,比如心率传感器,需要申请ohos.permission.READ_HEALTH_DATA权限,否则订阅失败。 订阅其余传感器不需要申请权限。 454 455**起始版本:** 11 456 457**参数:** 458 459| 名称 | 描述 | 460| -------- | -------- | 461| id | 指向传感器订阅ID的指针。请参考 [Sensor_SubscriptionId](#sensor_subscriptionid)。 | 462| attribute | 指向订阅属性的指针,该属性用于指定数据报告频率。请参考 [Sensor_SubscriptionAttribute](#sensor_subscriptionattribute)。 | 463| subscriber | 指向订阅者信息的指针,该信息用于指定的回调函数报告传感器数据。请参考 [Sensor_Subscriber](#sensor_subscriber)。 | 464 465**返回:** 466 467如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 468 469**Permission:** 470 471ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or ohos.permission.ACTIVITY_MOTION or ohos.permission.READ_HEALTH_DATA 472 473 474### OH_Sensor_Unsubscribe() 475 476``` 477Sensor_Result OH_Sensor_Unsubscribe(const Sensor_SubscriptionId *id, const Sensor_Subscriber *subscriber) 478``` 479**描述:** 480 481取消订阅传感器数据。 取消订阅加速度计传感器,需要申请ohos.permission.ACCELEROMETER权限; 取消订阅陀螺仪传感器,需要申请ohos.permission.GYROSCOPE权限; 取消订阅计步器相关传感器时,需要申请ohos.permission.ACTIVITY_MOTION权限; 取消订阅与健康相关的传感器时,需要申请ohos.permission.READ_HEALTH_DATA权限,否则取消订阅失败。 取消订阅其余传感器不需要申请权限。 482 483**起始版本:** 11 484 485**参数:** 486 487| 名称 | 描述 | 488| -------- | -------- | 489| id | 指向传感器订阅ID的指针。请参考 [Sensor_SubscriptionId](#sensor_subscriptionid)。 | 490| subscriber | 指向订阅者信息的指针,该信息用于指定的回调函数报告传感器数据。请参考 [Sensor_Subscriber](#sensor_subscriber)。 | 491 492**返回:** 493 494如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 495 496**Permission:** 497 498ohos.permission.ACCELEROMETER or ohos.permission.GYROSCOPE or ohos.permission.ACTIVITY_MOTION or ohos.permission.READ_HEALTH_DATA 499 500 501### OH_SensorEvent_GetAccuracy() 502 503``` 504int32_t OH_SensorEvent_GetAccuracy(Sensor_Event* sensorEvent, Sensor_Accuracy *accuracy) 505``` 506**描述:** 507 508获取传感器数据的精度。 509 510**起始版本:** 11 511 512**参数:** 513 514| 名称 | 描述 | 515| -------- | -------- | 516| sensorEvent | 指向传感器数据信息的指针。 | 517| accuracy | 指向精度的指针。 | 518 519**返回:** 520 521如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 522 523 524### OH_SensorEvent_GetData() 525 526``` 527int32_t OH_SensorEvent_GetData(Sensor_Event* sensorEvent, float **data, uint32_t *length) 528``` 529**描述:** 530 531获取传感器数据。数据的长度和内容依赖于监听的传感器类型,传感器上报的数据格式如下: 532 533SENSOR_TYPE_ACCELEROMETER:data[0]、data[1]、data[2]分别表示设备x、y、z轴的加速度分量,单位m/s²; 534 535SENSOR_TYPE_GYROSCOPE:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角速度,单位弧度/s; 536 537SENSOR_TYPE_AMBIENT_LIGHT:data[0]表示环境光强度,in lux; 538 539SENSOR_TYPE_MAGNETIC_FIELD:data[0]、data[1]、data[2]分别表示设备x、y、z轴的地磁分量,单位微特斯拉; 540 541SENSOR_TYPE_BAROMETER:data[0]表示气压值,单位hPa; 542 543SENSOR_TYPE_HALL:data[0]表示皮套吸合状态,0表示打开,大于0表示吸附; 544 545SENSOR_TYPE_PROXIMITY:data[0]表示接近状态,0表示接近,大于0表示远离; 546 547SENSOR_TYPE_ORIENTATION:data[0]、data[1]、data[2]分别表示设备绕z、x、y轴的角度,单位度; 548 549SENSOR_TYPE_GRAVITY:data[0]、data[1]、data[2]分别表示设备x、y、z轴的重力加速度分量,单位m/s²; 550 551SENSOR_TYPE_ROTATION_VECTOR:data[0]、data[1]、data[2]分别表示设备x、y、z轴的旋转角度,单位度,data[3]表示旋转向量元素; 552 553SENSOR_TYPE_PEDOMETER_DETECTION:data[0]表示计步检测状态,1表示检测到了步数变化; 554 555SENSOR_TYPE_PEDOMETER:data[0]表示步数; 556 557SENSOR_TYPE_HEART_RATE:data[0]表示心率数值。 558 559**起始版本:** 11 560 561**参数:** 562 563| 名称 | 描述 | 564| -------- | -------- | 565| sensorEvent | 传感器数据信息。 | 566| data | 出参,传感器数据。 | 567| length | 出参,数组长度。 | 568 569**返回:** 570 571如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 572 573 574### OH_SensorEvent_GetTimestamp() 575 576``` 577int32_t OH_SensorEvent_GetTimestamp(Sensor_Event* sensorEvent, int64_t *timestamp) 578``` 579**描述:** 580 581获取传感器数据的时间戳。 582 583**起始版本:** 11 584 585**参数:** 586 587| 名称 | 描述 | 588| -------- | -------- | 589| sensorEvent | 指向传感器数据信息的指针。 | 590| timestamp | 时间戳指针。 | 591 592**返回:** 593 594如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 595 596 597### OH_SensorEvent_GetType() 598 599``` 600int32_t OH_SensorEvent_GetType(Sensor_Event* sensorEvent, Sensor_Type *sensorType) 601``` 602**描述:** 603 604获取传感器类型。 605 606**起始版本:** 11 607 608**参数:** 609 610| 名称 | 描述 | 611| -------- | -------- | 612| sensorEvent | 指向传感器数据信息的指针。 | 613| sensorType | 指向传感器类型的指针。 | 614 615**返回:** 616 617如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 618 619 620### OH_SensorInfo_GetMaxSamplingInterval() 621 622``` 623int32_t OH_SensorInfo_GetMaxSamplingInterval(Sensor_Info* sensor, int64_t *maxSamplingInterval) 624``` 625**描述:** 626 627获取传感器的最大数据上报间隔时间。 628 629**起始版本:** 11 630 631**参数:** 632 633| 名称 | 描述 | 634| -------- | -------- | 635| sensor | 指向传感器信息的指针。 | 636| maxSamplingInterval | 指向最大数据报告间隔的指针,单位为纳秒。 | 637 638**返回:** 639 640如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 641 642 643### OH_SensorInfo_GetMinSamplingInterval() 644 645``` 646int32_t OH_SensorInfo_GetMinSamplingInterval(Sensor_Info* sensor, int64_t *minSamplingInterval) 647``` 648**描述:** 649 650获取传感器的最小数据上报间隔。 651 652**起始版本:** 11 653 654**参数:** 655 656| 名称 | 描述 | 657| -------- | -------- | 658| sensor | 指向传感器信息的指针。 | 659| minSamplingInterval | 指向最小数据报告间隔的指针,以纳秒为单位。 | 660 661**返回:** 662 663如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 664 665 666### OH_SensorInfo_GetName() 667 668``` 669int32_t OH_SensorInfo_GetName (Sensor_Info* sensor, char *sensorName, uint32_t *length) 670``` 671**描述:** 672 673获取传感器名称。 674 675**起始版本:** 11 676 677**参数:** 678 679| 名称 | 描述 | 680| -------- | -------- | 681| sensor | 指向传感器信息的指针。 | 682| sensorName | 指向传感器名称的指针。 | 683| length | 指向长度的指针,以字节为单位。 | 684 685**返回:** 686 687如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 688 689 690### OH_SensorInfo_GetResolution() 691 692``` 693int32_t OH_SensorInfo_GetResolution(Sensor_Info* sensor, float *resolution) 694``` 695**描述:** 696 697获取传感器分辨率。 698 699**起始版本:** 11 700 701**参数:** 702 703| 名称 | 描述 | 704| -------- | -------- | 705| sensor | 指向传感器信息的指针。 | 706| resolution | 指向传感器分辨率的指针。 | 707 708**返回:** 709 710如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 711 712 713### OH_SensorInfo_GetType() 714 715``` 716int32_t OH_SensorInfo_GetType(Sensor_Info* sensor, Sensor_Type *sensorType) 717``` 718**描述:** 719 720获取传感器类型。 721 722**起始版本:** 11 723 724**参数:** 725 726| 名称 | 描述 | 727| -------- | -------- | 728| sensor | 指向传感器信息的指针。 | 729| sensorType | 指向传感器类型的指针。 | 730 731**返回:** 732 733如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 734 735 736### OH_SensorInfo_GetVendorName() 737 738``` 739int32_t OH_SensorInfo_GetVendorName(Sensor_Info* sensor, char *vendorName, uint32_t *length) 740``` 741**描述:** 742 743获取传感器的厂商名称。 744 745**起始版本:** 11 746 747**参数:** 748 749| 名称 | 描述 | 750| -------- | -------- | 751| sensor | 指向传感器信息的指针。 | 752| vendorName | 指向供应商名称的指针。 | 753| length | 指向长度的指针,以字节为单位。 | 754 755**返回:** 756 757如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 758 759 760### OH_SensorSubscriber_GetCallback() 761 762``` 763int32_t OH_SensorSubscriber_GetCallback(Sensor_Subscriber* subscriber, Sensor_EventCallback *callback) 764``` 765**描述:** 766 767获取用于报告传感器数据的回调函数。 768 769**起始版本:** 11 770 771**参数:** 772 773| 名称 | 描述 | 774| -------- | -------- | 775| subscriber | 指向传感器订阅者信息的指针。 | 776| callback | 指向回调函数的指针。 | 777 778**返回:** 779 780如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 781 782 783### OH_SensorSubscriber_SetCallback() 784 785``` 786int32_t OH_SensorSubscriber_SetCallback (Sensor_Subscriber* subscriber, const Sensor_EventCallback callback ) 787``` 788**描述:** 789 790设置一个回调函数来报告传感器数据。 791 792**起始版本:** 11 793 794**参数:** 795 796| 名称 | 描述 | 797| -------- | -------- | 798| subscriber | 指向传感器订阅者信息的指针。 | 799| callback | 设置回调函数。 | 800 801**返回:** 802 803如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 804 805 806### OH_SensorSubscriptionAttribute_GetSamplingInterval() 807 808``` 809int32_t OH_SensorSubscriptionAttribute_GetSamplingInterval (Sensor_SubscriptionAttribute * attribute, int64_t * samplingInterval ) 810``` 811**描述:** 812 813获取传感器数据报告间隔。 814 815**起始版本:** 11 816 817**参数:** 818 819| 名称 | 描述 | 820| -------- | -------- | 821| attribute | 指向传感器订阅属性的指针。 | 822| samplingInterval | 指向数据报告间隔的指针,以纳秒为单位。 | 823 824**返回:** 825 826如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 827 828 829### OH_SensorSubscriptionAttribute_SetSamplingInterval() 830 831``` 832int32_t OH_SensorSubscriptionAttribute_SetSamplingInterval(Sensor_SubscriptionAttribute* attribute, const int64_t samplingInterval) 833``` 834**描述:** 835 836设置传感器数据报告间隔。 837 838**起始版本:** 11 839 840**参数:** 841 842| 名称 | 描述 | 843| -------- | -------- | 844| attribute | 指向传感器订阅属性的指针。 | 845| samplingInterval | 要设置的数据报告间隔,以纳秒为单位。 | 846 847**返回:** 848 849如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 850 851 852### OH_SensorSubscriptionId_GetType() 853 854``` 855int32_t OH_SensorSubscriptionId_GetType(Sensor_SubscriptionId* id, Sensor_Type *sensorType) 856``` 857**描述:** 858 859获取传感器类型。 860 861**起始版本:** 11 862 863**参数:** 864 865| 名称 | 描述 | 866| -------- | -------- | 867| id | 指向传感器订阅ID的指针。 | 868| sensorType | 指向传感器类型的指针。 | 869 870**返回:** 871 872如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 873 874 875### OH_SensorSubscriptionId_SetType() 876 877``` 878int32_t OH_SensorSubscriptionId_SetType(Sensor_SubscriptionId* id, const Sensor_Type sensorType) 879``` 880**描述:** 881 882设置传感器类型。 883 884**起始版本:** 11 885 886**参数:** 887 888| 名称 | 描述 | 889| -------- | -------- | 890| id | 指向传感器订阅ID的指针。 | 891| sensorType | 要设置的传感器类型。 | 892 893**返回:** 894 895如果操作成功返回**SENSOR_SUCCESS**;否则返回 [Sensor_Result](#sensor_result) 中定义的错误代码。 896