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 password"
	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 password"
	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 password"
	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 password"
	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 password"
	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 password"
	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 password"
	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
通过脚本解锁手机并连接-6
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  num_args=$#						# 获取参数数量    

  if [ "${num_args}" -eq 0 ]; then
	echo -e "Usage: \n$0 usb/net IPV4:Port"
  elif [ "$1" = "net" ]; then
	adb connect $2;
	# scrcpy -s $2 --no-audio -S &
	scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &
  elif [ "$1" = "usb" ]; then
	scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &     
  fi

  if [ "$3" = "unlock" ]; then
	if [ "$1" = "net" ]; then
	  adb connect $2;
	fi
	if [ "$4" = "" ]; then
	  echo -e "Usage: \n$0 usb/net device unlock password"
	else
	  adb -s $2 shell input keyevent 82; adb -s $2 shell input keyevent 82; sleep 0.4; adb -s $2 shell input text $4 && clear
	fi
  elif [ "$3" = "lock" ]; then
	adb -s $2 shell input keyevent 26
  fi
通过脚本解锁手机并连接-7
 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
26
27
28
29
30
31
32
33
34
35
36
37
  num_args=$#						# 获取参数数量    

  if [ "${num_args}" -eq 0 ]; then
	echo -e "Usage: \n$0 usb/net IPV4:Port"
  elif [ "$1" = "net" ]; then
	adb connect $2;
	# scrcpy -s $2 --no-audio -S &
	scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &
  elif [ "$1" = "usb" ]; then
	scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &     
  fi  

  if [ "$3" = "unlock" ]; then
	if [ "$1" = "net" ]; then
	  adb connect $2;
	fi
	if [ "$4" = "" ]; then
	  echo -e "Usage: \n$0 usb/net device unlock passward"
	else
	  adb -s $2 shell input keyevent 82; adb -s $2 shell input keyevent 82; sleep 0.4; adb -s $2 shell input text $4 && clear
	fi
  elif [ "$3" = "lock" ]; then
	adb -s $2 shell input keyevent 26
  fi

  if [ "$1" = "lock" ] || [ "$1" = "unlock" ]; then
	device=$(adb devices | grep -w "device" | awk 'NR==1{print $1}');
	if [ "$1" = "unlock" ]; then	
	  if [ "$2" = "" ]; then
		echo -e "Usage: \n$0 unlock passward"
	  else
		adb -s ${device} shell input keyevent 82; adb -s ${device} shell input keyevent 82; sleep 0.4; adb -s ${device} shell input text $2 && clear
	  fi
	elif [ "$1" = "lock" ]; then
	  adb -s ${device} shell input keyevent 26
	fi
  fi
通过脚本解锁手机并连接-8
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
num_args=$#						# 获取参数数量    

if [ "${num_args}" -eq 0 ]; then
  echo -e "Usage: \n$0 usb/net IPV4:Port"
elif [ "$1" = "net" ]; then
  adb connect $2;
  # scrcpy -s $2 --no-audio -S &
  scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &
elif [ "$1" = "usb" ]; then
  scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S &     
fi  

if [ "$3" = "unlock" ]; then
  if [ "$1" = "net" ]; then
	adb connect $2;
  fi
  if [ "$4" = "" ]; then
	echo -e "Usage: \n$0 usb/net deviceid unlock passward"
  else
	adb -s $2 shell input keyevent 82; adb -s $2 shell input keyevent 82; sleep 0.4; adb -s $2 shell input text $4 && clear
  fi
elif [ "$3" = "lock" ]; then
  adb -s $2 shell input keyevent 26
fi

