1

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(); 
 }
4

1 に答える 1

0

私の提案は、テキストファイルを使用することです。

テキスト ファイルを SD カードに保存します。次に、PC Java プログラムでadb pullコマンドを実行します。

このようにして、電話機が USB 経由で PC に接続されている場合、ファイルは実行時に電話機から PC にプルされますadb pull

もちろん、PCにadbがあり、電話でUSBデバッグが許可されている必要があります。

お役に立てれば。

よろしく

于 2013-03-19T01:32:01.667 に答える