DEV Community

桃花镇童长老
桃花镇童长老

Posted on

DeviceUtil, device-related tool class

Harmony-utils

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


getDeviceId Get the device ID (it remains unchanged after uninstalling the APP, and permissions are required: ohos.permission.STORE_PERSISTENT_DATA)
  let id = DeviceUtil.getDeviceId();
  DialogHelper.showToast(`当前设备号为:${id}`);
Enter fullscreen mode Exit fullscreen mode
deleteDeviceId Remove the device ID
  DeviceUtil.deleteDeviceId();
  ToastUtil.showToast("移除设备ID成功!")
Enter fullscreen mode Exit fullscreen mode
getODID Get the developer's anonymous device identifier, ODID
 let ODID = DeviceUtil.getODID();
 DialogHelper.showToast(`开发者匿名设备标识符为:${ODID}`);
Enter fullscreen mode Exit fullscreen mode
getOAID Get the open anonymous device identifier, OAID
PermissionUtil.requestPermissionsEasy("ohos.permission.APP_TRACKING_CONSENT").then(async (grant) => {
  if (grant) {
    DeviceUtil.getOAID().then((id) => {
      ToastUtil.showToast("开放匿名设备标识符位:" + id);
    }).catch((err: BusinessError) => {
      ToastUtil.showToast(`开放匿名设备标识符获取失败,${err.message}!`);
    });
  } else {
    ToastUtil.showToast("您拒绝了权限!")
  }
});
Enter fullscreen mode Exit fullscreen mode
getAAID Get the application anonymous identifier, AAID
  let AAID = await DeviceUtil.getAAID();
  DialogHelper.showToast(`AAID为:${AAID}`);
Enter fullscreen mode Exit fullscreen mode
deleteAAID Delete the app anonymous identifier, AAID
 DeviceUtil.deleteAAID();     
Enter fullscreen mode Exit fullscreen mode
getSerial Gets the device serial number. Description: Can be used as a unique identification code for the device. Example: The serial number varies with the device. Permission required: ohos.permission.sec.ACCESS_UDID (currently only for system applications and is not open to three-party applications)
 let str = DeviceUtil.getSerial();
Enter fullscreen mode Exit fullscreen mode
getUdid Get the device Udid. Permission required: ohos.permission.sec.ACCESS_UDID (currently only for system applications and is not open to three-party applications)
 let str = DeviceUtil.getUdid();
Enter fullscreen mode Exit fullscreen mode
getBrand Get the device brand name. Example: HUAWEI
 let str = DeviceUtil.getBrand();
Enter fullscreen mode Exit fullscreen mode
getProductModel Get certified model. Example: ALN-AL00
 let str = DeviceUtil.getProductModel();
Enter fullscreen mode Exit fullscreen mode
getBrandModel Get the device brand name Certified model. Example: HUAWEI ALN-AL00
 let str = DeviceUtil.getBrandModel();
Enter fullscreen mode Exit fullscreen mode
getMarketName Gets the external product series name. Example: HUAWEI Mate 60 Pro
 let str = DeviceUtil.getMarketName();
Enter fullscreen mode Exit fullscreen mode
getHardwareModel Gets the hardware version number. Example: HL1CMSM
 let str = DeviceUtil.getHardwareModel();
Enter fullscreen mode Exit fullscreen mode
getManufacture Gets the name of the device manufacturer. Example: HUAWEI
 let str = DeviceUtil.getManufacture();
Enter fullscreen mode Exit fullscreen mode
getOsFullName Get the system version
 let str = DeviceUtil.getOsFullName();
Enter fullscreen mode Exit fullscreen mode
getDisplayVersion Gets the product version. Example: ALN-AL00 5.0.0.1(XXX)
 let str = DeviceUtil.getDisplayVersion();
Enter fullscreen mode Exit fullscreen mode
getBuildVersion Gets the Build version number, identifying the version number of the compiled build
 let str = DeviceUtil.getBuildVersion();
Enter fullscreen mode Exit fullscreen mode
getSdkApiVersion Gets the system software API version. Example: 12
 let str = DeviceUtil.getSdkApiVersion();
Enter fullscreen mode Exit fullscreen mode
getOsVersion Gets the OS version number (Major version number, example: 5; Senior version number, example: 0; Feature version number, example: 0)
 let str = DeviceUtil.getOsVersion();
Enter fullscreen mode Exit fullscreen mode
getAbiList Application binary interface (Abi). Example: arm64-v8a
 let str = DeviceUtil.getAbiList();
Enter fullscreen mode Exit fullscreen mode
getOsReleaseType Gets the system's publishing type, with values: Canary, Beta, Release
 let str = DeviceUtil.getOsReleaseType();
Enter fullscreen mode Exit fullscreen mode
getDeviceType Gets the current device type
 let str = DeviceUtil.getDeviceType();
Enter fullscreen mode Exit fullscreen mode
getDeviceTypeStr Gets the current device type and returns the string
 let str = DeviceUtil.getDeviceTypeStr();
Enter fullscreen mode Exit fullscreen mode
getConfiguration Gets the configuration of the device
  let configuration = DeviceUtil.getConfigurationSync();
  let jsonStr = JSON.stringify(configuration, null, 2);
Enter fullscreen mode Exit fullscreen mode
getDirection Gets the current device screen direction
  let direction = DeviceUtil.getDirection();
Enter fullscreen mode Exit fullscreen mode
getDeviceCapability Get the DeviceCapability of the device
  let deviceCapability = DeviceUtil.getDeviceCapabilitySync();
  let jsonStr = JSON.stringify(deviceCapability, null, 2);
Enter fullscreen mode Exit fullscreen mode
getScreenDensity Gets the current device screen density
  let density = DeviceUtil.getScreenDensity();
  ToastUtil.showToast(`前设备屏幕密度: ${density}`)
Enter fullscreen mode Exit fullscreen mode
getBatterySOC Gets the percentage of battery remaining in the current device
  let result = DeviceUtil.getBatterySOC();
Enter fullscreen mode Exit fullscreen mode
getBatteryCapacityLevel Gets the current device battery level
  let result = DeviceUtil.getBatteryCapacityLevel();
Enter fullscreen mode Exit fullscreen mode
getHealthStatus Get the current device battery health status
  let result = DeviceUtil.getHealthStatus();
Enter fullscreen mode Exit fullscreen mode
getBatteryTemperature Gets the temperature of the current device battery in 0.1 degrees Celsius
  let result = DeviceUtil.getBatteryTemperature();
Enter fullscreen mode Exit fullscreen mode
getVoltage Gets the voltage of the current device battery, unit to microvoltage
  let result = DeviceUtil.getVoltage();
Enter fullscreen mode Exit fullscreen mode
getNowCurrent Get the current of the current device battery, unit mAh
  let result = DeviceUtil.getNowCurrent();
Enter fullscreen mode Exit fullscreen mode
isActive Detects whether the current device is active. Devices with screens are in a light screen state, and devices without screens are in a non-sleep state
  let result = DeviceUtil.isActive();
Enter fullscreen mode Exit fullscreen mode
isStandby Detects whether the current device enters standby low power battery life mode
  let result = DeviceUtil.isStandby();
Enter fullscreen mode Exit fullscreen mode
getPowerMode Gets the power mode of the current device
  let result = DeviceUtil.getPowerMode();
Enter fullscreen mode Exit fullscreen mode
startVibration Turn on device vibration
  DeviceUtil.getNowCurrent();
Enter fullscreen mode Exit fullscreen mode
stopVibration stops the device vibration (according to VIBRATOR_STOP_MODE_TIME mode)
  DeviceUtil.stopVibration();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)