手机通过usb线连接pc,实现pc主动向手机发送消息

11 篇文章 0 订阅
手机通过usb线连接pc,实现pc主动向手机发送消息;

 

一、获取与电脑连接的所有手机

 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static List<Device> findDevices(){  
  2.         List<Device>devices = new ArrayList<Device>();  
  3.         String str = null;  
  4.         int port = 15000;  
  5.         Process process = null;  
  6.         Device device = null;    
  7.         String[] deviceStr = new String[2];  
  8.         List<String>lines=new ArrayList<String>();  
  9.         try {  
  10.             process = Runtime.getRuntime().exec("adb devices");  
  11.             InputStream in = process.getInputStream();  
  12.             BufferedReader read=new BufferedReader(new InputStreamReader(in));  
  13.               
  14.              while ((str=read.readLine())!=null){  
  15.                  lines.add(str);    
  16.                  System.out.println(str);  
  17.              }  
  18.              for(int i=1;i<lines.size()-1;i++){  
  19.                  str = lines.get(i);  
  20.                  deviceStr = str.split("    ");  
  21.                  if ("device".equals(deviceStr[1])) {  
  22.                     device = new Device();  
  23.                     device.setDeviceId(deviceStr[0]);  
  24.                     device.setState(deviceStr[1]);  
  25.                     // 目前先指定port;  
  26.                     device.setPort(port);  
  27.                     port++;  
  28.                     devices.add(device);  
  29.                 }  
  30.              }  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.           
  35.         return devices;  
  36.     }  

二、端口映射;启动手机端的app

 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void portForwardBydevice(Devices device){  
  2.         String a = null;  
  3.         String b = null;  
  4.         String c = null;  
  5.         String d = null;  
  6.         Log logger = LogFactory.getLog(DeviceFinder.class);  
  7.           
  8.             a = "adb -s " + device.getDeviceId()+ " shell am broadcast -a NotifyServiceStop";  
  9.             b = "adb -s " + device.getDeviceId() + " forward tcp:"+device.getPort()+" tcp:12222";  
  10.             c = "adb -s " + device.getDeviceId()+ " shell am broadcast -a NotifyServiceStart";  
  11.             d= "adb -s " + device.getDeviceId()+ " shell am start -n com.newland.realmobiledetection/com.newland.realmobiledetection.system.activity.WelcomeActivity";  
  12.             logger.error("......device...a.."+a);  
  13.             logger.error("......device...b.."+b);  
  14.             logger.error("......device...c.."+c);  
  15.             logger.error("......device...d.."+d);  
  16.             try {  
  17.                 Runtime.getRuntime().exec(d);                 
  18.                 Thread.sleep(1000);   
  19.                 Runtime.getRuntime().exec(a);                 
  20.                 Thread.sleep(1000);               
  21.                 Runtime.getRuntime().exec(b);  
  22.                 Thread.sleep(1000);  
  23.                 Runtime.getRuntime().exec(c);  
  24.                 Thread.sleep(1000);  
  25.                 logger.error("端口映射完成。。");  
  26.             } catch (IOException e) {  
  27.                   
  28.                 logger.error("与手机通信异常"+e.getMessage());               
  29.             }catch (InterruptedException e) {  
  30.                 logger.error("线程中断异常"+e.getMessage());  
  31.             }  
  32.               
  33.         }  
  34.       

 

三、连接:

 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. try {    
  2.             InetAddress serverAddr = null;    
  3.             serverAddr = InetAddress.getByName("127.0.0.1");   
  4.             socket = new Socket(serverAddr, pcPort);    
  5.             socket.setKeepAlive(true);   
  6.             socket.setSoTimeout(120 * 1000);  
  7.             BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());    
  8.             BufferedInputStream in = new BufferedInputStream(socket.getInputStream());      
  9.             out.write(requestContent.getBytes());    
  10.             out.flush();    
  11.             responseContent=readFromSocket(in);  
  12.             System.out.println(":::::::::::::receive::::::::::::" + responseContent);  
  13.             logger.info(":::::::::::::receive::::::::::::" +responseContent);  
  14.             obj = gson.fromJson(responseContent, method.getGenericReturnType());  

手机端app);
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. serverSocket =new ServerScoket(12222);  
  2.   
  3. Socket socket = serverSocket.accept();  
  4.   
  5. out = new BufferedOutputStream(socket.getOutputStream());  
  6.   in = new BufferedInputStream(socket.getInputStream()); 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值