Accelerometer woes on Android

491 views
Skip to first unread message

Matias

unread,
Mar 3, 2011, 4:30:41 AM3/3/11
to phonegap
Hello,
I'm having trouble getting the accelerometer to work on Android 2.2.1.
Actually, the Phonegap sample project works fine, but in the project
I'm working on ( http://charlie-roberts.com/Control/ ), it does not:
for some reason, navigator.accelerometer is undefined (the navigator
object itself is not). I found this in phonegap.js:
if (typeof navigator.accelerometer == "undefined")
navigator.accelerometer = new Accelerometer();
If I manually execute that check, then I get a valid
navigator.accelerometer as a result, and I can even register callbacks
successfully (I get an ID number from watchAcceleration, and the
success callback gets called periodically). But, in the success
callback:
function onSuccess(acceleration) {
console.log("acceleration at " + acceleration.timestamp + ": "
+ acceleration.x + ", " + acceleration.y + ", " + acceleration.z);
},
I always see the same timestamp, and all components of acceleration
are 0.

My questions:
- Why is navigator.accelerometer not a valid object to begin with?
What can I do to ensure that it is?
- Is there a way to make my workaround actually do what I'd like (ie,
manually instantiate the accelerometer so that it works)?

Simon MacDonald

unread,
Mar 3, 2011, 3:24:06 PM3/3/11
to phon...@googlegroups.com
Matias,

I looked at your source code at:

https://github.com/charlieroberts/Control/blob/master/Android/Sample/assets/www/index.html

and it loads up PhoneGap from:

https://github.com/charlieroberts/Control/blob/master/Android/Sample/assets/www/js/phonegap-uncompressed.js

but that version of PhoneGap is for the iPhone. That is most probably
the reason why you code is not working on Android. There is a
different version of phonegap.js built for each platform.

Simon Mac Donald
http://hi.im/simonmacdonald

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Matias

unread,
Mar 4, 2011, 6:13:18 AM3/4/11
to phonegap
Thanks so much Simon! I was looking in all the wrong places. It works
now :)

> I looked at your source code at:
>
> https://github.com/charlieroberts/Control/blob/master/Android/Sample/...
>
> and it loads up PhoneGap from:
>
> https://github.com/charlieroberts/Control/blob/master/Android/Sample/...

redjhawk

unread,
Mar 21, 2011, 1:25:05 PM3/21/11
to phonegap
Hi!

I have downloaded phonegap from the web (http://
phonegap.googlecode.com/files/phonegap-0.9.4.zip) and i've been trying
to access to the accelerometers in Android by the demo this zip
offers. The html doc i'm accessing from my mobile phone is, once it's
been unziped, ~\phonegap-0.9.4\Android\Sample\assets\www

I can access to the geoposition and call, but nothing more works.

Thank you very much!

Simon MacDonald

unread,
Mar 21, 2011, 8:39:11 PM3/21/11
to phon...@googlegroups.com
Hey,

You aren't really using PhoneGap correctly. You'll need to setup an
application using PhoneGap in order to access the accelerometer. See:

http://www.phonegap.com/start/#android

for info on getting started.

The reason the geolocation is working for you is that it is built into
the web browser on Android on any 2.X or better device.

redjhawk

unread,
Mar 22, 2011, 4:24:01 PM3/22/11
to phonegap
So, it's required a native app installed in the android device. I
thought this framework creates html for all devices, or did i
misunderstand it? Or it is just that android still doesn't support
accelerometers access from webbrowser?

Thank you very much.


On Mar 22, 12:39 am, Simon MacDonald <simon.macdon...@gmail.com>
wrote:
> Hey,
>
> You aren't really using PhoneGap correctly.  You'll need to setup an
> application using PhoneGap in order to access the accelerometer.  See:
>
> http://www.phonegap.com/start/#android
>
> for info on getting started.
>
> The reason the geolocation is working for you is that it is built into
> the web browser on Android on any 2.X or better device.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

mahaboob basha

unread,
Mar 23, 2011, 1:29:43 AM3/23/11
to phon...@googlegroups.com, redjhawk
Hi All,
 
        I have also did the same thing on phonegap based on the given link (http://www.phonegap.com/start/#android
 
But in my emulator i'm not getting the out put. it is giving blank out put..
 
             please help me on this (mahaboo...@gmail.com) ...
 
Regards,
Basha

--
With regards,
 
Mahaboob(munna)

Simon MacDonald

unread,
Mar 23, 2011, 1:47:53 PM3/23/11
to phon...@googlegroups.com
PhoneGap provides a JavaScript API that is consistent across all
platforms. The real implementation is done in the platforms native
language like Obj-C or Java. You write all your code in HTML/CSS/JS
and when you compile you app for your target platform it is wrapped in
a native layer that is responsible for creating a web view and setting
that web view to your applications HTML code. That's how we can be
device independent but it still requires your app be compiled for each
platform you want it to be deployed on.

The web browser from Android does not have access to accelerometer functions.

mahaboob basha

unread,
Mar 24, 2011, 1:44:04 AM3/24/11
to phon...@googlegroups.com, Simon MacDonald
Hi Simon,

      Then can you tell me what and all the phonegap  features are working in  emulator .

          
Regards,
Basha
--
With regards,
 
Mahaboob(munna)

Simon MacDonald

unread,
Mar 24, 2011, 10:31:14 AM3/24/11
to phonegap
All of the features are working in the emulator. Things like the
camera, geolocation and accelerometer are simulated though.

http://developer.android.com/guide/developing/devices/emulator.html

@neavilag (Norman)

unread,
Mar 24, 2011, 4:27:36 PM3/24/11
to phon...@googlegroups.com, Simon MacDonald, mahaboob basha
Install the sample application from PhoneGap, it show the access to accelerometer via the HTML/JS but is accessed natively from PhoneGap, as Simon answered, from the Android webbrowser, you cannot access accelerometer.

regards

mahaboob basha

unread,
Mar 25, 2011, 2:44:26 AM3/25/11
to phon...@googlegroups.com, @neavilag (Norman), Simon MacDonald
Thanks  for giving rply.

   But i don't know where have i did the mistake. i have done the entire configuration of phonegap .. while trying to use ruby to create project it is showing errors..

Getting the error msg:


./droidgap:4:in `require': no such file to load -- E:/phonegap-android/bin/lib/g
enerate.rb (LoadError)
        from ./droidgap:4



how can i resolve it..

thanks in advance.

Regards,
Basha


On Fri, Mar 25, 2011 at 1:57 AM, @neavilag (Norman) <neav...@gmail.com> wrote:
Install the sample application from PhoneGap, it show the access to accelerometer via the HTML/JS but is accessed natively from PhoneGap, as Simon answered, from the Android webbrowser, you cannot access accelerometer.

regards




--
With regards,
 
Mahaboob(munna)

Reply all
Reply to author
Forward
0 new messages