HarmonyOS开发:设备管理使用详解

目录

前言

设备管理概述

设备管理组成

1、电量信息

(1)导入模块

(2)属性信息

(3)常用属性

(4)使用示例

2、设备信息

(1)导入模块

(2)属性信息

(3)使用示例

3、系统电源信息

(1)导入模块

(2)属性信息

(3)常用属性

a.power.isActive(9+)

b.power.rebootDevice(deprecated)

c.power.getPowerMode(9+)

d.power.isStandby(10+)

e.DevicePowerMode(9+)

(4)使用示例

4、Runninglock锁

(1)导入模块

(2)属性信息

(3)常用属性

a. runningLock.isSupported(9+)

b. runningLock.create(9+)

c.runningLock.create(9+)

d.RunningLock

e.RunningLockType

(4)使用示例

5、热管理

(1)导入模块

(2)属性信息

(3)常用属性

a.thermal.registerThermalLevelCallback(9+)

b.thermal.unregisterThermalLevelCallback(9+)

c.thermal.getLevel(9+)

d.ThermalLevel

(4)使用示例

6、USB管理

(1)导入模块

(2)属性信息

(3)常用属性

a.usbManager.getDevices

b.usbManager.connectDevice

c.usbManager.hasRight

d.usbManager.requestRight

e.usbManager.removeRight

f.usbManager.releaseInterface

g.USBEndpoint

h.USBDevice

i.USBAccessory(14+)

(4)使用示例

结束语


前言

在当今科技发展迅速的时刻,智能设备的种类和数量不断增加,从智能手机、平板电脑到智能家居设备、智能穿戴设备等,设备之间的互联互通成为了重要的发展趋势。HarmonyOS作为面向全场景智能设备的操作系统,其核心理念之一就是实现设备之间的无缝协同和高效管理。设备管理作为HarmonyOS开发中的关键环节,不仅涉及到设备的发现、连接和控制,还涉及到设备之间的数据同步和资源共享,其重要性不言而喻。设备管理能够帮助开发者更好地理解和利用HarmonyOS的分布式特性,为用户提供更加流畅和便捷的跨设备体验,通过设备管理,开发者可以实现设备之间的无缝切换、数据同步和资源共享,从而提升应用的实用性和用户满意度。那么本文就来深入介绍HarmonyOS中设备管理的使用方法,从基础概念到高级应用,从设备发现与连接到数据同步与资源共享,全方位剖析设备管理的特性和应用场景,通过丰富的示例代码和详细的解释说明,帮助大家快速掌握设备管理的使用要点,提升开发效率和应用质量。

设备管理概述

在HarmonyOS中,设备管理是基础功能中的基础服务,它是ArkTS的常用API之一。虽然说设备管理比较基础但是比较重要,包括电量信息、设备信息、系统电源信息、Runninglock锁、热管理、USB管理等模块。

设备管理组成

上面介绍了HarmonyOS中设备管理的主要组成模块,那么接下来就来详细介绍一下对应的组成模块。

1、电量信息

关于电量信息,即@ohos.batteryInfo,该模块主要提供电池状态和充放电状态的查询接口。

(1)导入模块

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

(2)属性信息

主要是描述电池信息。系统能力:SystemCapability.PowerManager.BatteryManager.Core,具体如下所示:

名称

类型

可读

可写

说明

batterySOC

number

表示当前设备剩余电池电量百分比。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

chargingStatus

BatteryChargeState

表示当前设备电池的充电状态。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

healthStatus

BatteryHealthState

表示当前设备电池的健康状态。

pluggedType

BatteryPluggedType

表示当前设备连接的充电器类型。

voltage

number

表示当前设备电池的电压,单位微伏。

technology

string

表示当前设备电池的技术型号。

batteryTemperature

number

表示当前设备电池的温度,单位0.1摄氏度。

isBatteryPresent

7+

boolean

表示当前设备是否支持电池或者电池是否在位。true表示支持电池或电池在位,false表示不支持电池或电池不在位,默认为false。

batteryCapacityLevel

9+

BatteryCapacityLevel

表示当前设备电池电量的等级。

nowCurrent

12+

number

表示当前设备电池的电流,单位毫安。

