Android笔记之adb命令应用实例1(手机端与PC端socket通讯下)

通过adb和Android通讯需要引用adb相关的组件到项目中,分别为:adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll。

可以在XXX\sdk\platform-tools目录下找到

界面效果如下:

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
namespace AndUsbClient
{
    public partial class Form1 : Form
    {
        public const int WaitMs = 2000;
        public const int TimeOut = 5000;
 
        private readonly TcpClient _tcpClient = new TcpClient();
        private Boolean _flag = true;
        private Thread _thread;
 
        public Form1()
        {
            InitializeComponent();
            btnConnect.Click += btnConnect_Click;
            FormClosed += new FormClosedEventHandler(Form1_FormClosed);
            btnSend.Click += btnSend_Click;
            btnClear.Click += btnClear_Click;
        }
 
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (_tcpClient.Connected)
            {
                var writer = new BinaryWriter(_tcpClient.GetStream());
                String content = rtxtContent.Text;
                try
                {
                    byte[] bytes = new UTF8Encoding(false).GetBytes(content);
                    writer.Write(Int32ToByteArray(bytes.Length));
                    writer.Write(bytes);
                    writer.Flush();
                    SetResult(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " PC: " + content);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
 
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
        }
 
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                btnConnect.Enabled = false;
                if (_thread != null && _thread.IsAlive)
                {
                    _thread.Abort();
                    _thread = null;
                }
                _thread = new Thread(() =>
                {
                    OutMsg(@"开始连接Android手机...");
                    DoConnect();
                });
                _thread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            btnConnect.Enabled = true;
        }
 
        private void OutMsg(String msg)
        {
            AsyncAction(() => { lblInfo.Text = msg; });
        }
 
        private void SetResult(String msg)
        {
            AsyncAction(() => { txtResult.Text += @"\r\n" + msg; });
        }
 
        public void AsyncAction(Action action)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<Action>(AsyncAction), action);
                return;
            }
            action();
        }
 
        //连接
        public void DoConnect()
        {
            try
            {
                ExecuteCmd("kill-server", 0);
                ExecuteCmd("devices", 0);
                string strCmd = "shell am broadcast -a NotifyServiceStop"; //利用adb shell am broadcast命令发送广播
                ExecuteCmd(strCmd, 0);
                strCmd = "forward tcp:13333 tcp:10101"; //利用adb命令转发端口
                ExecuteCmd(strCmd, 0);
                strCmd = "shell am broadcast -a NotifyServiceStart";
                ExecuteCmd(strCmd, 0);
                IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
                _tcpClient.Connect(ipaddress, 13333);
                OutMsg("连接成功!");
                NetworkStream networkkStream = _tcpClient.GetStream();
                networkkStream.ReadTimeout = TimeOut;
                networkkStream.WriteTimeout = TimeOut;
                //var reader = new BinaryReader(networkkStream);
                var tempbuffer = new byte[4];
                IAsyncResult result = networkkStream.BeginRead(tempbuffer, 0, tempbuffer.Length, null, tempbuffer);
                _flag = true;
                while (_flag)
                {
                    if (result.IsCompleted && _tcpClient.Connected)
                    {
                        tempbuffer = (byte[])result.AsyncState;
                        int length = ByteArrayToInt32(tempbuffer);
                        var buffer = new byte[length];
                        int size = networkkStream.Read(buffer, 0, length);
                        if (size > 0)
                        {
                            SetResult(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Android: " +
                                      Encoding.UTF8.GetString(buffer, 0, size));
                        }
                        result = networkkStream.BeginRead(tempbuffer, 0, tempbuffer.Length, null, tempbuffer);
                    }
                    Thread.Sleep(200);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 
        private static byte[] Int32ToByteArray(Int32 m)
        {
            var arry = new byte[4];
            arry[0] = (byte)(m & 0xFF);
            arry[1] = (byte)((m & 0xFF00) >> 8);
            arry[2] = (byte)((m & 0xFF0000) >> 16);
            arry[3] = (byte)((m >> 24) & 0xFF);
            return arry;
        }
 
        private static int ByteArrayToInt32(byte[] bytes)
        {
            return BitConverter.ToInt32(bytes, 0);
        }
 
        private String ExecuteCmd(string command, int seconds)
        {
            string output = ""; //输出字符串
            if (!String.IsNullOrEmpty(command))
            {
                Console.WriteLine(command);
                var process = new Process(); //创建进程对象
                var startInfo = new ProcessStartInfo();
                startInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                startInfo.FileName = "adb.exe"; //设定需要执行的命令
                startInfo.Verb = "runas";
                startInfo.Arguments = command; //“/C”表示执行完命令后马上退出
                startInfo.UseShellExecute = false; //不使用系统外壳程序启动
                startInfo.RedirectStandardInput = false; //不重定向输入
                startInfo.RedirectStandardOutput = true; //重定向输出
                startInfo.CreateNoWindow = true; //不创建窗口
                process.StartInfo = startInfo;
                Console.WriteLine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                try
                {
                    if (process.Start()) //开始进程
                    {
                        if (seconds == 0)
                        {
                            process.WaitForExit(); //这里无限等待进程结束
                        }
                        else
                        {
                            process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
                        }
                        output = process.StandardOutput.ReadToEnd(); //读取进程的输出
                        Console.WriteLine(output);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    process.Close();
                }
            }
            return output;
        }
 
        public string Obj2Json<T>(T data)
        {
            try
            {
                var json = new DataContractJsonSerializer(data.GetType());
                using (var ms = new MemoryStream())
                {
                    json.WriteObject(ms, data);
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        public object Json2Obj(string strJson, Type t)
        {
            try
            {
                var json = new DataContractJsonSerializer(t);
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson)))
                {
                    return json.ReadObject(ms);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
         
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            _flag = false;
            if (_tcpClient != null)
            {
                try
                {
                    if (_tcpClient.Connected)
                    {
                        NetworkStream stream = _tcpClient.GetStream();
                        var writer = new BinaryWriter(stream);
                        byte[] bytes = new UTF8Encoding(false).GetBytes("EXIT");
                        writer.Write(Int32ToByteArray(bytes.Length));
                        writer.Write(bytes);
                        writer.Flush();
                        stream.Close();
                        stream.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    _tcpClient.Close();
                }
            }
        }
    }
}

 

作者:sufish

出处:http://www.cnblogs.com/dotnetframework/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:dotnetframework@sina.com联系我,非常感谢。

posted @   Sufish  阅读(563)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示