tt.startDeviceMotionListening
基础库 2.92.0 开始支持本方法,这是一个异步方法。
创建设备方向监听实例,开始监听设备方向的变化。
| 前提条件 | 无 |
| 业务背景 | 无 |
| 使用限制 | 无 |
| 注意事项 | 无 |
| 相关教程 | 无 |
语法
tt.startDeviceMotionListening(options)
参数说明
options 为 object 类型,属性如下:
| 属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
|---|---|---|---|---|---|
| interval | enum | 是 | 监听设备方向的变化回调函数的执行频率,单位为ms/次 | 2.92.0 | |
| success | function | 否 | 接口调用成功的回调函数 | 2.92.0 | |
| fail | function | 否 | 接口调用失败的回调函数 | 2.92.0 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 2.92.0 |
interval 的合法值
| 值 | 说明 | 最低支持版本 |
|---|---|---|
| normal | 普通的回调频率:约200ms/次 | 2.92.0 |
| game | 适用于更新UI的回调频率:约60ms/次 | 2.92.0 |
| ui | 适用 于更新游戏的回调频率:约20ms/次 | 2.92.0 |
回调成功
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| errMsg | string | "startDeviceMotionListening:ok" | 2.92.0 |
回调失败
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| errMsg | string | "startDeviceMotionListening:fail" + 详细错误信息 | 2.92.0 |
错误码
| errNo | errMsg | 说明 | 最低支持版本 |
|---|---|---|---|
| 20001 | expect params.interval to be one of "normal", "ui", "game", but got xxx | 参数校验错误 | 2.92.0 |
扫码体验
请使用字节宿主APP扫码
代码示例
tt.startDeviceMotionListening({ interval: "normal", success(res) { console.log("startDeviceMotionListening success"); tt.onDeviceMotionChange((res) => { console.log("设备的偏航角为:", res.alpha); console.log("设备的俯仰角为:", res.beta); console.log("设备的翻滚角为:", res.gamma); }); }, fail(res) { console.log("startDeviceMotionListening fail"); }, complete(res) { console.log("startDeviceMotionListening complete"); }, });
