PC 与 Android(安卓)的adb同步通信(一)
16lz
2021-01-26
公司只能上csdn,作为中转站传资料。原文地址http://www.yidin.net/discuz/forum.php?mod=viewthread&tid=172&extra=page%3D1
1.一个android端的service后台运行的程序,作为socket的服务器端;用于接收Pc client端发来的命令,来处理数据后,把结果发给PC client
2.PC端程序,作为socket的客户端,用于给android手机端发操作命令
难点分析:
1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。
不过,我发现LG GW880就没有,要去下载个
2.android默认手机端的IP为“127.0.0.1”
3.要想联通PC与android手机的sokcet,一定要用adb forward 来作下端口转发才能连上socket.
- Runtime.getRuntime().exec("adb forward tcp:12580 tcp:10086");
- Thread.sleep(3000);
pc端命令:
- Runtime.getRuntime().exec(
- "adb shell am broadcast -a NotifyServiceStart");
-
- package eoe.otheri.service;
-
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
-
- public class ServiceBroadcastReceiver extends BroadcastReceiver {
- private static String START_ACTION = "NotifyServiceStart";
- private static String STOP_ACTION = "NotifyServiceStop";
-
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.d(androidService.TAG, Thread.currentThread().getName() + "---->"
- + "ServiceBroadcastReceiver onReceive");
- String action = intent.getAction();
- if (START_ACTION.equalsIgnoreCase(action)) {
- context.startService(new Intent(context, androidService.class));
- Log.d(androidService.TAG, Thread.currentThread().getName() + "---->"
- + "ServiceBroadcastReceiver onReceive start end");
- } else if (STOP_ACTION.equalsIgnoreCase(action)) {
- context.stopService(new Intent(context, androidService.class));
- Log.d(androidService.TAG, Thread.currentThread().getName() + "---->"
- + "ServiceBroadcastReceiver onReceive stop end");
- }
- }
-
-
- }
android的代码:
-
- public void run() {
- Log.d(androidService.TAG, Thread.currentThread().getName() + "---->"
- + "a client has connected to server!");
- BufferedOutputStream out;
- BufferedInputStream in;
- try {
- /* PC端发来的数据msg */
- String currCMD = "";
- out = new BufferedOutputStream(client.getOutputStream());
- in = new BufferedInputStream(client.getInputStream());
- // testSocket();// 测试socket方法
- androidService.ioThreadFlag = true;
- while (androidService.ioThreadFlag) {
- try {
- if (!client.isConnected()) {
- break;
- }
-
-
- /* 接收PC发来的数据 */
- Log.v(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "will read......");
- /* 读操作命令 */
- currCMD = readCMDFromSocket(in);
- Log.v(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "**currCMD ==== " + currCMD);
-
-
- /* 根据命令分别处理数据 */
- if (currCMD.equals("1")) {
- out.write("OK".getBytes());
- out.flush();
- } else if (currCMD.equals("2")) {
- out.write("OK".getBytes());
- out.flush();
- } else if (currCMD.equals("3")) {
- out.write("OK".getBytes());
- out.flush();
- } else if (currCMD.equals("4")) {
-
-
- /* 准备接收文件数据 */
- try {
- out.write("service receive OK".getBytes());
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- /* 接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据 */
- byte[] filelength = new byte[4];
- byte[] fileformat = new byte[4];
- byte[] filebytes = null;
-
-
- /* 从socket流中读取完整文件数据 */
- filebytes = receiveFileFromSocket(in, out, filelength,fileformat);
- // Log.v(Service139.TAG, "receive data =" + new
- // String(filebytes));
- try {
-
-
- /* 生成文件 */
- File file = FileHelper.newFile("R0013340.JPG");
- FileHelper.writeFile(file, filebytes, 0,
- filebytes.length);
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if (currCMD.equals("exit")) {
- }
- } catch (Exception e) {
- // try {
- // out.write("error".getBytes("utf-8"));
- // out.flush();
- // } catch (IOException e1) {
- // e1.printStackTrace();
- // }
- Log.e(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "read write error111111");
- }
- }
- out.close();
- in.close();
- } catch (Exception e) {
- Log.e(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "read write error222222");
- e.printStackTrace();
- } finally {
- try {
- if (client != null) {
- Log.v(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "client.close()");
- client.close();
- }
- } catch (IOException e) {
- Log.e(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "read write error333333");
- e.printStackTrace();
- }
- }
android端接收数据的代码:
- /**
- * 功能:从socket流中读取完整文件数据
- *
- * InputStream in:socket输入流
- *
- * byte[] filelength: 流的前4个字节存储要转送的文件的字节数
- *
- * byte[] fileformat:流的前5-8字节存储要转送的文件的格式(如.apk)
- *
- * */
- public static byte[] receiveFileFromSocket(InputStream in,
- OutputStream out, byte[] filelength, byte[] fileformat) {
- byte[] filebytes = null;// 文件数据
- try {
- int filelen = MyUtil.bytesToInt(filelength);// 文件长度从4字节byte[]转成Int
- String strtmp = "read file length ok:" + filelen;
- out.write(strtmp.getBytes("utf-8"));
- out.flush();
- filebytes = new byte[filelen];
- int pos = 0;
- int rcvLen = 0;
- while ((rcvLen = in.read(filebytes, pos, filelen - pos)) > 0) {
- pos += rcvLen;
- }
- Log.v(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "read file OK:file size=" + filebytes.length);
- out.write("read file ok".getBytes("utf-8"));
- out.flush();
- } catch (Exception e) {
- Log.v(androidService.TAG, Thread.currentThread().getName()
- + "---->" + "receiveFileFromSocket error");
- e.printStackTrace();
- }
- return filebytes;
- }
9.还要让服务器端可以同时连接多个client,即服务器端用new thread()来作数据读取操作。
更多相关文章
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
- 一句话锁定MySQL数据占用元凶
- 程序代码删除联系人的分组
- 安卓串口中InputStream数据接收不完整
- 百度地图 - Android所有Marker点显示在屏幕内
- Android中的Data Binding初探 (一)
- Android(安卓)进阶——代码插桩必知必会之初识ASM7字节码操作库
- 好用的 Android(安卓)数据库 DBFlow
- android安装和卸载的adb命令