(3)常用属性

  • BatteryPluggedType :表示连接的充电器类型的枚举。系统能力:SystemCapability.PowerManager.BatteryManager.Core
  • BatteryChargeState:表示电池充电状态的枚举。系统能力:SystemCapability.PowerManager.BatteryManager.Core
  • BatteryHealthState:表示电池健康状态的枚举。系统能力:SystemCapability.PowerManager.BatteryManager.Core
  • BatteryCapacityLevel(9+):表示电池电量等级的枚举。系统能力:SystemCapability.PowerManager.BatteryManager.Core
  • CommonEventBatteryChangedKey(9+):表示COMMON_EVENT_BATTERY_CHANGED通用事件附加信息的查询键。系统能力:SystemCapability.PowerManager.BatteryManager.Core

(4)使用示例

import {batteryInfo} from '@kit.BasicServicesKit';let batterySOCInfo: number = batteryInfo.batterySOC;
console.info("The batterySOCInfo is: " + batterySOCInfo);let chargingStatusInfo = batteryInfo.chargingStatus;
console.info("The chargingStatusInfo is: " + chargingStatusInfo);let healthStatusInfo = batteryInfo.healthStatus;
console.info("The healthStatusInfo is: " + healthStatusInfo);let pluggedTypeInfo = batteryInfo.pluggedType;
console.info("The pluggedTypeInfo is: " + pluggedTypeInfo);let voltageInfo: number = batteryInfo.voltage;
console.info("The voltageInfo is: " + voltageInfo);let technologyInfo: string = batteryInfo.technology;
console.info("The technologyInfo is: " + technologyInfo);let batteryTemperatureInfo: number = batteryInfo.batteryTemperature;
console.info("The batteryTemperatureInfo is: " + batteryTemperatureInfo);let isBatteryPresentInfo: boolean = batteryInfo.isBatteryPresent;
console.info("The isBatteryPresentInfo is: " + isBatteryPresentInfo);let batteryCapacityLevelInfo = batteryInfo.batteryCapacityLevel;
console.info("The batteryCapacityLevelInfo is: " + batteryCapacityLevelInfo);let nowCurrentInfo: number = batteryInfo.nowCurrent;
console.info("The nowCurrentInfo is: " + nowCurrentInfo);

2、设备信息

设备信息,即@ohos.deviceInfo ,本模块提供终端设备信息查询,开发者不可配置。

(1)导入模块

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

(2)属性信息

未特殊说明的字段,数据长度最大值为96字节;系统能力:SystemCapability.Startup.SystemInfo;权限:以下各项所需要的权限有所不同,如下所示:

名称

类型

可读

可写

说明

deviceType

string

设备类型。详细请参考deviceTypes标签。

manufacture

string

设备厂家名称。

brand

string

设备品牌名称。

marketName

string

外部产品系列。

productSeries

string

产品系列。

productModel

string

认证型号。

productModelAlias14+

string

认证型号别名。

softwareModel

string

内部软件子型号。

hardwareModel

string

硬件版本号。

hardwareProfile(deprecated)

string

硬件Profile。从API version 6 开始支持,从API version 9 开始废弃。

serial

string

设备序列号SN(Serial Number)。可作为设备唯一识别码。

需要权限:ohos.permission.sec.ACCESS_UDID(该权限只允许系统应用及企业定制应用申请)

bootloaderVersion

string

Bootloader版本号。

abiList

string

应用二进制接口(Abi)。

securityPatchTag

string

安全补丁级别。

displayVersion

string

产品版本。

incrementalVersion

string

差异版本号。

osReleaseType

string

系统的发布类型,取值为:

- Canary:面向特定开发者发布的早期预览版本,不承诺API稳定性。

- Beta:面向开发者公开发布的Beta版本,不承诺API稳定性。

- Release:面向开发者公开发布的正式版本,承诺API稳定性。

osFullName

string

系统版本,版本格式OpenHarmony-x.x.x.x,x为数值。

majorVersion

number

Major版本号,随主版本更新增加,值为osFullName中的第一位数值,建议直接使用deviceInfo.majorVersion获取,可提升效率,不建议开发者解析osFullName获取。

seniorVersion

number

Senior版本号,随局部架构、重大特性增加,值为osFullName中的第二位数值,建议直接使用deviceInfo.seniorVersion获取,可提升效率,不建议开发者自主解析osFullName获取。

featureVersion

number

