【纯血鸿蒙】鸿蒙HDC命令合集

由于鸿蒙生态还处于初期,官方提供的hdc命令还在不断修改中,部分命令会有变动。
如果文档没来得及更新,欢迎大家提PR和Issue补充指正,觉得有用的可以点 Star⭐️收藏。

HDC(OpenHarmony Device Connector) 是为鸿蒙开发/测试人员提供的用于设备调试的命令行工具,类似Android端的ADB工具。

HDC主要有三部分组成:

  1. hdc client:运行于电脑上的客户端,用户可以在电脑命令终端(windows cmd/linux shell)下请求执行相应的hdc命令。
  2. hdc server:作为后台进程也运行于电脑上,server管理client和设备端daemon之间通信包括连接的复用、数据通信包的收发,以及个别本地命令的直接处理。
  3. hdc daemon:daemon部署于OpenHarmony设备端作为守护进程按需运行,负责处理来自client端请求。

hdc

Table of Contents

HDC安装

  • 下载 Command Line Tools 并解压

  • hdc文件在command-line-tools/sdk/HarmonyOS-NEXT-DB2/openharmony/toolchains目录下

  • 配置电脑环境变量,以macOS为例,在~/.bash_profile 或者 ~/.zshrc文件中添加如下内容:

export HM_SDK_HOME="/Users/develop/command-line-tools/sdk/HarmonyOS-NEXT-DB2"  //请以sdk实际安装目录为准
export PATH=$PATH:$HM_SDK_HOME/hms/toolchains:$HM_SDK_HOME/openharmony/toolchains
export HDC_SERVER_PORT=7035

也可以自行编译安装:参考鸿蒙官方gitree文档

基本用法

基本语法

hdc -t <connectKey> <command>

如果只有一个设备/模拟器连接时,可以省略掉-t <connectKey> 这一部分,直接使用hdc <command>。在多个设备/模拟器连接的情况下需要指定-t 参数, connectKey可以通过hdc list targets命令获取,对应Android里的adb devices获取的serialNumber

$ hdc list targets

127.0.0.1:5555    //<IP>:<Port>形式的connectKey ,一般为无线连接的设备或模拟器
FMR0223C13000649

比如给FMR0223C13000649 这个设备安装应用:

$ hdc -t FMR0223C13000649 install entry-default-signed.hap

[Info]App install path:/Users/develop/entry-default-signed.hap, queuesize:0, msg:install bundle successfully.
AppMod finish

注意事项

  • 使用hdc,如果出现异常,可以尝试通过hdc kill -r命令杀掉并重启hdc服务。
  • 如果出现hdc list targets获取不到设备信息的情况,可以通过任务管理器查看是否有hdc进程存在。若进程存在,则通过hdc kill -r命令杀掉该进程。

设备连接管理

查看HDC版本

$ hdc -v

Ver: 2.0.0a

启动/停止 HDC Server

停止

$ hdc kill

Kill server finish

重启

$ hdc start -r

查询设备列表

$ hdc list targets

127.0.0.1:5555
FMR0223C13000649

-v 选项 显示详细信息

$ hdc list targets -v

127.0.0.1:5555		  TCP	  Connected	  localhost
FMR0223C13000649		USB	  Connected	  unknown...

输出的内容第一列为设备的connectKey, 第二列是设备连接方式,第三列为设备连接状态,第四列暂时未知

查询设备UDID

$ hdc shell bm get --udid

udid of current device is :
C46284C052AE01BBD2358FE44B279524B508FC959AAB5F4B0B74E42A06569B7E

这个udid在用开发者账号打包时,需要添加这个udid到对应的profile文件中

重启手机

$ hdc target boot

查看设备信息

名称

$ hdc shell param get const.product.name               

HUAWEI Mate 60 Pro

Brand

$ hdc shell param get const.product.brand

HUAWEI 

Model

$ hdc shell param get const.product.model

ALN-AL00 

系统版本

$ hdc shell param get const.product.software.version                                      

ALN-AL00 5.0.0.22(SP35DEVC00E22R4P1log) 

OS版本

$ hdc shell param get const.ohos.apiversion  

12 

CPU架构

$ hdc  shell param get const.product.cpu.abilist  

arm64-v8a 

分辩率

$ hdc shell hidumper -s RenderService -a screen


-------------------------------[ability]-------------------------------


----------------------------------RenderService---------------------------------
-- ScreenInfo
screen[0]: id=0, powerstatus=POWER_STATUS_OFF, backlight=51, screenType=EXTERNAL_TYPE, render size: 1260x2720, physical screen resolution: 1260x2720, isvirtual=false, skipFrameInterval_:1

  supportedMode[0]: 1260x2720, refreshrate=120
  supportedMode[1]: 1260x2720, refreshrate=90
  supportedMode[2]: 1260x2720, refreshrate=60
  supportedMode[3]: 1260x2720, refreshrate=30
  activeMode: 1260x2720, refreshrate=60
  capability: name=, phywidth=72, phyheight=156,supportlayers=12, virtualDispCount=0, propCount=0, type=DISP_INTF_HDMI, supportWriteBack=false

执行上述命令后,解析返回内容,可以通过正则表达式提取1260x2720

wlan IP

$ hdc shell ifconfig

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:99055 errors:0 dropped:0 overruns:0 frame:0
          TX packets:99055 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5889697 TX bytes:5889697

