the Accelerometer on Cordova is not working
I write small app to get Accelerometer View in html page but my problem is
not getting XYZ value after on my test device this is my javascript BTW I
ran the command
cordova plugin add
https://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion.git
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
this.testZone = {};
this.watchID = null;
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('pause', this.onPause, false);
document.addEventListener('resume', this.onResume, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the
'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
console.log("Ready");
//app.receivedEvent('deviceready');
app.testZone = document.getElementById("test-zone");
app.testZone.innerHTML = "Ready";
app.getDeviceInfo();
app.getConnectionType();
app.startAccelWatch();
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
},
onPause: function(){
app.testZone.innerHTML += "<br/> Pause";
},
onResume: function(){
app.testZone.innerHTML += "<br/> Resume";
},
getDeviceInfo: function(){
app.testZone.innerHTML += 'Device Name: ' + device.name +
'<br />' +
'Device Cordova: ' + device.cordova + '<br />' +
'Device Platform: ' + device.platform + '<br />' +
'Device UUID: ' + device.uuid + '<br />' +
'Device Version: ' + device.version + '<br />';
},
getConnectionType: function(){
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
app.testZone.innerHTML += "type: "+ states[networkState];
}
,
startAccelWatch: function(){
var option = { frequency: 100 };
app.watchID =
navigator.accelerometer.watchAcceleration(app.onSuccess,
app.onError, option);
},
onSuccess: function(acceleration){
app.testZone.innerHTML = 'Acceleration X: ' + acceleration.x +
'\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
},
onError: function(){
app.testZone.innerHTML +="er00r";
}
};
No comments:
Post a Comment