Feature版本号,标识规划的新特性版本,值为osFullName中的第三位数值,建议直接使用deviceInfo.featureVersion获取,可提升效率,不建议开发者自主解析osFullName获取。

buildVersion

number

Build版本号,标识编译构建的版本号,值为osFullName中的第四位数值,建议直接使用deviceInfo.buildVersion获取,可提升效率,不建议开发者自主解析osFullName获取。

sdkApiVersion

number

系统软件API版本。

firstApiVersion

number

首个版本系统软件API版本。

versionId

string

版本ID。由deviceType、manufacture、brand、productSeries、osFullName、productModel、softwareModel、sdkApiVersion、incrementalVersion、buildType拼接组成。

buildType

string

构建类型。

buildUser

string

构建用户。

buildHost

string

构建主机。

buildTime

string

构建时间。

buildRootHash

string

构建版本Hash。

udid7+

string

设备Udid。数据长度为65字节。可作为设备唯一识别码。

需要权限:ohos.permission.sec.ACCESS_UDID(该权限只允许系统应用及企业定制应用申请)

distributionOSName10+

string

发行版系统名称。

distributionOSVersion10+

string

发行版系统版本号。格式为x.x.x,x是数字

distributionOSApiVersion10+

number

发行版系统api版本。

distributionOSApiName13+

string

发行版系统api版本名称。

distributionOSReleaseType10+

string

发行版系统类型。

ODID12+

string

开发者匿名设备标识符。

ODID值会在以下场景重新生成:

手机恢复出厂设置。同一设备上同一个开发者(developerId相同)的应用全部卸载后重新安装时。

ODID生成规则:

根据签名信息里developerId解析出的groupId生成,developerId规则为groupId.developerId,若无groupId则取整个developerId作为groupId。

同一设备上运行的同一个开发者(developerId相同)的应用,ODID相同。

同一个设备上不同开发者(developerId不同)的应用,ODID不同。

不同设备上同一个开发者(developerId相同)的应用,ODID不同。

不同设备上不同开发者(developerId不同)的应用,ODID不同。

说明:数据长度为37字节。

diskSN15+

string

硬盘序列号。该字段只能在2in1上设备进行查询,其他设备查询结果为空。

需要权限:ohos.permission.ACCESS_DISK_PHY_INFO

