2 回答
TA贡献1876条经验 获得超5个赞
太好了,我最终使用了 ADB,它运行良好。我可以将对象直接发送到我的 Android 应用程序。
桌面类
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import callbacks.MainCallbacks;
public class UsbConnect {
private Socket socket = null;
private ObjectOutputStream out = null;
private ObjectInputStream in = null;
private boolean logged = false;
private boolean play = true;
private MainCallbacks callbacks;
public UsbConnect(MainCallbacks callbacks) {
this.callbacks = callbacks;
new Thread(new Runnable() {
@Override
public void run() {
while(play) {
if(!logged) {
// Tentative d'ouverture du port
if(!setPort()) {
continue;
}
// Tentative de connexion
if(connexion()) {
callbacks.pdaConnect();
logged = true;
}
else {
continue;
}
}
else {
if(!testConnexion()) {
callbacks.pdaDisconnect();
logged = false;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
private void sendData(Object o) {
try {
out.writeObject(o);
out.flush();
} catch (IOException e) {
callbacks.pdaDisconnect();
logged = false;
}
}
private void destroy() {
play = false;
try {
in.close();
out.close();
if (socket != null)
{
socket.close();
}
} catch (IOException e) {
// NOTHING
}
}
private boolean testConnexion() {
try{
in = new ObjectInputStream(socket.getInputStream());
}
catch (IOException io){
return false;
}
return true;
}
protected boolean connexion() {
try{
socket = new Socket("127.0.0.1", 38300);
try{
out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
in = new ObjectInputStream(socket.getInputStream());
String message = (String) in.readObject();
System.out.println("server>" + message);
}
catch (IOException io){
return false;
}
return true;
}
catch (UnknownHostException e){
return false;
}
catch (IOException e){
return false;
}
catch (Exception e){
return false;
}
}
private boolean setPort() {
try {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("cmd.exe", "/c", "C:\\Users\\Code\\Desktop\\adb.exe forward tcp:38300 tcp:38300");
Process process;
process = processBuilder.start();
int exitVal = -12;
try {
exitVal=process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
if (exitVal == 0) {
return true;
} else {
return false;
}
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
}
}
当设备连接到 usb 端口时,我的类监听器,设置端口 adb,然后它尝试连接到智能手机上的套接字服务器。
setPort()方法是设置端口,需要嵌入adb.exe及其dll
安卓类
server = new ServerSocket(38300);
client = server.accept();
out = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
TA贡献1824条经验 获得超5个赞
据我所知,这只能通过 ADB(Android 调试桥)实现。您需要在您的 Android 设备上启用 USB 调试才能正常工作。此外,android adb 可执行文件必须存在于桌面 pc 上。(如果需要,您可以将其与桌面应用程序可执行文件一起发布。)
通信本身将通过 tcp 套接字进行。您的 android 应用程序必须侦听 tcp 端口,而您的桌面应用程序必须打开该端口上的连接。
为此,您必须通过此 adb 命令将 tcp 端口从桌面 pc 转发到 android 设备:
adb forward tcp:6100 tcp:7100
您可以根据需要更改端口。第一个是桌面 pc 上的端口,第二个是通信将转发到 android 设备上的端口。
添加回答
举报