How to get information about uuid and device model in ion based app?
I have this piece of code to find out / get information about a device.
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();
});
according to the documentation given here .
ionic.Platform.device();
will return the object that is returned to it by the cord. So, in a cordova object, the object should contain information about uuid, version, model, etc. Documentation Here
So i tried to get uuid and model from object deviceInformation
and then created and saw app on mobile, after which it shows me undefined
in message.
I showed it like this:
$scope.getDeviceInfo = function() {
alert($scope.deviceInformation.uuid);
}
How can I get the details of the object that set my cordova back to ionic?
source to share
I had the same problem. I got what you need after a long search -> try-> implement-> erase-> new try loop. Although I followed ng-cordova plugins, I found out that adding ng-cordova.js does not make it any easier to solve the bcoz plugin problem, it just contains the initialization of the plugins that ng-cordova supports on their website. I followed the following steps: Here on the site of the native cordova
Note that ng-cordova.js internally calls od native cordova methods and APIs, so it is very important to install cordova plugins separately even after ng-cordova.js is installed. Then I initialized the device to app.module
:
$ionicPlatform.ready(function() {
$scope.deviceInformation = ionic.Platform.device();
});
and I called the method in my inline controller:
$scope.getDeviceInfo = function() {
alert($scope.deviceInformation.uuid);
}
This gave me everything I needed ....
You have assigned a value to a Javascript variable named deviceInformation
. This way you won't get access to AngularJs $scope.deviceInformation
.
Change 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 can also use alert(deviceInformation.uuid);
, but you have to stick $scope
with AngularJS because it allows easy HTML binding with expressions.
source to share
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);
}
}
source to share