(3)使用示例

    import { deviceInfo } from '@kit.BasicServicesKit';let deviceTypeInfo: string = deviceInfo.deviceType;// 输出结果:the value of the deviceType is :wearableconsole.info('the value of the deviceType is :' + deviceTypeInfo);let manufactureInfo: string = deviceInfo.manufacture;// 输出结果:the value of the manufacture is :HUAWEIconsole.info('the value of the manufactureInfo is :' + manufactureInfo);let brandInfo: string = deviceInfo.brand;// 输出结果:the value of the brand is :HUAWEIconsole.info('the value of the device brand is :' + brandInfo);let marketNameInfo: string = deviceInfo.marketName;// 输出结果:the value of the marketName is :Mate XXconsole.info('the value of the deviceInfo marketName is :' + marketNameInfo);let productSeriesInfo: string = deviceInfo.productSeries;// 输出结果:the value of the productSeries is :TASconsole.info('the value of the deviceInfo productSeries is :' + productSeriesInfo);let productModelInfo: string = deviceInfo.productModel;// 输出结果:the value of the productModel is :TAS-AL00console.info('the value of the deviceInfo productModel is :' + productModelInfo);let productModelAliasInfo: string = deviceInfo.productModelAlias;console.info('the value of the deviceInfo productModelAlias is :' + productModelAliasInfo);let softwareModelInfo: string = deviceInfo.softwareModel;// 输出结果:the value of the softwareModel is :TAS-AL00console.info('the value of the deviceInfo softwareModel is :' + softwareModelInfo);let hardwareModelInfo: string = deviceInfo.hardwareModel;// 输出结果:the value of the hardwareModel is :TASA00CVN1console.info('the value of the deviceInfo hardwareModel is :' + hardwareModelInfo);let serialInfo: string = deviceInfo.serial;// 输出结果:the value of the serial is :序列号随设备差异console.info('the value of the deviceInfo serial is :' + serialInfo);let bootloaderVersionInfo: string = deviceInfo.bootloaderVersion;// 输出结果:the value of the bootloaderVersion is :bootloaderconsole.info('the value of the deviceInfo bootloaderVersion is :' + bootloaderVersionInfo);let abiListInfo: string = deviceInfo.abiList;// 输出结果:the value of the abiList is :arm64-v8aconsole.info('the value of the deviceInfo abiList is :' + abiListInfo);let securityPatchTagInfo: string = deviceInfo.securityPatchTag;// 输出结果:the value of the securityPatchTag is :2021/01/01console.info('the value of the deviceInfo securityPatchTag is :' + securityPatchTagInfo);let displayVersionInfo: string = deviceInfo.displayVersion;// 输出结果:the value of the displayVersion is :XXX X.X.X.Xconsole.info('the value of the deviceInfo displayVersion is :' + displayVersionInfo);let incrementalVersionInfo: string = deviceInfo.incrementalVersion;// 输出结果:the value of the incrementalVersion is :defaultconsole.info('the value of the deviceInfo incrementalVersion is :' + incrementalVersionInfo);let osReleaseTypeInfo: string = deviceInfo.osReleaseType;// 输出结果:the value of the osReleaseType is :Releaseconsole.info('the value of the deviceInfo osReleaseType is :' + osReleaseTypeInfo);let osFullNameInfo: string = deviceInfo.osFullName;// 输出结果:the value of the osFullName is :OpenHarmony-5.0.0.1console.info('the value of the deviceInfo osFullName is :' + osFullNameInfo);let majorVersionInfo: number = deviceInfo.majorVersion;// 输出结果:the value of the majorVersion is :5console.info('the value of the deviceInfo majorVersion is :' + majorVersionInfo);let seniorVersionInfo: number = deviceInfo.seniorVersion;// 输出结果:the value of the seniorVersion is :0console.info('the value of the deviceInfo seniorVersion is :' + seniorVersionInfo);let featureVersionInfo: number = deviceInfo.featureVersion;// 输出结果:the value of the featureVersion is :0console.info('the value of the deviceInfo featureVersion is :' + featureVersionInfo);let buildVersionInfo: number = deviceInfo.buildVersion;// 输出结果:the value of the buildVersion is :1console.info('the value of the deviceInfo buildVersion is :' + buildVersionInfo);let sdkApiVersionInfo: number = deviceInfo.sdkApiVersion;// 输出结果:the value of the sdkApiVersion is :12console.info('the value of the deviceInfo sdkApiVersion is :' + sdkApiVersionInfo);let firstApiVersionInfo: number = deviceInfo.firstApiVersion;// 输出结果:the value of the firstApiVersion is :3console.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:nologconsole.info('the value of the deviceInfo versionId is :' + versionIdInfo);let buildTypeInfo: string = deviceInfo.buildType;// 输出结果:the value of the buildType is :defaultconsole.info('the value of the deviceInfo buildType is :' + buildTypeInfo);let buildUserInfo: string = deviceInfo.buildUser;// 输出结果:the value of the buildUser is :defaultconsole.info('the value of the deviceInfo buildUser is :' + buildUserInfo);let buildHostInfo: string = deviceInfo.buildHost;// 输出结果:the value of the buildHost is :defaultconsole.info('the value of the deviceInfo buildHost is :' + buildHostInfo);let buildTimeInfo: string = deviceInfo.buildTime;// 输出结果:the value of the buildTime is :defaultconsole.info('the value of the deviceInfo buildTime is :' + buildTimeInfo);let buildRootHashInfo: string = deviceInfo.buildRootHash;// 输出结果:the value of the buildRootHash is :defaultconsole.info('the value of the deviceInfo buildRootHash is :' + buildRootHashInfo);let udid: string = deviceInfo.udid;// 输出结果:the value of the udid is :9D6AABD147XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXE5536412console.info('the value of the deviceInfo udid is :' + udid);let distributionOSName: string = deviceInfo.distributionOSName// 输出结果:the value of the distributionOSName is :OpenHarmonyconsole.info('the value of the deviceInfo distributionOSName is :' + distributionOSName);let distributionOSVersion: string = deviceInfo.distributionOSVersion// 输出结果:the value of the distributionOSVersion is :5.0.0console.info('the value of the deviceInfo distributionOSVersion is :' + distributionOSVersion);let distributionOSApiVersion: number = deviceInfo.distributionOSApiVersion// 输出结果:the value of the distributionOSApiVersion is :500001console.info('the value of the deviceInfo distributionOSApiVersion is :' + distributionOSApiVersion);let distributionOSApiName: string = deviceInfo.distributionOSApiName// 输出结果:the value of the distributionOSApiName is :5.0.0console.info('the value of the deviceInfo distributionOSApiName is :' + distributionOSApiName);let distributionOSReleaseType: string = deviceInfo.distributionOSReleaseType// 输出结果:the value of the distributionOSReleaseType is :Releaseconsole.info('the value of the deviceInfo distributionOSReleaseType is :' + distributionOSReleaseType);let odid: string = deviceInfo.ODID;// 输出结果:the value of the ODID is :1234a567-XXXX-XXXX-XXXX-XXXXXXXXXXXXconsole.info('the value of the deviceInfo odid is :' + odid);let diskSN: string = deviceInfo.diskSN;// 输出结果:the value of the deviceInfo diskSN is :2502EM400567console.info('the value of the deviceInfo diskSN is :' + diskSN);

