scrcpy使用教程

scrcpy

scrcpy入门

官方主页

https://github.com/Genymobile/scrcpy

下载链接

根据电脑系统环境进行选择下载

https://github.com/Genymobile/scrcpy/releases/latest

连接方式

首先,启用手机的开发者模式,接着在开发者模式找到并打开手机的usb调试模式,然后使用usb数据线连接电脑与手机,在电脑上准备好scrcpy并添加scrcpy到环境变量,先使用adb查看是否有设备,然后在电脑终端中通过adb命令设置tcpip端口和连接的ip,电脑与手机保持在同一局域网,手机ip可以通过wifi(如果是手机开热点,可以通过热点查看ip)或者手机安装termux进行查看,再接着使用adb命令列出设备列表,使用scrcpy的-s选项选择要启动的设备即可,具体操作命令如下:

1
2
3
4
  adb devices						# 连接usb数据线后查看是否有设备
  adb tcpip 5555					# 在有设备的情况下设置端口,事实上这里似乎也可以不设置,那么可以尝试跳过这一步
  adb connect 192.168.*.*:5555		# 设置连接方式
  scrcpy -s 192.168.*.*:5555		# 使用scrcpy进行连接

无线投屏

在第一次进行过有线连接后,第二次就可以拔掉数据线进行连接了

安卓端录屏工具scrcpy

1
  scrcpy --record file.mp4   #file.mp4 是自己设置的文件名和对应的格式

scrcpy不传输声音,并且当scrcpy连接到设备时使设备黑屏,只显示scrcpy画面

该操作适用于蓝牙耳机连接手机,然后scrcpy投屏设备的场景

1
  scrcpy --no-audio -S

scrcpy指定窗口位置和大小

1
  scrcpy -s 192.168.*.*:5555 --window-x 1920 --window-y 0 --window-height 1080

如果要指定宽度可以增加选项:–window-width 100

通过脚本连接

ssh_host是ssh配置文件中的host,也可以改为user@hostname的形式,需要结合termux使用

1
2
3
4
5
6
7
  echo "adb shell input keyevent 82; adb shell input keyevent 82; sleep 0.4; adb shell input text password && clear"
  echo "adb shell input keyevent 224; adb shell input keyevent 82;"
  adb kill-server
  adb start-server  
  ssh_host="rog8"; mapfile -t ip_array < <(ssh $ssh_host ifconfig | grep -oE "inet ?192.168.[0-9]{0,3}.[0-9]{0,3}" | awk '{print $2}');
  adb connect "${ip_array[0]}"
  scrcpy -s $(adb devices | grep -Ew ".*device" | awk '{$2=""; print}' | head -n 1) -S

通过脚本解锁手机并连接-1

1
  adb shell input keyevent 82; adb shell input keyevent 82; sleep 0.4; adb shell input text password && clear && scrcpy --no-audio -S &

通过脚本解锁手机并连接-2