if [ "$1" = "lock" ] || [ "$1" = "unlock" ]; then
  device=$(adb devices | grep -w "device" | awk 'NR==1{print $1}');
  if [ "$1" = "unlock" ]; then	
	if [ "$2" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s ${device} shell input keyevent 82; adb -s ${device} shell input keyevent 82; sleep 0.4; adb -s ${device} shell input text $2 && clear
	fi
  elif [ "$1" = "lock" ]; then
	adb -s ${device} shell input keyevent 26
  fi
fi

if [ "$1" = "example" ]; then  
  echo "bash $0 usb \$(adb devices | grep -w \"device\" | awk 'NR==1{print \$1}') unlock \$passwd"
fi
通过脚本解锁手机并连接-9
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
num_args=$#						# 获取参数数量    

if [ "${num_args}" -eq 0 ]; then
  echo -e "Usage: \n$0 usb/net IPV4:Port"
elif [ "$1" = "net" ]; then
  adb connect $2;
  # scrcpy -s $2 --no-audio -S &
  scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 -S &
elif [ "$1" = "usb" ]; then
  scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 -S &     
fi  

if [ "$3" = "unlock" ]; then
  if [ "$1" = "net" ]; then
	adb connect $2;
  fi
  if [ "$4" = "" ]; then
	echo -e "Usage: \n$0 usb/net deviceid unlock passward"
  else
	adb -s $2 shell input keyevent 82; adb -s $2 shell input keyevent 82; sleep 0.4; adb -s $2 shell input text $4 && clear
  fi
elif [ "$3" = "lock" ]; then
  adb -s $2 shell input keyevent 26
fi

if [ "$1" = "lock" ] || [ "$1" = "unlock" ]; then
  device=$(adb devices | grep -w "device" | awk 'NR==1{print $1}');
  if [ "$1" = "unlock" ]; then	
	if [ "$2" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s ${device} shell input keyevent 82; adb -s ${device} shell input keyevent 82; sleep 0.4; adb -s ${device} shell input text $2 && clear
	fi
  elif [ "$1" = "lock" ]; then
	adb -s ${device} shell input keyevent 26
  fi
fi

if [ "$1" = "example" ]; then  
  echo "bash $0 usb \$(adb devices | grep -w \"device\" | awk 'NR==1{print \$1}') unlock \$passwd"
fi
通过脚本解锁手机并连接-10
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
num_args=$#						# 获取参数数量    

if [ "${num_args}" -eq 0 ]; then
  echo -e "Usage: \n$0 usb/net IPV4:Port"
elif [ "$1" = "net" ]; then
  adb connect $2;
  # scrcpy -s $2 --no-audio -S
  scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S
elif [ "$1" = "usb" ]; then
  scrcpy -s $2 --no-audio --keyboard=uhid --window-x 1920 --window-y 0 --window-height 1038 -S
fi  

if [ "$3" = "unlock" ]; then
  if [ "$1" = "net" ]; then
	adb connect $2;
  fi
  if [ "$4" = "" ]; then
	echo -e "Usage: \n$0 usb/net deviceid unlock passward"
  else
	adb -s $2 shell input keyevent 82; adb -s $2 shell input keyevent 82; sleep 0.4; adb -s $2 shell input text $4 && clear
  fi
elif [ "$3" = "lock" ]; then
  adb -s $2 shell input keyevent 26
fi

if [ "$1" = "lock" ] || [ "$1" = "unlock" ]; then
  device=$(adb devices | grep -w "device" | awk 'NR==1{print $1}');
  if [ "$1" = "unlock" ]; then	
	if [ "$2" = "" ]; then
	  echo -e "Usage: \n$0 unlock passward"
	else
	  adb -s ${device} shell input keyevent 82; adb -s ${device} shell input keyevent 82; sleep 0.4; adb -s ${device} shell input text $2 && clear
	fi
  elif [ "$1" = "lock" ]; then
	adb -s ${device} shell input keyevent 26
  fi
fi

if [ "$1" = "example" ]; then  
  echo "bash $0 usb \$(adb devices | grep -w \"device\" | awk 'NR==1{print \$1}') unlock \$passwd"
fi
如果要无限循环自动连接
1
while true; do ~/auto_android_scrcpy.sh usb device > /dev/null 2>&1; sleep 1; done
通过脚本解锁手机并连接-11
  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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash

# 显示使用帮助
show_usage() {
	echo "用法: $0 [usb|net|lock|unlock|example] [参数...]"
	echo ""
	echo "示例:"
	echo "  1) bash $0 usb \$(adb devices | grep -w \"device\" | awk 'NR==1{print \$1}') unlock \$passwd"
	echo "  2) while true; do ~/auto_android_scrcpy.sh usb DEVICE_ID > /dev/null 2>&1; sleep 1; done"
	echo "  3) $0 lock                        # 锁定当前设备"
	echo "  4) $0 unlock	              # 解锁当前设备"
}

# 启动 scrcpy 会话
start_scrcpy() {
	local conn_type=$1
	local device_id=$2

	if [[ -z "$device_id" ]]; then
		echo "错误: 设备ID不能为空"
		exit 1
	fi

	if [[ "$conn_type" == "net" ]]; then
		adb connect "$device_id" || {
			echo "错误: 无法连接到 $device_id"
					exit 1
				}
	fi

	scrcpy -s "$device_id" --no-audio --window-x 1920 --window-y 0 --window-height 1038 -S
}

# 解锁设备
unlock_device() {
	local device_id=$1
	local password=$2

	if [[ -z "$password" ]]; then
		echo "错误: 未提供解锁密码"
		show_usage
		return 1
	fi

	adb -s "$device_id" shell input keyevent 82
	adb -s "$device_id" shell input keyevent 82
	sleep 0.4
	adb -s "$device_id" shell input text "$password"
}

# 锁定设备
lock_device() {
	local device_id=$1
	adb -s "$device_id" shell input keyevent 26
}

# 获取第一个已连接的设备ID
get_first_device() {
	adb devices | grep -w "device" | awk 'NR==1{print $1}'
}

# 主逻辑
main() {
	local num_args=$#

	# 无参数时显示交互菜单
	if [[ $num_args -eq 0 ]]; then
		echo "请选择操作模式:"
		echo "1) USB设备 + 解锁 + 自动重连模式"
		echo "2) 自动重连模式"
		echo "3) 锁定设备"
		echo "4) 解锁设备"
		read -p "请输入选项 [1/2/3/4]: " choice

		case $choice in
			1)
				local device_id=$(get_first_device)
				if [[ -z "$device_id" ]]; then
					echo "错误: 未检测到已连接的USB设备"
					exit 1
				fi
				read -sp "请输入解锁密码: " passwd
				echo
				while true; do
					"$0" usb "$device_id" unlock "$passwd"
					sleep 1
				done
				;;
			2)
				local device_id=$(get_first_device)
				if [[ -z "$device_id" ]]; then
					echo "错误: 未检测到已连接的USB设备"
					exit 1
				fi
				echo
				while true; do
					"$0" usb "$device_id" > /dev/null 2>&1
					sleep 1
				done
				;;
			3)
				local device_id=$(get_first_device)
				if [[ -z "$device_id" ]]; then
					echo "错误: 未检测到已连接的USB设备"
					exit 1
				fi
				adb -s $device_id shell input keyevent 26
				;;
			4)
				local device_id=$(get_first_device)
				if [[ -z "$device_id" ]]; then
					echo "错误: 未检测到已连接的USB设备"
					exit 1
				fi
				read -sp "请输入解锁密码: " passwd
				echo
				# "$0" usb "$device_id" unlock "$passwd"
				adb -s "$device_id" shell input keyevent 82; adb -s "$device_id" shell input keyevent 82; sleep 0.4; adb -s "$device_id" shell input text "$passwd"
				;;
			,*)
				echo "无效选项"
				exit 1
				;;
		esac
		exit 0
	fi

	local command=$1

	# 处理 example 命令
	if [[ "$command" == "example" ]]; then
		show_usage
		exit 0
	fi

	# 处理 lock/unlock 快捷命令(无设备ID时使用默认设备)
	if [[ "$command" == "lock" ]] || [[ "$command" == "unlock" ]]; then
		local device_id=$(get_first_device)
		if [[ -z "$device_id" ]]; then
			echo "错误: 未检测到已连接的设备"
			exit 1
		fi

		if [[ "$command" == "unlock" ]]; then
			unlock_device "$device_id" "$2"
		else
			lock_device "$device_id"
		fi
		exit 0
	fi

	# 主流程: usb / net 模式
	if [[ "$command" != "usb" ]] && [[ "$command" != "net" ]]; then
		echo "错误: 未知命令 '$command'"
		show_usage
		exit 1
	fi

	local conn_type=$command
	local device_id=$2
	local action=$3
	local password=$4

	# 启动 scrcpy
	start_scrcpy "$conn_type" "$device_id" &
	local scrcpy_pid=$!

	# 处理 unlock/lock 操作
	if [[ "$action" == "unlock" ]]; then
		if [[ "$conn_type" == "net" ]]; then
			adb connect "$device_id" || {
				echo "警告: 解锁前连接失败"
			}
		fi
		unlock_device "$device_id" "$password"
	elif [[ "$action" == "lock" ]]; then
		lock_device "$device_id"
	fi

	# 等待 scrcpy 结束
	wait "$scrcpy_pid" 2>/dev/null
}

# 执行主函数
main "$@"

注意事项

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 会话的自动化控制和命令发送,适用于需要在后台运行的程序和脚本的自动化管理。

comments powered by Disqus