3、系统电源信息

@ohos.power (系统电源管理),该模块主要提供重启、关机、查询屏幕状态等接口。开发者可以使用该模块的接口获取设备的活动状态、电源模式、亮灭屏状态等。

(1)导入模块

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

(2)属性信息

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

(3)常用属性

a.power.isActive(9+)

isActive(): boolean

检测当前设备是否处于活动状态。

  • 有屏的设备亮屏时为活动状态,熄屏时为非活动状态。
  • 无屏的设备非休眠时为活动状态,休眠时为非活动状态。

系统能力: SystemCapability.PowerManager.PowerManager.Core

b.power.rebootDevice(deprecated)

rebootDevice(reason: string): void

重启设备。

需要权限: ohos.permission.REBOOT,该权限仅系统应用可申请。

系统能力: SystemCapability.PowerManager.PowerManager.Core

c.power.getPowerMode(9+)

getPowerMode(): DevicePowerMode

获取当前设备的电源模式。

系统能力: SystemCapability.PowerManager.PowerManager.Core

d.power.isStandby(10+)

isStandby(): boolean

检测当前设备是否进入待机低功耗续航模式。

系统能力: SystemCapability.PowerManager.PowerManager.Core

e.DevicePowerMode(9+)

表示电源模式的枚举值。

系统能力: SystemCapability.PowerManager.PowerManager.Core

(4)使用示例

try {let isActive = power.isActive();console.info('power is active: ' + isActive);
} catch(err) {}

4、Runninglock锁

@ohos.runningLock (Runninglock锁),该模块主要提供RunningLock锁相关操作的接口,包括创建、查询、持锁、释放锁等操作。

(1)导入模块

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

(2)属性信息

该模块首批接口从API version 7开始支持,后续版本的新增接口,采用上角标单独标记接口的起始版本。

(3)常用属性

a. runningLock.isSupported(9+)

isSupported(type: RunningLockType): boolean

查询系统是否支持该类型的锁。

系统能力: SystemCapability.PowerManager.PowerManager.Core

b. runningLock.create(9+)

create(name: string, type: RunningLockType, callback: AsyncCallback): void

创建RunningLock锁。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

c.runningLock.create(9+)

create(name: string, type: RunningLockType): Promise

创建RunningLock锁。

系统能力: SystemCapability.PowerManager.PowerManager.Core

需要权限: ohos.permission.RUNNING_LOCK

d.RunningLock

阻止系统休眠的锁。

e.RunningLockType

RunningLock锁的类型。

系统能力: SystemCapability.PowerManager.PowerManager.Core

(4)使用示例

// RunningLockTest.ets
class RunningLockTest {public static recordLock: runningLock.RunningLock;public static holdRunningLock(): void {if (RunningLockTest.recordLock) {RunningLockTest.recordLock.hold(500);console.info('hold running lock success');} else {runningLock.create('running_lock_test', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL, (err: Error, lock: runningLock.RunningLock) => {if (typeof err === 'undefined') {console.info('create running lock: ' + lock);RunningLockTest.recordLock = lock;try {lock.hold(500);console.info('hold running lock success');} catch(err) {console.error('hold running lock failed, err: ' + err);}} else {console.error('create running lock failed, err: ' + err);}});}}
}

