PC客户端与Android服务端的Socket同步通信(2)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

PC客户端与Android服务端的Socket同步通信(USB)1.一个android端的service后台运行的程序,作为socket的服务器端;用于接收Pcclient端发来的命令,来处理数据后,把结果发给PCclient2.PC端程序,作为socket的客户端,用于给android手机端发操作命令难点分析:1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。不过,我发现LGGW880就没有,要去下载个2.android默认手机端的IP为“127.0.0.1”3.要想联通PC与android手机的sokcet,一定要用adbforward来作下端口转发才能连上socket.viewplaincopytoclipboardprint?Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);Thread.sleep(3000);Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);Thread.sleep(3000);4.android端的service程序Install到手机上容易,但是还要有方法来从PC的client端来启动手机上的service,这个办法可以通过PC端adb命令来发一个Broastcast,手机端再写个接收BroastcastReceive来接收这个Broastcast,在这个BroastcastReceive来启动servicepc端命令:viewplaincopytoclipboardprint?Runtime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStart);Runtime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStart);android端的代码:ServiceBroadcastReceiver.javaviewplaincopytoclipboardprint?packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log;publicclassServiceBroadcastReceiverextendsBroadcastReceiver{privatestaticStringSTART_ACTION=NotifyServiceStart;privatestaticStringSTOP_ACTION=NotifyServiceStop;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.d(androidService.TAG,Thread.currentThread().getName()+----+ServiceBroadcastReceiveronReceive);Stringaction=intent.getAction();if(START_ACTION.equalsIgnoreCase(action)){context.startService(newIntent(context,androidService.class));Log.d(androidService.TAG,Thread.currentThread().getName()+----+ServiceBroadcastReceiveronReceivestartend);}elseif(STOP_ACTION.equalsIgnoreCase(action)){context.stopService(newIntent(context,androidService.class));Log.d(androidService.TAG,Thread.currentThread().getName()+----+ServiceBroadcastReceiveronReceivestopend);}}}packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log;publicclassServiceBroadcastReceiverextendsBroadcastReceiver{privatestaticStringSTART_ACTION=NotifyServiceStart;privatestaticStringSTOP_ACTION=NotifyServiceStop;@OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.d(androidService.TAG,Thread.currentThread().getName()+----+ServiceBroadcastReceiveronReceive);Stringaction=intent.getAction();if(START_ACTION.equalsIgnoreCase(action)){context.startService(newIntent(context,androidService.class));Log.d(androidService.TAG,Thread.currentThread().getName()+----+ServiceBroadcastReceiveronReceivestartend);}elseif(STOP_ACTION.equalsIgnoreCase(action)){context.stopService(newIntent(context,androidService.class));Log.d(androidService.TAG,Thread.currentThread().getName()+----+ServiceBroadcastReceiveronReceivestopend);}}}5.由于是USB连接,所以socket就可以设计为一但连接就一直联通,即在newsocket和开完out,in流后,就用个while(true){}来循环PC端和android端的读和写android的代码:viewplaincopytoclipboardprint?publicvoidrun(){Log.d(androidService.TAG,Thread.currentThread().getName()+----+aclienthasconnectedtoserver!);BufferedOutputStreamout;BufferedInputStreamin;try{/*PC端发来的数据msg*/StringcurrCMD=;out=newBufferedOutputStream(client.getOutputStream());in=newBufferedInputStream(client.getInputStream());//testSocket();//测试socket方法androidService.ioThreadFlag=true;while(androidService.ioThreadFlag){try{if(!client.isConnected()){break;}/*接收PC发来的数据*/Log.v(androidService.TAG,Thread.currentThread().getName()+----+willread......);/*读操作命令*/currCMD=readCMDFromSocket(in);Log.v(androidService.TAG,Thread.currentThread().getName()+----+**currCMD====+currCMD);/*根据命令分别处理数据*/if(currCMD.equals(1)){out.write(OK.getBytes());out.flush();}elseif(currCMD.equals(2)){out.write(OK.getBytes());out.flush();}elseif(currCMD.equals(3)){out.write(OK.getBytes());out.flush();}elseif(currCMD.equals(4)){/*准备接收文件数据*/try{out.write(servicereceiveOK.getBytes());out.flush();}catch(IOExceptione){e.printStackTrace();}/*接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据*/byte[]filelength=newbyte[4];byte[]fileformat=newbyte[4];byte[]filebytes=null;/*从socket流中读取完整文件数据*/filebytes=receiveFileFromSocket(in,out,filelength,fileformat);//Log.v(Service139.TAG,receivedata=+new//String(filebytes));try{/*生成文件*/Filefile=FileHelper.newFile(R0013340.JPG);FileHelper.writeFile(file,filebytes,0,filebytes.length);}catch(IOExceptione){e.printStackTrace();}}elseif(currCMD.equals(exit)){}}catch(Exceptione){//try{//out.write(error.getBytes(utf-8));//out.flush();//}catch(IOExceptione1){//e1.printStackTrace();//}Log.e(androidService.TAG,Thread.currentThread().getName()+----+readwriteerror111111);}}out.close();in.close();}catch(Exceptione){Log.e(androidService.TAG,Thread.currentThread().getName()+----+readwriteerror222222);e.printStackTrace();}finally{try{if(client!=null){Log.v(androidService.TAG,Thread.currentThread().getName()+----+client.close());client.close();}}catch(IOExceptione){Log.e(androidService.TAG,Thread.currentThread().getName()+----+readwriteerror333333);e.printStackTrace();}}publicvoidrun(){Log.d(androidService.TAG,Thread.currentThread().getName(

1 / 37
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功