Shaggie
Shaggie

Reputation: 1829

How to get the device's uuid and model information in ionic based App?

I have this piece of code in order to recognize/get the device information.

ionic.Platform.ready(function() {
    // will execute when device is ready, or immediately if the device is already ready.
    $scope.deviceInformation = ionic.Platform.device();
    $scope.currentPlatform = ionic.Platform.platform();
    $scope.currentPlatformVersion = ionic.Platform.version();
});

as per the documentation given Here. The ionic.Platform.device(); will return object that is returned to it by cordova. So in the object of cordova the object should contain information of uuid, version, model etc. The documentation is Here

So i tried to get the uuid and model from the object deviceInformation and built and seen the app in mobile, then it shows me undefined in the alert.

I have shown it like this:

$scope.getDeviceInfo = function() {
    alert($scope.deviceInformation.uuid);
}

How can i get the details of object returned my cordova to ionic??

Upvotes: 3

Views: 11285

Answers (4)

Abdul Rashid
Abdul Rashid

Reputation: 183

The solution is simple

import { BrowserModule } from '@angular/platform-browser';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { Device } from 'ionic-native';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';

@NgModule({
            declarations: [
    ],
    imports: [
    ],
    bootstrap: [IonicApp],
    entryComponents: [
    ],
    providers: [
        StatusBar,
        SplashScreen,
        Device,
        {provide: ErrorHandler, useClass: IonicErrorHandler}
    ]
})
export class AppModule {}

import { Component } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Device } from 'ionic-native';

import { HomePage } from '../pages/home/home';

@Component({
            templateUrl: 'app.html'
})

export class MyApp{
>
    rootPage:any = HomePage;
    deviceName: string = '';
>
    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {

            platform.ready().then(() ={
                        // Okay, so the platform is ready and our plugins are available.
                        // Here you can do any higher level native things you might need.
                        statusBar.styleDefault();
                        splashScreen.hide();

                    });

            document.addEventListener("deviceready", this.onDeviceReady, false);
        }

    onDeviceReady() {
            console.log(Device.uuid);
            console.log(Device.manufacturer);
        }
}

Upvotes: 0

Mohsin Bagwan
Mohsin Bagwan

Reputation: 343

I got this by simply using following code in device ready function

    $ionicPlatform.ready(function() { 
   var deviceInformation = ionic.Platform.device();
   console.log('platform: ' +  deviceInformation.platform);
   console.log('udid: ' + deviceInformation.uuid);
   });


Upvotes: 0

user4741577
user4741577

Reputation:

I was suffering with the same problem. I got what you need after long search->try->implement->erase->new try cycle. Though i followed ng-cordova plugins, i came to know that adding ng-cordova.js will not make it easy to resolve the plugin problem bcoz it just contain the initialization of plugins that are supported by the ng-cordova on its website. I have followed the steps given Here on native cordova site

Point to note here is ng-cordova.js internally calls the methods and API's od native cordova so it is very important to install the cordova plugins separately even after you install the ng-cordova.js. Then i initialized the device in app.module:

$ionicPlatform.ready(function() {
  $scope.deviceInformation = ionic.Platform.device();
});

and i called the method in my intro controller:

$scope.getDeviceInfo = function() {
  alert($scope.deviceInformation.uuid);
}

That gave me all that i need....

Upvotes: 4

Keval
Keval

Reputation: 3389

You have assigned the value to a Javascript variable named deviceInformation. So you won't get access to AngularJs' $scope.deviceInformation.

Change your var to $scope:

ionic.Platform.ready(function() {
    // will execute when device is ready, or immediately if the device is already ready.
    $scope.deviceInformation = ionic.Platform.device();
    $scope.currentPlatform = ionic.Platform.platform();
    $scope.currentPlatformVersion = ionic.Platform.version();
    $scope.getDeviceInfo = function() {
        alert($scope.deviceInformation.uuid);
    }
});

You could also use alert(deviceInformation.uuid); but you should stick to $scope in AngularJS, because it allows simple binding with the HTML using expressions.

Upvotes: 0

Related Questions