5、热管理

@ohos.thermal (热管理),该模块提供热管理相关的接口,包括热档位查询及注册回调等功能。

(1)导入模块

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

(2)属性信息

该模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

(3)常用属性

a.thermal.registerThermalLevelCallback(9+)

registerThermalLevelCallback(callback: Callback): void

订阅热档位变化时的回调提醒。

系统能力: SystemCapability.PowerManager.ThermalManager

b.thermal.unregisterThermalLevelCallback(9+)

unregisterThermalLevelCallback(callback?: Callback): void

取消订阅热档位变化时的回调提醒。

系统能力: SystemCapability.PowerManager.ThermalManager

c.thermal.getLevel(9+)

getLevel(): ThermalLevel

获取当前热档位信息。

系统能力: SystemCapability.PowerManager.ThermalManager

d.ThermalLevel

热档位信息。

系统能力: SystemCapability.PowerManager.ThermalManager

(4)使用示例

try {thermal.registerThermalLevelCallback((level: thermal.ThermalLevel) => {console.info('thermal level is: ' + level);});console.info('register thermal level callback success.');
} catch(err) {console.error('register thermal level callback failed, err: ' + err);
}

6、USB管理

@ohos.usbManager (USB管理),本模块主要提供管理USB设备的相关功能,包括主设备上查询USB设备列表、批量数据传输、控制命令传输、权限控制等;从设备上端口管理、功能切换及查询等。

(1)导入模块

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

(2)属性信息

该模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

(3)常用属性

a.usbManager.getDevices

getDevices(): Array>

获取接入主设备的USB设备列表。如果没有设备接入,那么将会返回一个空的列表。开发者模式关闭时,如果没有设备接入,接口可能返回undefined,注意需要对接口返回值做判空处理。

系统能力: SystemCapability.USB.USBManager

b.usbManager.connectDevice

connectDevice(device: USBDevice): Readonly

根据getDevices()返回的设备信息打开USB设备。如果USB服务异常,可能返回undefined,注意需要对接口返回值做判空处理。

  • 需要调用usbManager.getDevices
  • 调用usbManager.requestRight

特别说明:单次批量传输的传输数据总量(包括pipe、endpoint、buffer、timeout)请控制在200KB以下。

系统能力: SystemCapability.USB.USBManager

c.usbManager.hasRight

hasRight(deviceName: string): boolean

判断是否有权访问该设备。

如果“使用者”(如各种App或系统)有权访问设备则返回true;无权访问设备则返回false。

系统能力: SystemCapability.USB.USBManager

d.usbManager.requestRight

requestRight(deviceName: string): Promise

请求软件包的临时权限以访问设备。使用Promise异步回调。系统应用默认拥有访问设备权限,无需调用此接口申请。

系统能力: SystemCapability.USB.USBManager

e.usbManager.removeRight

removeRight(deviceName: string): boolean

移除软件包访问设备的权限。系统应用默认拥有访问设备权限,调用此接口不会产生影响。

系统能力: SystemCapability.USB.USBManager

f.usbManager.releaseInterface

releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number

释放注册过的通信接口。

需要调用usbManager.claimInterface先获取接口,才能使用此方法释放接口。

系统能力: SystemCapability.USB.USBManager

g.USBEndpoint

通过USB发送和接收数据的端口。通过USBInterface获取。

系统能力: SystemCapability.USB.USBManager

h.USBDevice

USB设备信息。

系统能力: SystemCapability.USB.USBManager

i.USBAccessory(14+)

USB配件信息。

系统能力: SystemCapability.USB.USBManager

(4)使用示例

let devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
console.log(`devicesList = ${devicesList}`);
/*
devicesList 返回的数据结构,此处提供一个简单的示例,如下
[{name: "1-1",serial: "",manufacturerName: "",productName: "",version: "",vendorId: 7531,productId: 2,clazz: 9,subClass: 0,protocol: 1,devAddress: 1,busNum: 1,configs: [{id: 1,attributes: 224,isRemoteWakeup: true,isSelfPowered: true,maxPower: 0,name: "1-1",interfaces: [{id: 0,protocol: 0,clazz: 9,subClass: 0,alternateSetting: 0,name: "1-1",endpoints: [{address: 129,attributes: 3,interval: 12,maxPacketSize: 4,direction: 128,number: 1,type: 3,interfaceId: 0,},],},],},],},
]
*/

