Software Archive
Read-only legacy content
17060 Discussions

Accelerometer is not working in phone but working properly in emulator

Anjali_P_
Beginner
807 Views

Hi

I have created accelerometer example using accelerometer object and its method. It works properly in emulator but not in phone. Please reply as soon as possible.The below function is called on window onload. My code is below : 

function init()
{
    try
    {
        if(intel.xdk.accelerometer)
            alert("accelerometer is supported");
        else
            alert("accelerometer is not supported");
        intel.xdk.accelerometer.getCurrentAcceleration(
            function(acceleration)
            {
                document.getElementById("text").innerHTML = 'Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: '      + acceleration.timestamp + '\n';
            });
    }
    catch(e)

{

alert('catched');}

};

 

Thanks & Regards, 

Anjali Paria.

0 Kudos
3 Replies
John_H_Intel2
Employee
807 Views

Is your init() function being called after onDeviceReady has fired?

You should use the Cordova version of the accelerometer of the plugin, not the intel.xdk version.

0 Kudos
Elroy_A_Intel
Employee
807 Views

I recommend using the Accelerometer (device-motion) Cordova plugin. You can find more information at https://github.com/apache/cordova-plugin-device-motion.

0 Kudos
Anusha_M_Intel1
Employee
807 Views

Hi Anjali, the xdk plugins are no longer actively maintained. We recommended using the device-motion cordova plugin like Elroy pointed out. You can also refer to the Hello Cordova sample (ships with the XDK) for an example. 

 js file with accelerometer code: https://github.com/gomobile/sample-hello-cordova/blob/master/www/js/cordova-acc.js

 

0 Kudos
Reply