• 大小: 373KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-07
  • 语言: Java
  • 标签: Android  Socket  USB  

资源简介

Android手机和电脑通USB线连接,手机里面的app作为Socket的服务器端,PC作为Socket的客户端,两者进行通信。

资源截图

代码片段和文件信息

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 测试usb与pc通信 通过adb端口转发方式
 * 
 * @author chl
 * 
 */
public class testPcClient {

public static void main(String[] args) throws InterruptedIOException {
try {
// adb 指令
Runtime.getRuntime().exec(
“adb shell am broadcast -a NotifyServiceStop“);
Thread.sleep(3000);
Runtime.getRuntime().exec(“adb forward tcp:12580 tcp:10086“); // 端口转换
Thread.sleep(3000);
Runtime.getRuntime().exec(
“adb shell am broadcast -a NotifyServiceStart“);
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();

}
Socket socket = null;
try {
InetAddress serveraddr = null;
serveraddr = InetAddress.getByName(“127.0.0.1“);
System.out.println(“TCP 1111“ + “C: Connecting...“);
socket = new Socket(serveraddr 12580);
String str = “hichenhl“;
System.out.println(“TCP 221122“ + “C:RECEIVE“);
BufferedOutputStream out = new BufferedOutputStream(
socket.getOutputStream());
BufferedInputStream in = new BufferedInputStream(
socket.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
boolean flag = true;
while (flag) {
System.out.print(“请输入1~6的数字退出输入exit:“);
String strWord = br.readLine();// 从控制台输入1~6
if (strWord.equals(“1“)) {
out.write(“1“.getBytes());
out.flush();
System.out.println(“1 finish sending the data“);
String strFormsocket = readFromSocket(in);
System.out.println(“the data sent by server is:/r/n“
+ strFormsocket);
System.out
.println(“=============================================“);
}else if(strWord.equals(“2“)){
out.write(“2“.getBytes());  
                    out.flush();  
                    System.out.println(“2 finish sending the data“);  
                    String strFormsocket = readFromSocket(in);  
                    System.out.println(“the data sent by server is:/r/n“  
                            + strFormsocket);  
                    System.out  
                            .println(“=============================================“);  
}else if(strWord.equals(“3“)){
out.write(“3“.getBytes());  
                    out.flush();  
                    System.out.println(“3 finish sending the data“);  
                    String strFormsocket = readFromSocket(in);  
                    System.out.println(“the data sent by server is:/r/n“  
                            + strFormsocket);  
                    System.out  
                            .println(“=============================================“); 
}else if(strWord.equals(“4“)){
/* 发送命令 */  
                    out.write(“4“.getBytes());  
                    out.fl

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-07-23 07:04  手机通过usb与pc通信\
     目录           0  2013-07-23 07:03  手机通过usb与pc通信\testUsbConnect\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\libs\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\src\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\src\com\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\menu\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values-v14\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values-v11\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\layout\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\drawable-ldpi\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\drawable-mdpi\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\drawable-hdpi\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\drawable-xhdpi\
     目录           0  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\src\com\chenhl\
     目录           0  2013-07-22 09:59  手机通过usb与pc通信\testUsbConnect\src\com\chenhl\testusbconnect\
     目录           0  2013-07-22 09:59  手机通过usb与pc通信\testUsbConnect\src\com\chenhl\testusbconnect\utils\
     文件          36  2013-07-23 07:04  手机通过usb与pc通信\readme.txt
     文件        6560  2013-07-23 06:18  手机通过usb与pc通信\testPcClient.java
     文件         563  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\project.properties
     文件         356  2013-07-22 09:55  手机通过usb与pc通信\testUsbConnect\.classpath
     文件        1326  2013-07-23 06:13  手机通过usb与pc通信\testUsbConnect\AndroidManifest.xml
     文件         781  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\proguard-project.txt
     文件         817  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\.project
     文件      385685  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\libs\android-support-v4.jar
     文件         249  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\menu\main.xml
     文件         380  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values-v14\styles.xml
     文件         323  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values-v11\styles.xml
     文件         221  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values\strings.xml
     文件         679  2013-07-22 09:54  手机通过usb与pc通信\testUsbConnect\res\values\styles.xml
............此处省略11个文件信息

评论

共有 条评论