I’m a newbie here.
I’m trying to understand how to use Capacitor’s Device API. There is a method getInfo(). When using it, I don’t get the appVersion or appBuild returned.
Using a blank starter generated by ionic start test2 blank --type=angular --capacitor
home.page.ts:
import { Component } from '@angular/core';
import { Device } from '@capacitor/core';
then:
async getDeviceInfo() {
const info = await Device.getInfo();
this.deviceAppVersion = info.appVersion;
this.deviceAppBuild = info.appBuild;
this.deviceOsVersion = info.osVersion;
this.devicePlatform = info.platform;
this.deviceManufacturer = info.manufacturer;
this.deviceModel = info.model;
console.log(info);
}
This is the result of the console.log(info) line from ionic serve:
Object { model: “Macintosh”, platform: “web”, appVersion: “”, appBuild: “”, osVersion: “Intel Mac OS X 10.15”, manufacturer: “”, isVirtual: false, batteryLevel: undefined, isCharging: undefined, uuid: “e40187f0-6294-4232-a144-a5231f3afe8b” }
The info.appBuild and info.appVersion items are always empty. The other fields get filled out OK. This doesn’t matter whether I am running using ionic serve, or in Android Studio. How do I get this to report the appVersion and appBuild?
English is not my native language, so please excuse grammatical errors. Thank you.
I’m not certain what you mean by “in Android Studio”, but those fields only get populated when you build an actual .ipa or .apk for deployment in iOS or Android.
OK. I see. by “in Android Studio” I meant running in AVD emulator or in a USB attached device.
So i did not generate an APK yet.
Thank you.
OK, I spoke too quickly. I generated an APK which is showing an output.json with the following content:
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":5,"versionName":"6.7.8","enabled":true,"outputFile":"app-debug.apk","fullName":"debug","baseName":"debug"},"path":"app-debug.apk","properties":{}}]
When installing this on a phone. Then running it. Still nothing is output for the info.appVersion or info.appBuild. I expected appBuild would be 5 and appVersion would be “6.7.8”
Thank you @rapropos. After looking into the capacitor source, I find the appVersion and appBuild will always be blank string (case 4:). This also explain the platform always = ‘web’. Looks like capacitor is work in progress. It does work with the cordova plugin.
DevicePluginWeb.prototype.getInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var ua, uaFields, battery, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ua = navigator.userAgent;
uaFields = this.parseUa(ua);
battery = {};
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, navigator.getBattery()];
case 2:
battery = _a.sent();
return [3 /*break*/, 4];
case 3:
e_1 = _a.sent();
return [3 /*break*/, 4];
case 4: return [2 /*return*/, Promise.resolve({
model: uaFields.model,
platform: 'web',
appVersion: '',
appBuild: '',
osVersion: uaFields.osVersion,
manufacturer: navigator.vendor,
isVirtual: false,
batteryLevel: battery.level,
isCharging: battery.charging,
uuid: this.getUid()
})];
}
});
});
};