Get device info
Mobile app user is increased day by day. Mobile application is increased. Developer are using a hybrid framework to develop a mobile application. Ionic is also one kind of hybrid framework for creating a mobile application. Ionic have so many plugin to develop an app very easily and quickly.
In this post, I am going to show you how to get device info using ionic 2 native device plugin. If you want to get device id or other info in then follow the step.
Generate a new project
First, generate a new project by the following command in terminal or command prompt.
ionic start my_project blank
Install the required plugin
Then install Cordova device plugin using terminal or command prompt using this command one by one.
$ ionic plugin add cordova-plugin-device $ npm install --save @ionic-native/device
Then import the plugin in your js file where your controller will be declared.
import { Device } from '@ionic-native/device';
Then add the Device in provider in your app module js.
import { BrowserModule } from '@angular/platform-browser'; import { ErrorHandler, NgModule } from '@angular/core'; import { IonicApp, IonicModule } from 'ionic-angular'; import { SplashScreen } from '@ionic-native/splash-screen'; import { StatusBar } from '@ionic-native/status-bar'; import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { Device } from '@ionic-native/device'; @NgModule({ declarations: [ MyApp, HomePage ], imports: [ BrowserModule, IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [ MyApp, HomePage ], providers: [ Device , StatusBar, SplashScreen, {provide: ErrorHandler, useClass: IonicErrorHandler} ] }) export class AppModule {}
By using the native Device plugin, you can easily retrieve the basic device information.
Here is the list of information.
1. cordova – The version of Cordova running on the device.
2. model – The device.model.
3. platform – The device’s operating system.
4. uuid – The device’s Universally Unique Identifier.
5. version – The operating system version.
6. manufacturer – The device’s manufacturer.
7. isVirtual – Whether the device is running on a simulator.
8. serial – The device hardware serial number.
Then use the code to get the device unique id and other information of the device.
device_id:string=""; constructor(private device: Device) { this.device_id= this.device.uuid; console.log("Your device id- "+this.device_id); }