更新记录
1.0.0(2024-04-09)
陀螺仪、加速度传感器、摇一摇
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:4.4,iOS:9,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
陀螺仪加速度传感器摇一摇
权限
"<uses-permission android:name=\"android.permission.VIBRATE\"/>"
接口
import {
UTSShare
} from "@/uni_modules/wrs-uts-shake"
let share = new UTSShare()
share.setParams({
accelerometerUpdateInterval: 1
})
// 陀螺仪
if (this.isAndroid) {
var params = {}
// 4: 陀螺仪 1:加速度传感器
params.type = 4
// * 多久获取一次数据
// 0: SENSOR_DELAY_FASTEST 最快
// 1: SENSOR_DELAY_GAME 适合游戏
// 2: SENSOR_DELAY_UI 绘画UI
// 3: SENSOR_DELAY_NORMAL 普通界面
params.samplingPeriodUs = 2
share.startSensor(params, (resp) => {
// this.showMsg(JSON.stringify(resp))
var sensorEvent = resp.sensorEvent
if (sensorEvent) {
if (sensorEvent.values.length >= 3) {
var x = sensorEvent.values[0]
var y = sensorEvent.values[1]
var z = sensorEvent.values[2]
var deltaX = x - this.x;
var deltaY = y - this.y;
var deltaZ = z - this.z;
this.x = x
this.y = y
this.z = z
}
}
})
} else {
share.startGyroUpdates((resp) => {
this.showMsg(JSON.stringify(resp))
if (resp.data) {
let y = resp.data.y
let x = resp.data.x
let z = resp.data.z
this.x = x
this.y = y
this.z = z
}
})
}
if (this.isAndroid) {
var params = {}
params.type = 4
share.stopSensor(params)
} else {
share.stopGyroUpdates()
}
// 加速度传感器
if (this.isAndroid) {
var params = {}
// 4: 陀螺仪 1:加速度传感器
params.type = 1
// * 多久获取一次数据
// 0: SENSOR_DELAY_FASTEST 最快
// 1: SENSOR_DELAY_GAME 适合游戏
// 2: SENSOR_DELAY_UI 绘画UI
// 3: SENSOR_DELAY_NORMAL 普通界面
params.samplingPeriodUs = 2
share.startSensor(params, (resp) => {
// this.showMsg(JSON.stringify(resp))
var opt = resp.opt
switch (opt) {
// 值发生变化
case "onSensorChanged":
var sensorEvent = resp.sensorEvent
if (sensorEvent) {
if (sensorEvent.values.length >= 3) {
var x = sensorEvent.values[0]
var y = sensorEvent.values[1]
var z = sensorEvent.values[2]
var deltaX = x - this.x;
var deltaY = y - this.y;
var deltaZ = z - this.z;
this.x = x
this.y = y
this.z = z
var GRAVITY_EARTH = 9.80665
var accelerameter = Math.sqrt(deltaX * deltaX + deltaY * deltaY +
deltaZ *
deltaZ) / GRAVITY_EARTH
// console.log("accelerameter:" + accelerameter)
if (accelerameter > 3.0) {
// 毫秒
var currentTime = Date.now()
if (this.lastShakeTime == 0) {
this.lastShakeTime = currentTime
} else {
var between = currentTime - this.lastShakeTime
if (between >= 1000) {
console.log("accelerameter:" + accelerameter)
this.shakeAction()
}
this.lastShakeTime = currentTime
}
}
}
}
break;
// 精确度发生变化
case "onAccuracyChanged":
break;
default:
break;
}
})
} else {
share.startAccelerometerUpdates((resp) => {
this.showMsg(JSON.stringify(resp))
if (resp.data) {
let y = resp.data.y
let x = resp.data.x
let z = resp.data.z
this.x = x
this.y = y
this.z = z
let accelerameter = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))
//
if (accelerameter > 0.7) {
// 毫秒
var currentTime = Date.now()
if (this.lastShakeTime == 0) {
this.lastShakeTime = currentTime
} else {
var between = currentTime - this.lastShakeTime
if (between > 1000) {
console.log("accelerameter:" + accelerameter)
this.shakeAction()
}
this.lastShakeTime = currentTime
}
}
}
})
}
if (this.isAndroid) {
var params = {}
params.type = 1
share.stopSensor(params)
} else {
share.stopAccelerometerUpdates()
}