@expo-harmony/expo-sensors 55.0.15-harmony.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # @expo-harmony/expo-sensors
2
+
3
+ [**GitHub 仓库**](https://github.com/renbaoshuo/expo-harmony/tree/master/packages/expo-sensors) | [官方文档](https://docs.expo.dev/versions/v55.0.0/sdk/sensors/)
4
+
5
+ 为 HarmonyOS 上的 React Native 应用提供 Expo Sensors 的原生实现,与官方同版本的 `expo-sensors` 配套使用。已实现加速度计、陀螺仪、磁力计、未校准磁力计、气压计、环境光、设备运动和计步器。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm install @expo-harmony/expo-sensors expo-sensors@55.0.15
11
+ ```
12
+
13
+ 本包适配 Expo SDK 55 的 `expo-sensors`,原生模块通过 Expo Harmony 自动链接,不需要配置插件。最低支持 HarmonyOS 5.0.1(API 13),宿主的 `compatibleSdkVersion` 也要满足这一要求。
14
+
15
+ HAR 声明了 `ohos.permission.ACCELEROMETER`、`ohos.permission.GYROSCOPE` 和 `ohos.permission.ACTIVITY_MOTION`。加速度和陀螺仪属于系统权限,随安装授予,不会弹窗;运动权限用于计步器,订阅前需要申请,权限的 `usedScene` 默认挂在 `EntryAbility` 的 `inuse` 场景上,宿主的入口 Ability 使用其他名称时要改成实际名称。
16
+
17
+ 业务代码从官方包导入:
18
+
19
+ ```ts
20
+ import { Accelerometer, DeviceMotion, Pedometer } from 'expo-sensors';
21
+
22
+ Accelerometer.addListener(({ x, y, z }) => console.log(x, y, z));
23
+ ```
24
+
25
+ ## API 对照表
26
+
27
+ ### Classes
28
+
29
+ #### `Accelerometer`
30
+
31
+ 测量三轴加速度,单位 `g`。
32
+
33
+ #### `Barometer`
34
+
35
+ 测量气压,单位 `hPa`。
36
+
37
+ #### `DeviceMotion`
38
+
39
+ 测量设备的运动状态,同时依赖加速度、线性加速度、重力、陀螺仪和旋转矢量五个传感器。五个传感器全部可用时才有数据,缺任何一个都没有更新;单个字段在对应传感器首次上报前为 `null`。更新间隔默认 `1000 / 60` 毫秒,其他传感器默认 100 毫秒。
40
+
41
+ 加速度的单位和坐标约定与 Expo Android 一致。`rotation` 的 `alpha`、`beta`、`gamma` 是弧度,`rotationRate` 是度每秒。`orientation` 取屏幕旋转方向,值为 `0`、`90`、`180` 或 `-90`。
42
+
43
+ #### `Gyroscope`
44
+
45
+ 测量三轴角速度,单位 `rad/s`。
46
+
47
+ #### `LightSensor`
48
+
49
+ 测量环境光,单位 `lux`。
50
+
51
+ #### `Magnetometer`
52
+
53
+ 测量三轴磁场强度,单位 `μT`。
54
+
55
+ #### `MagnetometerUncalibrated`
56
+
57
+ 测量未经校准的三轴磁场强度,单位 `μT`。
58
+
59
+ #### `DeviceSensor`
60
+
61
+ 传感器的基类,上面七个传感器都继承它。以下方法在所有传感器上都可用。
62
+
63
+ ##### `addListener(listener)`
64
+
65
+ 返回 `Subscription`,订阅测量更新。订阅前应先用 `isAvailableAsync()` 检查硬件。订阅期间应用切到后台会暂停上报,回到前台后恢复;订阅一时失败会自动重试三次。传感器不可用或缺少权限时订阅静默失败,不向调用方抛错,只是收不到更新。
66
+
67
+ ##### `isAvailableAsync()`
68
+
69
+ 返回 `Promise<boolean>`,设备是否提供该传感器。
70
+
71
+ ##### `setUpdateInterval(intervalMs)`
72
+
73
+ 设置更新间隔,单位毫秒,默认 100。传入负数、非有限数或超出安全整数范围的值时抛出 `ERR_SENSOR_INTERVAL`。实际频率受硬件和系统限制。上报按间隔节流,节流依据设备开机时长,不受系统时间调整影响。
74
+
75
+ ##### `hasListeners()`
76
+
77
+ 返回 `boolean`,是否有已注册的监听。
78
+
79
+ ##### `getListenerCount()`
80
+
81
+ 返回 `number`,已注册的监听数量。
82
+
83
+ ##### `removeAllListeners()`
84
+
85
+ 移除该传感器的全部监听。
86
+
87
+ ##### `removeSubscription(subscription)`
88
+
89
+ 取消指定的监听。官方已标记废弃,改调 `subscription.remove()`。
90
+
91
+ ##### `getPermissionsAsync()`
92
+
93
+ 返回 `Promise<PermissionResponse>`。加速度计、陀螺仪和设备运动读取系统权限状态;磁力计、未校准磁力计、气压计和环境光不需要权限,直接返回已授权。
94
+
95
+ ##### `requestPermissionsAsync()`
96
+
97
+ 返回 `Promise<PermissionResponse>`,取值与 `getPermissionsAsync()` 一致。加速度计和陀螺仪的权限随安装授予,不会弹出授权对话框。
98
+
99
+ ### Constants
100
+
101
+ #### `DeviceMotion.Gravity`
102
+
103
+ 类型:`number`,值为 `9.80665`。
104
+
105
+ ### Methods
106
+
107
+ #### `Pedometer.watchStepCount(callback)`
108
+
109
+ 返回 `Subscription`,订阅步数更新,需要运动权限。步数更新按默认的 100 毫秒间隔节流,`Pedometer` 上没有调整间隔的方法。没有运动权限或设备没有计步器时订阅静默失败,不抛错。
110
+
111
+ 系统上报的是开机以来的累计步数,模块会减去订阅时刻的基线,所以 `steps` 从订阅那一刻重新计数。设备记录被系统清零时,模块会接续之前的计数。
112
+
113
+ #### `Pedometer.isAvailableAsync()`
114
+
115
+ 返回 `Promise<boolean>`,设备是否提供计步器。
116
+
117
+ #### `Pedometer.getPermissionsAsync()`
118
+
119
+ 返回 `Promise<PermissionResponse>`,读取运动权限状态。
120
+
121
+ `status` 可能为 `'granted'`、`'denied'` 或 `'undetermined'`。API 20 及以上由系统区分未决定和已拒绝;API 13 到 API 19 的系统查询只能给出已授权或未授权,模块自己记录用户有没有被问过,没问过时按未决定返回。
122
+
123
+ #### `Pedometer.requestPermissionsAsync()`
124
+
125
+ 返回 `Promise<PermissionResponse>`,弹出授权对话框,取值与 `getPermissionsAsync()` 一致。同时发起的多次调用合并为一次请求,授权成功后正在等待的订阅自动恢复。权限没有在 `module.json5` 中声明时抛出 `ERR_SENSOR_PERMISSION`。
126
+
127
+ > **未实现的内容**
128
+ >
129
+ > - `Pedometer.getStepCountAsync()`:iOS 专属接口,HarmonyOS 上没有按时间段查询步数的能力,调用抛出 `ERR_NOT_SUPPORTED`。
130
+
131
+ ### Types
132
+
133
+ #### `AccelerometerMeasurement`
134
+
135
+ | 属性 | 类型 | 说明 |
136
+ | ------------- | -------- | ------------------------ |
137
+ | `x`、`y`、`z` | `number` | 三个轴的加速度,单位 `g` |
138
+ | `timestamp` | `number` | 测量时间,单位秒 |
139
+
140
+ #### `BarometerMeasurement`
141
+
142
+ | 属性 | 类型 | 说明 |
143
+ | ----------- | -------- | ---------------- |
144
+ | `pressure` | `number` | 气压,单位 `hPa` |
145
+ | `timestamp` | `number` | 测量时间,单位秒 |
146
+
147
+ > **未实现的内容**
148
+ >
149
+ > - `BarometerMeasurement.relativeAltitude`:iOS 专属字段,HarmonyOS 上没有取值来源。
150
+
151
+ #### `GyroscopeMeasurement`
152
+
153
+ | 属性 | 类型 | 说明 |
154
+ | ------------- | -------- | ---------------------------- |
155
+ | `x`、`y`、`z` | `number` | 三个轴的角速度,单位 `rad/s` |
156
+ | `timestamp` | `number` | 测量时间,单位秒 |
157
+
158
+ #### `LightSensorMeasurement`
159
+
160
+ | 属性 | 类型 | 说明 |
161
+ | ------------- | -------- | ---------------------- |
162
+ | `illuminance` | `number` | 环境光照度,单位 `lux` |
163
+ | `timestamp` | `number` | 测量时间,单位秒 |
164
+
165
+ #### `MagnetometerMeasurement`
166
+
167
+ | 属性 | 类型 | 说明 |
168
+ | ------------- | -------- | --------------------------- |
169
+ | `x`、`y`、`z` | `number` | 三个轴的磁场强度,单位 `μT` |
170
+ | `timestamp` | `number` | 测量时间,单位秒 |
171
+
172
+ #### `MagnetometerUncalibratedMeasurement`
173
+
174
+ 字段与 `MagnetometerMeasurement` 相同,数值未经校准。
175
+
176
+ #### `DeviceMotionMeasurement`
177
+
178
+ | 属性 | 类型 | 说明 |
179
+ | ------------------------------ | ------------------------------------------- | ----------------------------- |
180
+ | `acceleration` | `{ x, y, z, timestamp } \| null` | 不含重力的加速度,单位 `m/s²` |
181
+ | `accelerationIncludingGravity` | `{ x, y, z, timestamp } \| null` | 含重力的加速度,单位 `m/s²` |
182
+ | `rotation` | `{ alpha, beta, gamma, timestamp } \| null` | 设备在空间中的姿态,单位弧度 |
183
+ | `rotationRate` | `{ alpha, beta, gamma, timestamp } \| null` | 旋转速率,单位度每秒 |
184
+ | `interval` | `number` | 更新间隔,单位毫秒 |
185
+ | `orientation` | `DeviceMotionOrientation` | 屏幕旋转方向 |
186
+
187
+ #### `PedometerResult`
188
+
189
+ | 属性 | 类型 | 说明 |
190
+ | ------- | -------- | ---- |
191
+ | `steps` | `number` | 步数 |
192
+
193
+ #### `PedometerUpdateCallback`
194
+
195
+ `(result: PedometerResult) => void`。
196
+
197
+ #### `Listener`
198
+
199
+ `(event: Measurement) => void`。
200
+
201
+ #### `Subscription`
202
+
203
+ 取消订阅的对象,实现见 `addListener()`。
204
+
205
+ #### `PermissionExpiration`
206
+
207
+ `'never' | number`,恒为 `'never'`。
208
+
209
+ #### `PermissionResponse`
210
+
211
+ | 属性 | 类型 | 说明 |
212
+ | ------------- | ---------------------- | --------------------------------------------- |
213
+ | `status` | `PermissionStatus` | `'granted'`、`'denied'` 或 `'undetermined'` |
214
+ | `granted` | `boolean` | 是否已授权 |
215
+ | `canAskAgain` | `boolean` | 已授权或未决定时为 `true`,被拒绝后为 `false` |
216
+ | `expires` | `PermissionExpiration` | 恒为 `'never'` |
217
+
218
+ ### Enums
219
+
220
+ #### `DeviceMotionOrientation`
221
+
222
+ `Portrait = 0`、`RightLandscape = 90`、`UpsideDown = 180`、`LeftLandscape = -90`。
223
+
224
+ #### `PermissionStatus`
225
+
226
+ `'granted' | 'denied' | 'undetermined'`,三个取值都会出现。
227
+
228
+ ## Author
229
+
230
+ **expo-harmony** © [Baoshuo](https://github.com/renbaoshuo), Released under the MIT License.<br>
231
+ Authored and maintained by Baoshuo with help from [contributors](https://github.com/renbaoshuo/expo-harmony/contributors)
232
+
233
+ > [Personal Website](https://baoshuo.ren) · [Blog](https://blog.baoshuo.ren) · GitHub [@renbaoshuo](https://github.com/renbaoshuo) · Twitter [@renbaoshuo](https://twitter.com/renbaoshuo)
@@ -0,0 +1,17 @@
1
+ {
2
+ "platforms": [
3
+ "harmony"
4
+ ],
5
+ "harmony": {
6
+ "modules": [
7
+ "AccelerometerModule",
8
+ "BarometerModule",
9
+ "DeviceMotionModule",
10
+ "GyroscopeModule",
11
+ "LightSensorModule",
12
+ "MagnetometerModule",
13
+ "MagnetometerUncalibratedModule",
14
+ "PedometerModule"
15
+ ]
16
+ }
17
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@expo-harmony/expo-sensors",
3
+ "version": "55.0.15-harmony.1",
4
+ "description": "HarmonyOS port of expo-sensors",
5
+ "keywords": [
6
+ "react-native",
7
+ "expo",
8
+ "expo-harmony",
9
+ "harmonyos"
10
+ ],
11
+ "homepage": "https://github.com/renbaoshuo/expo-harmony/tree/master/packages/expo-sensors#readme",
12
+ "repository": "https://github.com/renbaoshuo/expo-harmony",
13
+ "author": {
14
+ "name": "Baoshuo",
15
+ "email": "i@baoshuo.ren",
16
+ "url": "https://github.com/renbaoshuo"
17
+ },
18
+ "main": "Index.ets",
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "@expo-harmony/expo-modules-core": "55.0.25-harmony.3"
22
+ }
23
+ }
Binary file
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ module.exports = Object.freeze({});
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@expo-harmony/expo-sensors",
3
+ "version": "55.0.15-harmony.1",
4
+ "description": "HarmonyOS port of expo-sensors",
5
+ "keywords": [
6
+ "react-native",
7
+ "expo",
8
+ "expo-harmony",
9
+ "harmonyos"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "Baoshuo <i@baoshuo.ren>",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/renbaoshuo/expo-harmony.git",
16
+ "directory": "packages/expo-sensors"
17
+ },
18
+ "homepage": "https://github.com/renbaoshuo/expo-harmony/tree/master/packages/expo-sensors#readme",
19
+ "bugs": "https://github.com/renbaoshuo/expo-harmony/issues",
20
+ "main": "index.js",
21
+ "types": "index.d.ts",
22
+ "scripts": {
23
+ "harmony:clean": "expo-harmony-module prepare --clean-only",
24
+ "harmony:build": "expo-harmony-module prepare",
25
+ "prepack": "expo-harmony-module prepack"
26
+ },
27
+ "peerDependencies": {
28
+ "@expo-harmony/expo-modules-core": "55.0.25-harmony.3",
29
+ "expo-sensors": "55.0.15"
30
+ },
31
+ "devDependencies": {
32
+ "@expo-harmony/expo-module-scripts": "55.0.0-harmony.3",
33
+ "@expo-harmony/expo-modules-core": "55.0.25-harmony.3",
34
+ "@react-native-oh/react-native-harmony": "0.84.1",
35
+ "expo": "55.0.26",
36
+ "expo-modules-core": "55.0.25",
37
+ "expo-sensors": "55.0.15",
38
+ "react": "19.2.3",
39
+ "react-native": "0.84.1"
40
+ },
41
+ "engines": {
42
+ "node": ">=20"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ }
47
+ }