结束语

通过本文的详细介绍,大家应该都了解了HarmonyOS中设备管理的使用方法,从基础概念到高级应用,从设备发现与连接到数据同步与资源共享,全方位剖析了设备管理的特性和应用场景。设备管理作为HarmonyOS开发中的关键环节,不仅涉及到设备的发现、连接和控制,还涉及到设备之间的数据同步和资源共享。掌握设备管理的使用方法,对于开发出能够充分利用HarmonyOS分布式特性的高质量应用至关重要。在实际开发中,合理使用设备管理不仅可以提升应用的性能和用户体验,还能在资源管理、错误处理和性能监控等方面发挥重要作用,通过设备发现、连接、控制、数据同步等技术手段,我们可以实现设备之间的无缝协同,提升应用的实用性和用户满意度。同时,结合资源管理、错误处理与容错机制、性能监控与分析等最佳实践,我们能够进一步提升应用的质量和用户体验。希望本文的介绍和示例代码能够帮助你在HarmonyOS开发中更好地使用设备管理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/908924.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

el-select下拉框 添加 el-checkbox 多选框

效果 vue <el-select v-model"value" multiple style"width: 100%" popper-class"select-popover-class" placeholder"请选择试验项目"><el-option v-for"item in options" :key"item.value" :value&qu…

Memory Repair (三)

Top-Level Verification and Pattern Generation 本节涵盖 fuse box 编程、顶层 BISR&#xff08;内置自修复&#xff09;验证以及生产测试 pattern 的生成 Fuse Box Programming 通过 BISR controller 对 fuse box 进行编程的两种方法如下&#xff1a; • 采用 Autonomous mod…

通过Wrangler CLI在worker中创建数据库和表

官方使用文档&#xff1a;Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后&#xff0c;会在本地和远程创建数据库&#xff1a; npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库&#xff1a; 现在&#xff0c;您的Cloudfla…

谷歌aab怎么转 apk

一、环境搭建&#xff1a; 1、搭建 java 环境&#xff1b;2、安装 AndroidStudio&#xff1b;3、下载 bundletool&#xff08;地址&#xff1a;Releases google/bundletool GitHub&#xff09;&#xff1b;4、确定本地有没有签名文件&#xff0c;mac电脑一般在/users/ 自己的…

04-初识css

一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…

AWS EKS 集群日志上报观测云实践

AWS Lambda 介绍 AWS Lambda 是亚⻢逊提供的⼀种⽆服务器计算服务。它允许开发⼈员在⽆需管理服务器的情况下运⾏代码。AWS Lambda 基于事件驱动的模型&#xff0c;当触发指定的事件时&#xff0c;Lambda 会⾃动执⾏相应的代码逻辑。 Amazon CloudWatch 日志 CloudWatch 日志…

浏览器指纹科普 | 端口扫描保护是什么?

&#x1f50d; 什么是“端口”&#xff1f; 每台电脑都像一个办公大楼&#xff0c;端口就像是不同的房间号。不同软件&#xff08;比如浏览器、代理、远程控制工具&#xff09;会用不同的端口来“对外沟通”。 比如&#xff1a; 浏览网页可能用端口 80 或 443 用代理软件或某…

傲软录屏:轻松录制,高效分享

在数字内容创作和在线教育日益流行的今天&#xff0c;屏幕录制已成为许多人表达创意、分享知识的重要方式。无论是制作教学视频、记录游戏过程&#xff0c;还是进行远程会议记录&#xff0c;一款简单易用且功能强大的屏幕录制软件都是不可或缺的。傲软录屏正是这样一款能够满足…

小程序查广州楼盘网签数据和备案价(免费)

目录 一、网签数据/销控表查询二、备案价和不利因素查询三、如何体验 一、网签数据/销控表查询 二、备案价和不利因素查询 三、如何体验 #广州楼盘备案价查询 #网签数据查询 #广州买房必看攻略 #小程序查广州楼盘备案价

【HarmonyOS5】UIAbility组件生命周期详解:从创建到销毁的全景解析

