HarmonyOS NEXT project practice:get device info - Velvet-Siren/harmonyos GitHub Wiki

[##HarmonyOS Next project practice##HarmonyOS SDK application service##education##]

Core concept

@ohos.deviceInfo (Equipment information) This module provides terminal device information query, which cannot be configured by developers. Note: This module interface returns device constant information. It is recommended that the application only calls it once and does not need to call it frequently.

Import module:

import { deviceInfo } from '@kit.BasicServicesKit';

Practical code

import { deviceInfo } from '@kit.BasicServicesKit';

@Entry
@Component
struct DeviceInfoPage {
  @State deviceType: ResourceStr = '';
  @State manufacture: ResourceStr = '';
  @State brand: ResourceStr = '';
  @State marketName: ResourceStr = '';
  @State productSeries: ResourceStr = '';
  @State productModel: ResourceStr = '';
  @State productModelAlias: ResourceStr = '';
  @State odid: ResourceStr = '';
  @State sdkApiVersion: number = 0;

  build() {
    Column({ space: 6 }) {
      Text('DeviceInfoPage')
        .fontSize(24)
        .fontWeight(FontWeight.Bold)
      Button('getDeviceInfo')
        .onClick(() => {
          this.getDeviceInfo()
        })

      Text('设备类型:' + this.deviceType)
      Text('厂家名称:' + this.manufacture)
      Text('品牌名称:' + this.brand)
      Text('外部产品系列:' + this.marketName)
      Text('产品型号:' + this.productModel)
      Text('odid:' + this.odid)
      Text('sdkApiVersion:' + this.sdkApiVersion)
    }
    .height('100%')
    .width('100%')
  }

  getDeviceInfo() {
    //设备类型,示例:phone
    this.deviceType = deviceInfo.deviceType;
    //厂家名称,示例:HUAWEI
    this.manufacture = deviceInfo.manufacture;
    //品牌名称,示例:HUAWEI
    this.brand = deviceInfo.brand;
    //外部产品系列,示例:HUAWEI Mate 60
    this.marketName = deviceInfo.marketName;
    //产品系列,示例:ALN
    this.productSeries = deviceInfo.productSeries;
    //认证型号,示例:TAS-AL00
    this.productModel = deviceInfo.productModel;
    //认证型号别名,示例:TAS-AL00
    this.productModelAlias = deviceInfo.productModelAlias;
    //内部软件子型号,示例:ALN-AL00
    let softwareModelInfo: string = deviceInfo.softwareModel;
    //硬件版本号。示例:HL1CMSM
    let hardwareModelInfo: string = deviceInfo.hardwareModel;
    //设备序列号SN(Serial Number)。
    let serialInfo: string = deviceInfo.serial;
    //Bootloader版本号。示例:bootloader
    let bootloaderVersionInfo: string = deviceInfo.bootloaderVersion;
    //应用二进制接口(Abi)。示例:arm64-v8a
    let abiListInfo: string = deviceInfo.abiList;

    let securityPatchTagInfo: string = deviceInfo.securityPatchTag;
    // 输出结果:the value of the securityPatchTag is :2021/01/01
    console.info('the value of the deviceInfo securityPatchTag is :' + securityPatchTagInfo);

    //产品版本。示例:ALN-AL00 5.0.0.1(XXX)
    let displayVersionInfo: string = deviceInfo.displayVersion;
    // 输出结果:the value of the displayVersion is :XXX X.X.X.X
    console.info('the value of the deviceInfo displayVersion is :' + displayVersionInfo);

    let incrementalVersionInfo: string = deviceInfo.incrementalVersion;
    // 输出结果:the value of the incrementalVersion is :default
    console.info('the value of the deviceInfo incrementalVersion is :' + incrementalVersionInfo);

    let osReleaseTypeInfo: string = deviceInfo.osReleaseType;
    // 输出结果:the value of the osReleaseType is :Release
    console.info('the value of the deviceInfo osReleaseType is :' + osReleaseTypeInfo);
    //系统版本,版本格式OpenHarmony-x.x.x.x,x为数值。
    let osFullNameInfo: string = deviceInfo.osFullName;
    // 输出结果:the value of the osFullName is :OpenHarmony-5.0.0.1
    console.info('the value of the deviceInfo osFullName is :' + osFullNameInfo);

    let majorVersionInfo: number = deviceInfo.majorVersion;
    // 输出结果:the value of the majorVersion is :5
    console.info('the value of the deviceInfo majorVersion is :' + majorVersionInfo);

    let seniorVersionInfo: number = deviceInfo.seniorVersion;
    // 输出结果:the value of the seniorVersion is :0
    console.info('the value of the deviceInfo seniorVersion is :' + seniorVersionInfo);

    let featureVersionInfo: number = deviceInfo.featureVersion;
    // 输出结果:the value of the featureVersion is :0
    console.info('the value of the deviceInfo featureVersion is :' + featureVersionInfo);

    let buildVersionInfo: number = deviceInfo.buildVersion;
    // 输出结果:the value of the buildVersion is :1
    console.info('the value of the deviceInfo buildVersion is :' + buildVersionInfo);

    //系统软件API版本。示例:12
    this.sdkApiVersion = deviceInfo.sdkApiVersion;

    let firstApiVersionInfo: number = deviceInfo.firstApiVersion;
    // 输出结果:the value of the firstApiVersion is :3
    console.info('the value of the deviceInfo firstApiVersion is :' + firstApiVersionInfo);

    let versionIdInfo: string = deviceInfo.versionId;
    // 输出结果:the value of the versionId is :wearable/HUAWEI/HUAWEI/TAS/OpenHarmony-5.0.0.1/TAS-AL00/TAS-AL00/12/default/release:nolog
    console.info('the value of the deviceInfo versionId is :' + versionIdInfo);

    let buildTypeInfo: string = deviceInfo.buildType;
    // 输出结果:the value of the buildType is :default
    console.info('the value of the deviceInfo buildType is :' + buildTypeInfo);

    let buildUserInfo: string = deviceInfo.buildUser;
    // 输出结果:the value of the buildUser is :default
    console.info('the value of the deviceInfo buildUser is :' + buildUserInfo);

    let buildHostInfo: string = deviceInfo.buildHost;
    // 输出结果:the value of the buildHost is :default
    console.info('the value of the deviceInfo buildHost is :' + buildHostInfo);

    let buildTimeInfo: string = deviceInfo.buildTime;
    // 输出结果:the value of the buildTime is :default
    console.info('the value of the deviceInfo buildTime is :' + buildTimeInfo);

    let buildRootHashInfo: string = deviceInfo.buildRootHash;
    // 输出结果:the value of the buildRootHash is :default
    console.info('the value of the deviceInfo buildRootHash is :' + buildRootHashInfo);

    let udid: string = deviceInfo.udid;
    // 输出结果:the value of the udid is :9D6AABD147XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXE5536412
    console.info('the value of the deviceInfo udid is :' + udid);

    let distributionOSName: string = deviceInfo.distributionOSName
    // 输出结果:the value of the distributionOSName is :OpenHarmony
    console.info('the value of the deviceInfo distributionOSName is :' + distributionOSName);

    let distributionOSVersion: string = deviceInfo.distributionOSVersion
    // 输出结果:the value of the distributionOSVersion is :5.0.0
    console.info('the value of the deviceInfo distributionOSVersion is :' + distributionOSVersion);

    let distributionOSApiVersion: number = deviceInfo.distributionOSApiVersion
    // 输出结果:the value of the distributionOSApiVersion is :500001
    console.info('the value of the deviceInfo distributionOSApiVersion is :' + distributionOSApiVersion);

    let distributionOSApiName: string = deviceInfo.distributionOSApiName
    console.info('the value of the deviceInfo distributionOSApiName is :' + distributionOSApiName);

    let distributionOSReleaseType: string = deviceInfo.distributionOSReleaseType
    // 输出结果:the value of the distributionOSReleaseType is :Release
    console.info('the value of the deviceInfo distributionOSReleaseType is :' + distributionOSReleaseType);

    //开发者匿名设备标识符。示例:1234a567-XXXX-XXXX-XXXX-XXXXXXXXXXXX
    this.odid = deviceInfo.ODID;

    //硬盘序列号。
    let diskSN: string = deviceInfo.diskSN;
    // 输出结果:the value of the deviceInfo diskSN is :2502EM400567
    console.info('the value of the deviceInfo diskSN is :' + diskSN);
  }
}