No matter what I do or try I can't get info about the device. I'm using last version of ionic 4.
Since all I find online is basically
Of course that's not what's going on. I'm not doing that, I have cordova available, etc.
I'm testing on device, both android and ios. I deal with this in a service. I'm calling the functions with buttons so everything is more than loeaded, everything is ready. This is the code I'm trying to make work:
import { Injectable } from '@angular/core';
import { Device } from '@ionic-native/device/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class InitialService {
eldevice: any = '';
constructor(
private device: Device,
private platform: Platform
) {}
async setup() {
if (this.platform.is('cordova')) { // es movil
this.eldevice = await this.storage.get('device');
if (this.eldevice == null) { // nuevo device
this.eldevice.platform = this.device.platform;
this.eldevice.version = this.device.version;
this.eldevice.uuid = this.device.uuid;
this.eldevice.manufacturer = this.device.manufacturer;
console.log('datos sin await', this.eldevice);
this.eldevice = await this.storage.set('device', this.device);
} else { // device conocido
console.log('datos guardados', this.eldevice);
}
} else { // es virtual
console.log('virtual');
}
}
clearData() {
this.storage.clear();
this.eldevice = null;
}
}
Updated:
Any cordova plugins require you to await for platform to be in "ready" state. So before calling / using plugins you need to call platform.ready() method and call specific plugin functions in the callback, or after "await".
Also looking at your setup() method you seem like attempting to access properties of an object that gets 'null' assigned to it after storage check. See comments in the code below:
import { Injectable } from '@angular/core';
import { Device } from '@ionic-native/device/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class InitialService {
eldevice: any = '';
constructor(
private device: Device,
private platform: Platform
) {}
async setup() {
// await for platform ready:
await this.platform.ready();
if (this.platform.is('cordova')) { // es movil
this.eldevice = await this.storage.get('device');
if (this.eldevice == null) { // nuevo device
// here below 'eldevice' is null (as confirmed by the check above) and you are attempting to assign something to non existing properties (platform, version etc).
// to fix you need actual object to be assigned to eldevice:
this.eldevice = {
platform: this.device.platform,
version: this.device.version,
uuid: this.device.uuid,
manufacturer: this.device.manufacturer
}
//this.eldevice.platform = this.device.platform;
//this.eldevice.version = this.device.version;
//this.eldevice.uuid = this.device.uuid;
//this.eldevice.manufacturer = this.device.manufacturer;
console.log('datos sin await', this.eldevice);
this.eldevice = await this.storage.set('device', this.device);
} else { // device conocido
console.log('datos guardados', this.eldevice);
}
} else { // es virtual
console.log('virtual');
}
}
clearData() {
this.storage.clear();
this.eldevice = null;
}
}