lola_the_coding_girl
lola_the_coding_girl

Reputation: 863

Cordova device plugin not working

I have a simple Phonegap Build application where I am trying to use the device plugin. I have installed the plugin via command line and have confirmed that it is installed.

I have the following js inside onDeviceReady:

alert(device.platform); and alert(device.model);

I get white screen with no alerts.

Chrome dev tools remote inspect console says: Uncaught ReferenceError: platform is not defined

The plugin is not being recognized.

Upvotes: 3

Views: 13941

Answers (6)

Théo T. Carranza
Théo T. Carranza

Reputation: 8459

If this is Angular/Ionic, you need to do two things:

  1. import { Device } from '@ionic-native/device/ngx' <- this is how the import should look like
  2. Do the same import on the module that contains the components you gonna use the Device class, and add the cass to that module's provider.

Upvotes: 0

iSandeep
iSandeep

Reputation: 725

Try this

<html>
  <head>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">
    
    //add event listener
    document.addEventListener("deviceready", onDeviceReady, false);
    
    //device.name -> Gives the name of device.
    //device.cordova -> Gives current version of cordova running on device.
    //device.platrof -> Gives the name of platform.
    //device.uuid -> Gives the UUID.
    //device.version -> Gives the Android Version.

    function onDeviceReady() {
        var vInfo =  'Device Name: '     + device.name     + '\n' +
                            'Device Cordova: '  + device.cordova  + '\n' +
                            'Device Platform: ' + device.platform + '\n' +
                            'Device UUID: '     + device.uuid     + '\n' +
                            'Device Version: '  + device.version;
       alert(vInfo);
    }

    </script>
  </head>
  <body>
  </body>
</html>

Upvotes: 3

Paul Schorey
Paul Schorey

Reputation: 1182

I was stumped with this same problem for awhile. First I built to iOS and everything was working great, then I went to add the Android platform to make the app for Android and began getting the error.

Re-adding the plugin to my project after creating the Android platform fixed the error for me. I'm sure there have to be others who have had the same issue, so I wanted to share.

cordova plugin add cordova-plugin-device

Upvotes: 0

Kishore Vaishnav
Kishore Vaishnav

Reputation: 510

Add this cordova pluign using this commend :

cordova plugin add cordova-plugin-device

read More

Try this
<html>
  <head>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript">

    //add event listener
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() { 
       console.log(device); // device object 
       console.log(device.name); // Gives the name of device.
       console.log(device.uuid ); // Gives the UUID.
    }

    </script>
  </head>
  <body>

  </body>
</html>

Upvotes: 6

404
404

Reputation: 1155

I hope you can solve this issue by using this cordova device plugin.Its an updated as well as live plugin with properties including model,platform,uuid,version,manufacturer,serial number etc...

you can add this plugin to your project via cli using the command

cordova plugin add cordova-plugin-device

if you have added any previous device plugins, please remove that prior to adding this plugin.

Upvotes: 0

jcesarmobile
jcesarmobile

Reputation: 53361

If you are using phonegap build you don't have to install the plugins with the CLI, you have to add it in the config.xml file that you upload to phonegap build

Add this line:

<plugin name="cordova-plugin-device"/>

And don't use it before the deviceready event is fired.

http://docs.build.phonegap.com/en_US/configuring_plugins.md.html#Plugins

Upvotes: 0

Related Questions