Java over USB で Android デバイスから PC にオブジェクト/文字列を送信したいと思います。可能です?基本的に、USB 経由のクライアント/サーバーです。
実際、エミュレーターを使用すると、特別な 10.0.2.2 アドレスを介してサーバー ソケットにアクセスできます。Wi-Fi経由で、192.168.1.Xのアドレスでアクセスできます。そして、USB経由で、それはどのように機能しますか?
thisによると、可能だと思いますが、python ではなく Java でサンプル コードを実行するにはどうすればよいですか? 何か案は?
private Byte[] bytes
private static int TIMEOUT = 5000;
private boolean forceClaim = true
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()){
UsbDevice device = deviceIterator.next()
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
connection.claimInterface(intf, forceClaim);
bytes = toByteArray("any path");
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);
}
public static byte[] toByteArray(File file) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean threw = true;
InputStream in = new FileInputStream(file);
try {
byte[] buf = new byte[BUF_SIZE];
long total = 0;
while (true) {
int r = in.read(buf);
if (r == -1) {
break;
}
out.write(buf, 0, r);
}
threw = false;
} finally {
try {
in.close();
} catch (IOException e) {
if (threw) {
log.warn("IOException thrown while closing", e);
} else {
throw e;
}
}
}
return out.toByteArray();
}