⭐本期内容&#xff1a;【HarmonyOS5】UIAbility组件生命周期详解&#xff1a;从创建到销毁的全景解析 &#x1f3c6;系列专栏&#xff1a;鸿蒙HarmonyOS&#xff1a;探索未来智能生态新纪元 文章目录 前言生命周期全景图详细状态解析与最佳实践&#x1f3ac; Create状态&#…

【云计算系统】云计算中的计算几何

一、云计算系统中的几何算法 云计算系统在资源调度、空间数据处理、安全加密及大规模优化等场景中广泛运用几何算法以提升效率与精度。 空间数据处理与索引算法 ​空间索引算法(R树、四叉树)​​ ​作用​:高效管理地理空间数据(如地图坐标、三维点云),支持快速范围查询…

基于物联网技术设计的设计室内宠物监护系统

目录 项目开发背景设计实现的功能项目硬件模块组成设计思路系统功能总结技术方案使用的模块的技术详情介绍预期成果总结 1. 项目开发背景 随着科技的不断进步&#xff0c;物联网&#xff08;IoT&#xff09;技术逐渐渗透到生活中的各个方面&#xff0c;尤其在智能家居领域&am…

aurora与pcie的数据高速传输

设备&#xff1a;zynq7100&#xff1b; 开发环境&#xff1a;window&#xff1b; vivado版本&#xff1a;2021.1&#xff1b; 引言 之前在前面两章已经介绍了aurora读写DDR,xdma读写ddr实验。这次我们做一个大工程&#xff0c;pc通过pcie传输给fpga&#xff0c;fpga再通过aur…

产品经理入门到精通:01需求调研

一、需求调研 1、需求&#xff1a;用户在某些方面需要得到某种帮助以达成目的。 2、调研&#xff1a;通过一些方法来了解某件事情的真相&#xff0c;也可以叫调查研究。 3、需求调研&#xff1a;通过观察、访谈和体验等方式&#xff0c;探究事物本质的过程。是需求诞生的开始…

【Android】Android 开发 ADB 常用指令

查看当前连接的设备 adb devices 连接设备 adb connect 设备IP 断开已连接的设备 adb disconnect 设备IP 安装应用 adb install 安装包的路径 卸载应用 adb uninstall 应用包名 查看已安装的应用包名 adb shell pm list packages 查看已安装的第三方应用包名 adb shell pm list…

Android 应用开发概述与环境搭建指南

Android 应用开发概述与环境搭建指南 Android 应用开发概述与环境搭建指南一、Android 开发概述&#xff08;一&#xff09;Android 平台简介&#xff08;二&#xff09;Android 开发特点&#xff08;三&#xff09;开发语言与技术栈 二、开发环境搭建&#xff08;Windows 系统…

LocalDate类使用

1.LocalDateTime转LocalDate LocalDateTime startTime new LocalDateTime; LocalDate localDate startTime.toLocalDate(); localDate.atTime(0,0) // 设置小时分钟 localDate.atTime(23,59)// 获取明天日期 LocalDate.now().plusDays(1).atTime(0,0,0) 2.流式计算通过时间作…

驭码CodeRider 2.0深度测评:助力高效开发【探索化学奇妙世界】网站

目录 前言&#xff1a; 一、驭码 CodeRider2.0介绍 二、驭码 CodeRider2.0集成 1、准备编辑器 2、打开 Visual Studio Code 扩展窗口&#xff0c;搜索“驭码 CodeRider”&#xff0c;找到插件后点击安装​编辑 3、登录 CodeRider 4、选择OAuth 登录 5、登录成功后即可体…

【java】【服务器】线程上下文丢失 是指什么

目录 ■前言 ■正文开始 线程上下文的核心组成部分 为什么会出现上下文丢失&#xff1f; 直观示例说明 为什么上下文如此重要&#xff1f; 解决上下文丢失的关键 总结 ■如果我想在servlet中使用线程&#xff0c;代码应该如何实现 推荐方案&#xff1a;使用 ManagedE…

代码规范和架构【立芯理论一】(2025.06.08)

1、代码规范的目标 代码简洁精炼、美观&#xff0c;可持续性好高效率高复用&#xff0c;可移植性好高内聚&#xff0c;低耦合没有冗余规范性&#xff0c;代码有规可循&#xff0c;可以看出自己当时的思考过程特殊排版&#xff0c;特殊语法&#xff0c;特殊指令&#xff0c;必须…