Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap on Android window.device is undefined

It seems that the device object is undefined on Android. It is defined in the simulator, the iOS device & iOS simulator

Obviously I'm checking to see the window.device (and tried all variants of Window.device, window.Device, etc.), and always getting undefined -- but only on a real Android device. It works fine on all other scenarios.

Please help.

like image 396
Nadav Avatar asked Dec 07 '22 22:12

Nadav


2 Answers

When are you trying to use window.device?

I found that on Android when "ondeviceready" is fired the window.device variable isn't available yet.

So what I did was set a timeout of 1 second after "ondeviceready" is fired and then do all the stuff I want to do.

like image 149
Felipe Brahm Avatar answered Dec 09 '22 11:12

Felipe Brahm


My problem was that even though my plugins folder had the org.apache.cordova.device plugin folder listed, and it was referenced in the config.xml. I still couldn't get it to find window.device (or just device) when onDeviceReady was fired (or anywhere else for that matter)

How I managed to fix it was to run $ cordova plugin add cordova-plugin-device again, expecting a 'plug-in already installed' message but it actually installed into the org.apache.cordova.device folder and then everything just worked.

My folder wasn't empty so I'm not sure why this worked and I'm a little concerned about what will happen when I deploy via phonegap-build but using $ cordova run android, it now works perfectly.

like image 36
Daniel van Heerden Avatar answered Dec 09 '22 12:12

Daniel van Heerden