3 Replies Latest reply on Sep 14, 2016 12:08 PM by Ro4cHii

    Cannot get cordova-plugin-device to work


      Hello everyone!


      I am pretty new to PhoneGap and since I couldn't find a solution via Google nor in this forum, I decided to start a new topic.


      I use PhoneGap Build to create my mobile app. Besides some css I have two files in my root directory: index.html and config.xml.



      <!DOCTYPE html>
      <html lang="en">
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <link rel="stylesheet" type="text/css" href="css/style.css" />
        <link rel="stylesheet" type="text/css" href="css/font-awesome.css" />
        <link rel='stylesheet' type='text/css' href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:700,300' />
        <script src="cordova.js"></script>
        <script type="text/javascript">
          document.addEventListener("deviceready", onDeviceReady, false);
          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;
          <div class="container">
            <section class="main">
              <button onclick="window.plugins.toast.showShortBottom('Hello World!', function(a){console.log('toast success: ' + a)}, function(b){alert('toast error: ' + b)})">Show Toast</button>
              <button onclick="alert('Device name: ' + device.name)">Show Device Cordova</button>



      <widget xmlns   = "http://www.w3.org/ns/widgets"
        xmlns:gap   = "http://phonegap.com/ns/1.0"
        id="de.sample.app" version="0.0.1">
        <plugin name="cordova-plugin-device" spec="1.1.2" source="npm" />
        <plugin name="cordova-plugin-dialogs" spec="1.2.1" source="npm" />
        <plugin name="cordova-plugin-x-toast" source="npm" />
        <preference name='phonegap-version' value='cli-6.3.0' />
        <name>Sample App</name>
          A sample app.
        <content src="index.html" />
        <access origin="*" />

      The problem is it seems that I cannot get the plugin cordova-plugin-device to work. When I install the APK on my Android phone, neither the "deviceready"-alert nor the "button"-alert executes. When I try to use navigator.notification.alert(device.name, alertCallback) (dialogs plugin) instead (and of course provide the callback function) it doesn't work either. I even tried to add a span element and change its html to device.cordova on button-click via jQuery - no effect.


      I also tried to pull the plugin from GitHub via <plugin spec="https://github.com/apache/cordova-plugin-device.git" /> instead from npm. However, this also didn't work. Strange to say, the 3rd party toast plugin works just fine.


      I've run out of ideas by now and really hope you can help me out.


      Best regards,


        • 1. Re: Cannot get cordova-plugin-device to work
          kerrishotts Adobe Employee

          Try moving your inline script to the body or add the "defer" attribute so that it waits until everything else has finished loading. You might also try moving your script to a separate .js file (like index.js).


          Also, have you tried debugging the app using either Chrome (Android)? There might be some error messages appearing in the console that might be of use. Also check "adb logcat" to see if your device is logging messages that might be of use.

          • 2. Re: Cannot get cordova-plugin-device to work
            Ro4cHii Level 1

            I moved it to the body, it didn't change anything.


            Debugging was the right choice though: the console says "Uncaught ReferenceError: device is not defined".

            When I take a look into the plugins folder, there is no folder for cordova-plugin-device. However, when I download the APK and open it with 7-Zip, it's there (SampleApp-debug.apk\assets\www\plugins\cordova-plugin-device\www\device.js).

            • 3. Re: Cannot get cordova-plugin-device to work
              Ro4cHii Level 1

              I kinda solved the problem. Well, I didn't really solve it, but it's not occurring anymore.


              I deleted the app on https://build.phonegap.com/apps and reuploaded it. I don't know what did the trick, but now it's working fine!


              Thanks a lot for your help - remote debugging using Chrome is awesome!