脚本名称:auto_android_scrcpy.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  num_args=$#						# 获取参数数量    

  if [ "${num_args}" -eq 0 ]; then
	echo -e "Usage: \n$0 IPV4:Port"
  elif [ "${num_args}" -eq 1 ]; then
	adb connect $1;
	scrcpy -s $1 --no-audio -S &
  elif [ "$2" = "unlock" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && clear
	fi
  elif [ "$2" = "rescrcpy" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && scrcpy -s $1 --no-audio -S && clear &
	fi
  fi

通过脚本解锁手机并连接-3

该脚本使用的前提条件是两个显示器,因为指定坐标是x=1920,y=1038。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  num_args=$#                                             # 获取参数数量

  if [ "${num_args}" -eq 0 ]; then
	echo -e "Usage: \n$0 IPV4:Port"
  elif [ "${num_args}" -eq 1 ]; then
	adb connect $1;
	# scrcpy -s $1 --no-audio -S &
	scrcpy -s $1 --no-audio --window-x 1920 --window-y 0 --window-height 1038 -S &
  elif [ "$2" = "unlock" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && clear
	fi
  elif [ "$2" = "rescrcpy" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && scrcpy -s $1 --no-audio -S && clear &
	fi
  fi

通过脚本解锁手机并连接-4

基于"通过脚本解锁手机并连接-3",优化解锁,添加了解锁前连接adb的命令,使解决可以单独使用,添加了锁命令,使手机没有通过scrcpy远程控制时也可以通过脚本对手机进行锁屏操作。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  num_args=$#						# 获取参数数量    

  if [ "${num_args}" -eq 0 ]; then
	echo -e "Usage: \n$0 IPV4:Port"
  elif [ "${num_args}" -eq 1 ]; then
	adb connect $1;
	# scrcpy -s $1 --no-audio -S &
	scrcpy -s $1 --no-audio --window-x 1920 --window-y 0 --window-height 1038 -S &
  elif [ "$2" = "unlock" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb connect $1;
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && clear
	fi
  elif [ "$2" = "lock" ]; then
	adb connect $1;
	adb -s $1 shell input keyevent 26
  elif [ "$2" = "rescrcpy" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && scrcpy -s $1 --no-audio -S && clear &
	fi
  fi

通过脚本解锁手机并连接-5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  num_args=$#						# 获取参数数量    

  if [ "${num_args}" -eq 0 ]; then
	echo -e "Usage: \n$0 IPV4:Port"
  elif [ "${num_args}" -eq 1 ]; then
	adb connect $1;
	# scrcpy -s $1 --no-audio -S &
	scrcpy -s $1 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &
  elif [ "$2" = "unlock" ]; then
	if [ "$3" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb connect $1;
	  adb -s $1 shell input keyevent 82; adb -s $1 shell input keyevent 82; sleep 0.4; adb -s $1 shell input text $3 && clear
	fi
  elif [ "$2" = "lock" ]; then
	adb connect $1;
	adb -s $1 shell input keyevent 26
  fi

注意事项

scrcpy建议使用最新版本,如果手机的安卓版本较高,而scrcpy的版本又较老的话,会出现一些快捷键用不了的情况,而最新版本的scrcpy则会好很多,一般快捷键都支持

异地组网——5 分钟,使用内网穿透快速实现远程手机桌面!

准备

PC 安装 Scrcpy 控制器

Scrpy 控制器是一款免 Root 的开源安卓设备投屏工具,支持 USB 和 WIFI 两种连接方式

它适用于 Linux、Mac OS、Windows

根据系统类型,通过下面链接选择合适的方式进行安装

PS:以 Windows 为例,我们只需要下载 Zip 压缩包,然后将文件夹目录添加到 PATH 环境变量中

https://github.com/Genymobile/scrcpy#summary

配置教程参考,将scrcpy按照参考教程中adb的配置方式进行配置:

https://sspai.com/post/40471

PC 和 Android 设备分别安装网穿透工具

这里以蒲公英为例,到官网选择「 蒲公英-个人版 」进行下载

下载地址:

https://pgy.oray.com/download/personal/#visitor

首先,保证 PC 与 Android 设备在同一局域网,这里的局域网是指通过数据线将PC与手机连接在一起

然后,在 PC 端完成注册登录

最后,在 Android 设备上以同一个账号进行登录

实操一下

Android 设备开启网络调试并连接

首先,在 Android 设备中开启「 开发者选项 」,并打开「 USB 调试 」

不同手机开启「 开发者选项 」的方式不一样,大家可以自行搜索,另外 MIUI 需要另外开启「 USB 调试(安全设置) 」

然后,Android 设备通过数据线连接 PC,使用 adb 命令开启手机的网络调试

PS:这里指定网络连接的端口号为 5555

查询连接的设备列表
1
2
3
  C:\Users\xingag>adb devices
  List of devices attached
  0e279f0d7d33    device
开启手机的网络调试,端口为5555
1
2
  C:\Users\xingag>adb tcpip 5555
  restarting in TCP mode port: 5555

接着,断开数据线连接,在蒲公英 PC 客户端上查看 Android 设备的 IP 地址,通过「 该 IP 地址 + 端口号 」连接手机设备

比如:PC版本蒲公英显示手机的ip地址为:172.11.1.233
1
  adb connect 172.11.1.233:5555
使用adb命令连接设备
1
2
  C:\Users\xingag>adb connect 172.11.1.233:5555
  connected to 172.11.1.233:5555

最后,可以通过一些简单的 adb 命令测试一下是否能控制手机

比如:修改屏幕的大小
1
  adb shell wm size 1000x1000
恢复原来屏幕尺寸
1
  adb shell wm size reset
休眠或者解锁屏幕
1
  adb shell input keyevent 26
局域网屏幕控制

在 CMD 命令行,使用「 scrcpy.exe 」命令就可以将手机投屏在 PC 上了,如此就能在 PC 端完成对手机的完全控制了

scrcpy 功能非常地强大,可以通过一些参数命令设置屏幕控制的特性

比如,通过 -S 设置投屏的同时关闭设备屏幕,也可以通过 -b、-m 改变比特率、限制分辨率,提高屏幕控制的响应速度

直接将手机界面投屏到PC上,通过PC可以控制手机界面
1
  scrcpy.exe
投屏的同时,关闭设备屏幕
1
  scrcpy.exe -S
  1. 通过改变比特率和限制分辨率,提高响应速度
  2. -b 2M:改变比特率为2M,减少延迟,默认码率为8M,码率越高,画质越好,但是同时延迟越大
  3. -m 1024:限制分辨率提供响应性能,分辨率越高,延迟越大
  4. –max-fps:限制帧率,提升响应速度
1
  scrcpy -b 2M  -m 1024 --max-fps 15
远程屏幕控制

上面的操作都是在同一局域网下完成的 PC 端对 Android 设备的屏幕控制

事实上,只要保证 Android 设备上的蒲公英 App 一直在线,PC 即使在外网环境下,开启 PC 版本的蒲公英,也能远程控制之前配置好的 Android 设备

如此,就完成了远程控制 Android 设备屏幕的需求了

解锁手机

1
  adb shell input text password	# 输入密码

测试

手机密码:123456

1
  adb shell input keyevent 82; adb shell input keyevent 82; sleep 0.4; adb shell input text 123456; sleep 2; adb shell input keyevent 26 #输入密码解锁后延时2秒,然后再锁屏

解锁手机:adb shell input keyevent 82

延时0.4秒:sleep 0.4

输入密码:adb shell input text 123456

锁屏:adb shell input keyevent 26

实操命令

1
  adb shell input keyevent 82; adb shell input keyevent 82; sleep 0.4; adb shell input text 123456 # 实操命令为去掉前边测试命令中的解锁后延时2秒再锁屏的过程

如何用bash脚本创建screen并向其发送命令?

1
screen -dmS test
1
2
3
4
screen -dmS test 
screen -S test -X stuff "ls -a\n" 
screen -S test -p 0 -X stuff "ls -a\n"

解析命令-ChatGPT

这些命令是使用 screen 进行会话管理和自动化输入的示例。我们来逐个解析:

1. screen -dmS test
各部分解析:
  • screen:启动 screen 会话,screen 是一个终端多路复用器,允许在一个终端窗口内运行多个虚拟终端。
  • -d:这个选项告诉 screen 在后台启动会话。也就是说,screen 会话启动后不会占用当前的终端窗口,而是将其放在后台运行。
  • -m:强制启动一个新的会话,即使没有其他正在运行的 screen 会话。
  • -S test:指定会话的名称为 test。-S 选项后面的 test 是会话的标识符,通过这个名字,你可以后续对该会话进行操作(例如发送命令、分离等)。
作用:

这个命令的作用是:启动一个名为 test 的 screen 会话,并将其放在后台运行。此时会话没有关联到任何窗口(终端)。它是一个“分离模式”启动的 screen 会话。

2. screen -S test -X stuff "ls -a\n"
各部分解析:
  • screen:再次调用 screen,用于对已存在的 screen 会话进行操作。
  • -S test:指定要操作的会话名称为 test。
  • -X:执行对 screen 会话的命令。-X 选项让我们向 screen 会话发送命令。
  • stuff "ls -a\n":stuff 命令将 ls -a(列出当前目录的所有文件,包括隐藏文件)发送到指定会话的当前窗口中,并模拟按下回车键(\n)。这等同于在该会话的终端中执行了 ls -a 命令。
作用:

该命令将 ls -a 这个命令发送到名为 test 的 screen 会话中的当前窗口,模拟在该窗口执行 ls -a。由于是后台操作,所以并不会直接在终端显示输出,命令的结果会被发送到该 screen 会话的输出流。

3. screen -S test -p 0 -X stuff "ls -a\n"
各部分解析:
  • screen:同样,调用 screen 来操作会话。
  • -S test:指定会话名称为 test。
  • -p 0:指定窗口编号为 0,即第一个窗口。screen 会话可以包含多个窗口,这个命令告诉 screen 要向编号为 0 的窗口发送命令。
  • -X stuff "ls -a\n":将 ls -a 发送到指定窗口,并模拟按下回车键。
作用:

该命令将 ls -a 命令发送到名为 test 的 screen 会话中的第一个窗口(编号为 0),并模拟按下回车键。这意味着,如果会话已经包含多个窗口,这个命令只会作用于第一个窗口,而不会影响其他窗口。

总结
  1. screen -dmS test:在后台启动一个新的 screen 会话,命名为 test。
  2. screen -S test -X stuff "ls -a\n":向名为 test 的 screen 会话的当前窗口发送 ls -a 命令并执行。
  3. screen -S test -p 0 -X stuff "ls -a\n":向名为 test 的 screen 会话的第一个窗口(编号为 0)发送 ls -a 命令并执行。

这些命令组合起来,可以实现对后台 screen 会话的自动化控制和命令发送,适用于需要在后台运行的程序和脚本的自动化管理。