1+ /*
2+ __ __ __ ______ __ ______ _____ ______
3+ /\ \ _ \ \ /\ \ /\ ___\ /\ \ /\ __ \ /\ __-. /\ == \
4+ \ \ \/ ".\ \ \ \ \ \ \ __\ \ \ \ \ \ __ \ \ \ \/\ \ \ \ __<
5+ \ \__/".~\_\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \____- \ \_____\
6+ \/_/ \/_/ \/_/ \/_/ \/_/ \/_/\/_/ \/____/ \/_____/
7+
8+ */
9+ package main
10+ import (
11+ "fmt"
12+ "os"
13+ "os/exec"
14+ "strings"
15+ "strconv"
16+ )
17+
18+ func execCommand (cmd string , args []string ) (output string , e error ){
19+ cmdOut , err := exec .Command (cmd , args ... ).Output ()
20+ return string (cmdOut ), err
21+ }
22+
23+ func log (a interface {}) {
24+ fmt .Println ("[WiFi ADB] " , a )
25+ }
26+
27+ func err (a interface {}, err error ) {
28+ if err != nil {
29+ fmt .Println ("[WiFi ADB] " , a , err )
30+ } else {
31+ fmt .Println ("[WiFi ADB] " , a )
32+ }
33+ }
34+
35+ func die (a interface {}, e error ) {
36+ err (a , e )
37+ os .Exit (1 )
38+ }
39+
40+ /**
41+ * check if there are any devices connected via `adb devcies`
42+ * prompt user to chose one if multi-device connected;
43+ */
44+ func selectDevice ()string {
45+ var (
46+ checkOutput string
47+ checkErr error
48+ )
49+ if checkOutput , checkErr = execCommand ("adb" , []string {"devices" }); checkErr != nil {
50+ die ("devices check failed" , checkErr )
51+ }
52+ devices := strings .Split (strings .Trim (checkOutput , "\n " ), "\n " )
53+ devicesCount := cap (devices )- 1
54+
55+ log ("device count " + strconv .Itoa (devicesCount ))
56+ var slectedDevice string
57+ if (devicesCount < 1 ) {
58+ die ("no device found" , nil )
59+ } else if (devicesCount > 1 ) {
60+ log ("find more than one device" )
61+ for i := 1 ; i <= devicesCount ; i ++ {
62+ log (strconv .Itoa (i )+ ":\t " + devices [i ])
63+ }
64+ fmt .Println ("please input device index as list:" )
65+ var inputDevice string
66+ fmt .Scanln (& inputDevice )
67+ var (
68+ index int
69+ e error
70+ )
71+ if index , e = strconv .Atoi (inputDevice ); e != nil {
72+ die ("inlaid input, please try again!!" , e )
73+ }
74+ if index <= 0 || index > devicesCount {
75+ die ("inlaid input, please try again!!" , nil )
76+ } else {
77+ slectedDevice = devices [index ]
78+ }
79+ } else {
80+ slectedDevice = devices [1 ]
81+ }
82+ spliteDevice := strings .Fields (slectedDevice )
83+ deviceId := spliteDevice [0 ]
84+ deviceType := spliteDevice [1 ]
85+ if deviceType != "device" {
86+ die (slectedDevice + " is not valid USB device" , nil )
87+ }
88+ if strings .Contains (deviceId , "emulator" ) {
89+ die (deviceId + " seems to be an emulator" , nil )
90+ }
91+ if len (strings .Split (deviceId , "." )) == 4 {
92+ die (deviceId + " seems to be connected via tcpip already" , nil )
93+ }
94+ return deviceId
95+ }
96+
97+ func main () {
98+ device := selectDevice ()
99+ log (device )
100+ var (
101+ cmdOut string
102+ err error
103+ )
104+ cmdName := "adb"
105+ cmdArgs := []string {"-s" , device , "shell" , " ip -f inet addr show wlan0" }
106+ /*
107+ 40: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
108+ inet 10.252.224.182/20 brd 10.252.239.255 scope global wlan0
109+ */
110+ if cmdOut , err = execCommand (cmdName , cmdArgs ); err != nil {
111+ // fmt.Fprintln(os.Stderr, "Execute adb shell failed: ", err)
112+ // os.Exit(1)
113+ die ("execute adb shell failed:" , err )
114+ }
115+ log (cmdOut )
116+ splited := strings .Split (cmdOut , "\n " )
117+ addres := strings .Split (strings .Trim (splited [1 ], " " ), " " );
118+ /*
119+ 10.252.224.182
120+ */
121+ ip := strings .Split (addres [1 ], "/" )[0 ]
122+ log ("device ip " + ip )
123+ if _ , e := execCommand ("adb" , []string {"-s" , device , "tcpip" , "5555" }); e != nil {
124+ die ("adb tcpip failed: " , err )
125+ }
126+ log ("unplugin USB then press ENTER:" )
127+ //adb connect 10.0.101.192
128+ var input string
129+ fmt .Scanln (& input )
130+ if _ , e := execCommand ("adb" , []string {"connect" , ip }); e != nil {
131+ die ("adb connect failed: " , err )
132+ }
133+ log ("device connected: " + ip + ":55555" )
134+ }
0 commit comments