wlan0     Link encap:Ethernet  HWaddr ea:f9:7d:21:52:31
          inet addr:172.31.125.111  Bcast:172.31.125.255  Mask:255.255.254.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1232924 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2061202 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:877179224 TX bytes:2570352818

p2p0      Link encap:Ethernet  HWaddr d2:0d:f7:cc:12:fb
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 TX bytes:0

chba0     Link encap:Ethernet  HWaddr ec:11:05:fb:18:66
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 TX bytes:0

注意:这个命令在Beta3版本之前,会提示Cannot open netlink socket: Permission denied,需要升级系统。

电量/温度

$ hdc shell hidumper -s BatteryService -a -i                

-------------------------------[ability]-------------------------------


----------------------------------BatteryService---------------------------------
Current time: 2024-05-30 12:08:37.419
capacity: 100 
batteryLevel: 1 
chargingStatus: 3 
healthState: 1 
pluggedType: 1 
voltage: 4496732 
present: 1 
technology: Li-poly 
nowCurrent: 123 
currentAverage: 83 
totalEnergy: 5203 
remainingEnergy: 5207 
remainingChargeTime: 0 
temperature: 280 
chargeType: 1 

查看屏幕信息

$ hdc shell hidumper -s DisplayManagerService -a -a

-------------------------------[ability]-------------------------------


----------------------------------DisplayManagerService----------------------------------
-------------- DMS Multi User Info --------------
[oldScbPid:]
[userId:] 100
[ScbPid:] 4438
---------------- Screen ID: 0 ----------------
FoldStatus:                   UNKNOWN
[SCREEN SESSION]
Name:                         UNKNOWN
RSScreenId:                   0
activeModes<id, W, H, RS>:    0, 1260, 2720, 120
SourceMode:                   0
ScreenCombination:            0
Orientation:                  0
Rotation:                     0
ScreenRequestedOrientation:   0
[RS INFO]
SupportedColorGamuts:         0, 4, 6
ScreenColorGamut:             4
GraphicPixelFormat:           0
SupportedScreenHDRFormat:     2, 1, 3
ScreenHDRFormat:              2
SupportedColorSpaces:         0, 2294273, 2294278
ScreenColorSpace:             2294273
[CUTOUT INFO]
WaterFall_L<X,Y,W,H>:         0, 0, 0, 0
WaterFall_T<X,Y,W,H>:         0, 0, 0, 0
WaterFall_R<X,Y,W,H>:         0, 0, 0, 0
WaterFall_B<X,Y,W,H>:         0, 0, 0, 0
BoundingRects<X,Y,W,H>:       [494, 36, 273, 72]
[SCREEN INFO]
VirtualWidth:                 387
VirtualHeight:                836
LastParentId:                 18446744073709551615
ParentId:                     1
IsScreenGroup:                0
VirtualPixelRatio:            3.25
Rotation:                     0
Orientation:                  0
SourceMode:                   0
ScreenType:                   1
[SCREEN PROPERTY]
Rotation:                     0
Density:                      3.25
DensityInCurResolution:       3.25
PhyWidth:                     72
PhyHeight:                    156
RefreshRate:                  60
VirtualPixelRatio:            3.25
ScreenRotation:               0
Orientation:                  0
DisplayOrientation:           0
GetScreenType:                1
ReqOrientation:               0
DPI<X, Y>:                    444.5, 442.871
Offset<X, Y>:                 0, 0
Bounds<L,T,W,H>:              0, 0, 1260, 2720,
PhyBounds<L,T,W,H>:           0, 0, 1260, 2720,
AvailableArea<X,Y,W,H>        
### 关于鸿蒙 HDC 命令的使用教程 对于鸿蒙操作系统中的HDCHarmonyOS Device Connect)命令,其主要功能在于提供一套类似于Android ADB命令的工具来支持开发者进行设备管理、应用程序控制以及文件传输等一系列操作。 #### 设备管理和基本操作 为了查看当前已连接至开发机器上的所有目标设备,可以利用`hdc list targets`这一指令[^2]。这有助于确认设备是否成功连接并处于可调试状态。 #### 应用程序生命周期管理 针对应用层面的操作,提供了诸如启动指定的应用实例(`hdc shell aa start -a EntryAbility -b [appName]`)、终止正在运行的应用进程(`hdc shell am force-stop [appName]`)等功能;同时也能处理包级别的安装(`hdc shell bm install -p [appName]`)与卸载(`hdc shell bm uninstall [appName]`)需求。 #### 文件系统交互 当涉及到文件系统的读写时,则有推送本地文件到远程设备的功能(`hdc file send [filePath] [targetPath]`),还有移除特定路径下文件的能力(`hdc shell rm -rf [filePath]`)。 #### 环境配置建议 为了让这些命令更加便捷地被调用,在环境变量中加入HDC工具所在的目录是非常必要的措施之一。具体做法是在shell配置文件里追加类似这样的语句:`export PATH=${PATH}:/path/to/hdc/tools`,其中/path/to/hdc/tools应替换为实际存放HDC二进制文件的位置[^4]。 此外,考虑到可能存在的端口冲突问题,默认监听5037端口的服务可以通过设定环境变量`HDC_SERVER_PORT=其他未占用端口号`来进行调整,从而避免与其他服务发生冲突的情况出现。 ```bash # 设置HDC服务器使用的不同端口 export HDC_SERVER_PORT=7035 ``` 通过上述介绍可以看出,掌握好这套基于命令行接口的工作流能够极大地提高日常开发效率,并简化跨平台项目的维护成本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值