我使用Cordova CLI创建我的项目,添加了插件cordova-plugin-device-motion 然后按照简单的教程尝试获取值
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<!--<link rel="stylesheet" type="text/css" href="css/index.css">-->
<title>Rotations</title>
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<!--<script type="text/javascript" src="js/rotationHandler.js"></script>-->
<script type="text/javascript" charset="utf-8">
// The watch id references the current `watchAcceleration`
var watchID = null;
// Wait for device API libraries to load
//
//document.addEventListener("deviceready", onDeviceReady, false);
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
console.log('OGDEBUG onDeviceReady');
startWatch();
}
// Start watching the acceleration
//
function startWatch() {
// Update acceleration every 1 seconds
var options = { frequency: 1000 };
try{
watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}catch(ex){
console.log('OGDEBUG startWatch error '+ex);
}
console.log('OGDEBUG startWatch');
}
// Stop watching the acceleration
function stopWatch() {
try{
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}catch(ex){
console.log('OGDEBUG stopWatch error '+ex);
}
console.log('OGDEBUG stopWatch');
}
// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
var accelerationString =
'Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n';
console.log('OGDEBUG onSuccess '+accelerationString);
}
// onError: Failed to get the acceleration
//
function onError() {
alert('onError!');
console.log('OGDEBUG onError' );
}
function buttonTapStart() {
startWatch();
console.log('OGDEBUG buttonTap (index)');
}
function buttonTapStop() {
stopWatch();
console.log('OGDEBUG buttonTap (index)');
}
</script>
<div>
<button onclick="buttonTapStart()">Start Watch</button>
<button onclick="buttonTapStop()">Stop Watch</button>
</div>
</body>
</html>
我的所有catch /错误日志语句都没有被调用,所以它似乎正在工作(之前我收到navigator
未定义,但移动的东西现在似乎很好。)
但我的onSuccess
回调从未被调用过,任何想法为什么?
供参考:
cordova.js和js / index.js都是未经编辑的生成文件,以及由cli创建的文件(针对特定平台)
另请注意,我的onDeviceReady
也从未被调用,但“开始观看”按钮会调用startWatch
我也使用sensorsimulator-2.0-rc1伪造加速度计的值。无论如何(即使那些不起作用,虽然我没有理由怀疑它确实存在,并且它自己的测试工作)我会认为我的onSuccess将被调用0值。
答案 0 :(得分:0)
但是我确实得到了我的机器人x到4.4.4,并且能够对它进行测试 它的工作原理
所以我现在有一个解决方案(代码适用于真实设备)