gpt4 book ai didi

java - 无法通过 USB 连接 Java 桌面应用程序和 Android 应用程序

转载 作者:行者123 更新时间:2023-12-02 01:27:00 25 4
gpt4 key购买 nike

我找不到任何通过 USB 连接 Android 应用和桌面应用的解决方案。

我需要通过 USB 将数据从我的桌面软件发送到 Android 应用程序。最好的解决方案允许我直接与我的 Android 应用程序通信以发送 JSON 对象,否则在智能手机上发送文件。

你有什么解决办法吗?

谢谢。

最佳答案

太好了,我终于使用了 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() method it's to set port, you need to embbed adb.exe and its dll

Android类

server = new ServerSocket(38300);
client = server.accept();

out = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());

来源:https://github.com/user370305/Android-USB-Communication

关于java - 无法通过 USB 连接 Java 桌面应用程序和 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com