-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrcpy-wireless.sh
More file actions
326 lines (284 loc) · 11.5 KB
/
scrcpy-wireless.sh
File metadata and controls
326 lines (284 loc) · 11.5 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/bash
# SCRCPY Wireless Launcher for macOS/Linux
# Author: netphantom.og
# GitHub: https://github.com/netphantom-og
# Instagram: https://instagram.com/netphantom.og
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color
# Function to print colored output
print_banner() {
echo
echo -e "${CYAN}==================================================================${NC}"
echo -e "${CYAN} ${NC}"
echo -e "${GREEN} SCRCPY WIRELESS LAUNCHER ${NC}"
echo -e "${YELLOW} Version 1.0.0 ${NC}"
echo -e "${CYAN} ${NC}"
echo -e "${MAGENTA} Author: netphantom.og ${NC}"
echo -e "${CYAN} ${NC}"
echo -e "${CYAN}==================================================================${NC}"
echo
}
# Function to detect OS
detect_os() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "macos"
else
echo "unknown"
fi
}
# Function to get architecture
get_architecture() {
if [[ "$(uname -m)" == "x86_64" ]]; then
echo "64"
elif [[ "$(uname -m)" == "arm64" ]] || [[ "$(uname -m)" == "aarch64" ]]; then
echo "arm64"
else
echo "32"
fi
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to download scrcpy
download_scrcpy() {
local os=$1
local arch=$2
echo -e "${CYAN}[INFO] Detecting system and providing download link...${NC}"
if [[ "$os" == "macos" ]]; then
if [[ "$arch" == "arm64" ]]; then
echo -e "${GREEN}[INFO] macOS ARM64 detected${NC}"
echo -e "${GRAY}Download: https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-v2.4.0-macos-arm64.tar.gz${NC}"
else
echo -e "${GREEN}[INFO] macOS Intel detected${NC}"
echo -e "${GRAY}Download: https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-v2.4.0-macos-x86_64.tar.gz${NC}"
fi
elif [[ "$os" == "linux" ]]; then
if [[ "$arch" == "arm64" ]]; then
echo -e "${GREEN}[INFO] Linux ARM64 detected${NC}"
echo -e "${GRAY}Download: https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-v2.4.0-linux-arm64.tar.gz${NC}"
else
echo -e "${GREEN}[INFO] Linux x86_64 detected${NC}"
echo -e "${GRAY}Download: https://github.com/Genymobile/scrcpy/releases/download/v2.4.0/scrcpy-v2.4.0-linux-x86_64.tar.gz${NC}"
fi
fi
echo -e "${GRAY}1. Download the file above${NC}"
echo -e "${GRAY}2. Extract it to the 'scrcpy' folder${NC}"
echo -e "${GRAY}3. Make sure adb and scrcpy are executable${NC}"
echo -e "${GRAY}4. Run this script again${NC}"
echo
}
# Function to create desktop shortcut
create_desktop_shortcut() {
local script_path="$1"
local os="$2"
if [[ "$os" == "macos" ]]; then
# Create macOS application bundle or alias
local desktop_dir="$HOME/Desktop"
local shortcut_name="SCRCPY Wireless Launcher.command"
local shortcut_path="$desktop_dir/$shortcut_name"
if [[ ! -f "$shortcut_path" ]]; then
echo -e "${CYAN}[INFO] Creating desktop shortcut...${NC}"
cat > "$shortcut_path" << EOF
#!/bin/bash
cd "$(dirname "$0")"
cd "$(dirname "$script_path")"
./scrcpy-wireless.sh
EOF
chmod +x "$shortcut_path"
fi
elif [[ "$os" == "linux" ]]; then
# Create Linux desktop entry
local desktop_dir="$HOME/Desktop"
local shortcut_name="SCRCPY Wireless Launcher.desktop"
local shortcut_path="$desktop_dir/$shortcut_name"
if [[ ! -f "$shortcut_path" ]]; then
echo -e "${CYAN}[INFO] Creating desktop shortcut...${NC}"
cat > "$shortcut_path" << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=SCRCPY Wireless Launcher
Comment=One-click wireless Android screen mirroring
Exec=$script_path
Icon=phone
Terminal=true
Categories=Utility;
EOF
chmod +x "$shortcut_path"
fi
fi
}
# Main script
main() {
print_banner
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRCPY_DIR="$SCRIPT_DIR/scrcpy"
# Detect OS and architecture
OS=$(detect_os)
ARCH=$(get_architecture)
echo -e "${CYAN}[INFO] Detected: $OS ($(uname -m))${NC}"
# Check if scrcpy directory exists
if [[ ! -d "$SCRCPY_DIR" ]]; then
echo -e "${YELLOW}[INFO] Creating scrcpy directory...${NC}"
mkdir -p "$SCRCPY_DIR"
fi
# Check for adb
ADB_PATH="$SCRCPY_DIR/adb"
if [[ ! -f "$ADB_PATH" ]]; then
echo -e "${RED}[ERROR] Missing file: adb${NC}"
echo -e "${YELLOW}Expected at: $ADB_PATH${NC}"
echo
download_scrcpy "$OS" "$ARCH"
exit 1
fi
# Check for scrcpy
SCRCPY_PATH="$SCRCPY_DIR/scrcpy"
if [[ ! -f "$SCRCPY_PATH" ]]; then
echo -e "${RED}[ERROR] Missing file: scrcpy${NC}"
echo -e "${YELLOW}Expected at: $SCRCPY_PATH${NC}"
echo
download_scrcpy "$OS" "$ARCH"
exit 1
fi
# Make files executable
chmod +x "$ADB_PATH" 2>/dev/null
chmod +x "$SCRCPY_PATH" 2>/dev/null
# Create desktop shortcut
create_desktop_shortcut "$SCRIPT_DIR/scrcpy-wireless.sh" "$OS"
# Initialize adb server
echo -e "${CYAN}[INFO] Starting ADB server...${NC}"
"$ADB_PATH" start-server >/dev/null 2>&1
# Detect existing TCP/IP device first
echo -e "${CYAN}[INFO] Checking for existing wireless connections...${NC}"
SERIAL=""
FOUND_WIRELESS=false
# Check for existing wireless connections
while IFS= read -r line; do
if [[ $line =~ :5555[[:space:]]+device ]]; then
SERIAL=$(echo "$line" | awk '{print $1}')
FOUND_WIRELESS=true
echo -e "${GREEN}[INFO] Using existing wireless device $SERIAL${NC}"
"$ADB_PATH" connect "$SERIAL" >/dev/null 2>&1
break
fi
done < <("$ADB_PATH" devices)
# If no wireless connection, check for USB device
if [[ "$FOUND_WIRELESS" == false ]]; then
echo -e "${CYAN}[INFO] Checking for USB devices...${NC}"
while IFS= read -r line; do
if [[ $line =~ device$ ]]; then
SERIAL=$(echo "$line" | awk '{print $1}')
echo -e "${GREEN}[INFO] USB device detected: $SERIAL${NC}"
break
fi
done < <("$ADB_PATH" devices)
fi
# If no device found
if [[ -z "$SERIAL" ]]; then
echo -e "${YELLOW}[WARNING] No device detected over USB or wireless.${NC}"
echo -e "${GRAY}➡ If you used this before, just ensure the phone stayed on the same WiFi and try again.${NC}"
echo -e "${GRAY}➡ Otherwise, connect via USB once to initialize wireless mode.${NC}"
read -p "Press Enter to continue..."
exit 1
fi
# If USB device found, enable wireless
if [[ "$FOUND_WIRELESS" == false ]]; then
echo -e "${CYAN}[INFO] Enabling TCP/IP mode (port 5555)...${NC}"
"$ADB_PATH" tcpip 5555 >/dev/null 2>&1
sleep 2
# Get device IP using multiple strategies
echo -e "${CYAN}[INFO] Detecting phone IP address...${NC}"
IP=""
# Method 1: Android properties (most reliable)
echo -e "${CYAN}[INFO] Trying Android properties method...${NC}"
for prop in dhcp.wlan0.ipaddress dhcp.eth0.ipaddress dhcp.wlan1.ipaddress; do
if [[ -z "$IP" ]]; then
IP=$("$ADB_PATH" shell "getprop $prop 2>/dev/null")
if [[ -n "$IP" ]] && [[ "$IP" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo -e "${GREEN}[SUCCESS] IP found via property $prop: $IP${NC}"
break
else
IP=""
fi
fi
done
# Method 2: Network interfaces
if [[ -z "$IP" ]]; then
echo -e "${CYAN}[INFO] Trying network interface method...${NC}"
for interface in wlan0 eth0 wlan1 rmnet_data0; do
if [[ -z "$IP" ]]; then
IP=$("$ADB_PATH" shell "ip -f inet addr show $interface 2>/dev/null | grep -o 'inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | awk '{print \$2}' | head -1")
if [[ -n "$IP" ]]; then
echo -e "${GREEN}[SUCCESS] IP found via interface $interface: $IP${NC}"
break
fi
fi
done
fi
# Method 3: Route-based detection
if [[ -z "$IP" ]]; then
echo -e "${CYAN}[INFO] Trying route-based method...${NC}"
IP=$("$ADB_PATH" shell "ip route 2>/dev/null | grep -o 'src [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | awk '{print \$2}' | head -1")
if [[ -n "$IP" ]]; then
echo -e "${GREEN}[SUCCESS] IP found via route: $IP${NC}"
fi
fi
# Method 4: Compact addr list
if [[ -z "$IP" ]]; then
echo -e "${CYAN}[INFO] Trying compact address method...${NC}"
IP=$("$ADB_PATH" shell "ip -o -4 addr show 2>/dev/null | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | head -1")
if [[ -n "$IP" ]]; then
echo -e "${GREEN}[SUCCESS] IP found via compact method: $IP${NC}"
fi
fi
if [[ -z "$IP" ]]; then
echo -e "${RED}[ERROR] Could not detect phone IP address.${NC}"
echo -e "${GRAY}➡ Ensure your phone is connected to WiFi.${NC}"
echo -e "${GRAY}➡ You may need to edit the script (replace wlan0 with eth0).${NC}"
read -p "Press Enter to continue..."
exit 1
fi
echo -e "${GREEN}[INFO] Phone IP detected: $IP${NC}"
# Connect wirelessly
SERIAL="$IP:5555"
echo -e "${CYAN}[INFO] Connecting wirelessly to $SERIAL...${NC}"
CONNECT_OUTPUT=$("$ADB_PATH" connect "$SERIAL" 2>&1)
if echo "$CONNECT_OUTPUT" | grep -qi "failed\|unable\|cannot"; then
echo -e "${RED}[ERROR] Could not connect wirelessly.${NC}"
echo -e "${GRAY}➡ Ensure PC and phone are on the same WiFi network.${NC}"
echo -e "${GRAY}➡ Reconnect USB once and re-run this script.${NC}"
read -p "Press Enter to continue..."
exit 1
fi
fi
# Launch scrcpy
echo -e "${GREEN}[INFO] Starting scrcpy...${NC}"
echo
echo -e "${YELLOW}[NOTE] If you restart your PC or kill ADB,${NC}"
echo -e "${YELLOW}you must reconnect your phone via USB once to restore wireless mode.${NC}"
echo
echo -e "${RED}Do not close this window.!!!${NC}"
echo
echo -e "${YELLOW}Wireless connection is now active and stable.${NC}"
echo
echo -e "${GREEN}Use the Desktop shortcut 'SCRCPY Wireless Launcher' for everyday use. Happy journey!${NC}"
echo
# Launch scrcpy with the detected serial
"$SCRCPY_PATH" -s "$SERIAL" --verbosity=error
# Keep terminal open if scrcpy exits
echo
echo -e "${YELLOW}[NOTE] scrcpy has exited. You can close this window.${NC}"
read -p "Press Enter to continue..."
}
# Run main function
main "$@"