You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
import {
|
|
DEVICE_FREQUENCY
|
|
} from '../constants'
|
|
|
|
import {
|
|
getLastWebview
|
|
} from '../util'
|
|
|
|
import {
|
|
publish
|
|
} from '../../bridge'
|
|
|
|
let watchAccelerationId = false
|
|
let isWatchAcceleration = false
|
|
|
|
const clearWatchAcceleration = () => {
|
|
if (watchAccelerationId) {
|
|
plus.accelerometer.clearWatch(watchAccelerationId)
|
|
watchAccelerationId = false
|
|
}
|
|
}
|
|
|
|
export function enableAccelerometer ({
|
|
enable
|
|
}) {
|
|
if (enable) { // 启用监听
|
|
clearWatchAcceleration()
|
|
watchAccelerationId = plus.accelerometer.watchAcceleration((res) => {
|
|
publish('onAccelerometerChange', {
|
|
x: res.xAxis,
|
|
y: res.yAxis,
|
|
z: res.zAxis,
|
|
errMsg: 'enableAccelerometer:ok'
|
|
})
|
|
}, (e) => {
|
|
publish('onAccelerometerChange', {
|
|
errMsg: 'enableAccelerometer:fail'
|
|
})
|
|
}, {
|
|
frequency: DEVICE_FREQUENCY
|
|
})
|
|
if (!isWatchAcceleration) {
|
|
isWatchAcceleration = true
|
|
const webview = getLastWebview()
|
|
if (webview) {
|
|
webview.addEventListener('close', clearWatchAcceleration)
|
|
}
|
|
}
|
|
} else {
|
|
clearWatchAcceleration()
|
|
}
|
|
return {
|
|
errMsg: 'enableAccelerometer:ok'
|
|
}
|
|
}
|
|
|