I am new to ionic and cordova... I am trying to read the device UUID but its returning empty dictionary

I am using the following code to get the device details inside a controller. In ionic framework.

    angular.module('starter.controllers', [])
    .controller('AppCtrl', function($scope, $ionicModal, $timeout, $ionicPlatform) {
      // Form data for the login modal
      $scope.loginData = {};
      // Create the login modal that we will use later
      $ionicModal.fromTemplateUrl('templates/login.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.modal = modal;
     $scope.device = ionic.Platform.device();
        $scope.uuid = $scope.device.uuid;
        // localStorage.setItem("uuid", uuid);

  ionic.Platform.ready(function() {
var device = ionic.Platform.device();

But ionic.Platform.device() is returning empty dictionary.

Even on the emulator its coming empty.

If is there another ways to read the Device UUID show me the direction to it.

Install cordova device plugin and setup ngCordova and inject $cordovaDevice to your controller and do this:

var foo = $cordovaDevice.getUUID();
1 Like

Can you please tell me the detail step to do it.

Read about ngCordova. Learn for yourself. It might take an hour at max.

Hey i did it and finally getting this:

installed cordova device plugin by :

sudo cordova plugin add org.apache.cordova.device

then downloaded ngCordova and included ng-cordova.min.js in to js folder and also included in index.html

next what i did is injected ngCordova as follows

angular.module('starter', ['ionic', 'starter.controllers','ngCordova'])

then included in controller as follows

angular.module('starter.controllers', [])

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $ionicPlatform,$cordovaDevice) 

but still getting the following errors

ReferenceError: device is not defined
at Object.getUUID (http://localhost:8100/js/ng-cordova.min.js:1:14929)
at new <anonymous> (http://localhost:8100/js/controllers.js:27:26)
at invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11591:17)
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11602:23)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:14906:28
at updateView (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42986:30)
at eventHook (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42933:17)
at Scope.$broadcast (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20605:28)
at $state.transition.resolved.then.$state.transition (http://localhost:8100/lib/ionic/js/ionic.bundle.js:34122:22)
at wrappedCallback (http://localhost:8100/lib/ionic/js/ionic.bundle.js:19197:81)

Can you now tell me what went wrong?

wow found out what wrong i was doing … through this question. Problem to use ngCordova : "device is not defined"

Happy you did!!! :slight_smile:

Just to add, the reason for this is that both cordova and its plugin will be unavailable when you are testing it out on ur web browser. to test the things on browser I use a small utility code

//mode code here give a hard coded value, 

the above code can be used to simulate the responses that you expect to get from the actual device/emulator

1 Like