我正在尝试通过 USB 将 PIC 微处理器连接到平板电脑应用程序。发送按钮应触发由 "$$$"
组成的字符串的发送。到 PIC。通过消除过程,它似乎一直到底部的“requestWait”行。该按钮保持突出显示,应用程序似乎挂起。我怀疑 requestWait 没有答案,所以它无限期地等待。我会很感激一些帮助。我已经查看了 AdbTest 和 Missile Launcher 应用程序,但对于我的 Android 专业知识水平来说,它们的内容太多了。
后来补充:
查看 UsbRequest queue(..)
的代码, result = native_queue_direct(...)
.
我认为这是 native 代码。请问我在哪里可以找到?
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.view.View;
import android.widget.Toast;
@Override
protected void onResume() {
super.onResume();
mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
// check for existing devices
UsbDevice device;
HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
device = deviceIterator.next();
if (device.getVendorId() == 1027) {
UsbInterface intf = device.getInterface(0);
if (setUsbInterface(device, intf)) {
break;
}
}
}
private UsbManager mManager;
private UsbDevice mDevice;
private UsbDeviceConnection mDeviceConnection;
private UsbInterface mInterface;
private ScorerDevice mScorerDevice ;
// ***********************************
// Sets the current USB device and interface
private boolean setUsbInterface(UsbDevice device, UsbInterface intf) {
if (mDeviceConnection != null) {
if (mInterface != null) {
mDeviceConnection.releaseInterface(mInterface);
mInterface = null;
}
mDeviceConnection.close();
mDevice = null;
mDeviceConnection = null;
}
if (device != null && intf != null) {
UsbDeviceConnection connection = mManager.openDevice(device);
if (connection != null) {
if (connection.claimInterface(intf, true)) {
toast = Toast.makeText(getApplicationContext(), "claim interface succeeded", Toast.LENGTH_SHORT);
toast.show();
mDevice = device;
mDeviceConnection = connection;
mInterface = intf;
mScorerDevice = new ScorerDevice(this, mDeviceConnection, intf);
toast = Toast.makeText(getApplicationContext(), "USB started", Toast.LENGTH_SHORT);
toast.show();
mScorerDevice.start();
return true;
} else {
toast = Toast.makeText(getApplicationContext(), "claim interface failed", Toast.LENGTH_SHORT);
toast.show();
connection.close();
}
} else {
toast = Toast.makeText(getApplicationContext(), "USB failed", Toast.LENGTH_SHORT);
toast.show();
}
}
if (mDeviceConnection == null && mScorerDevice != null) {
mScorerDevice.stop();
mScorerDevice = null;
}
return false;
}
// ***********************************
public int onClick_Send(View view) {
String dollars = "$$$";
byte[] bytes = dollars.getBytes();
int len = bytes.length;
int offset = 0;
int PacketSize = mScorerDevice.mEndpointOut.getMaxPacketSize();
if ((len > PacketSize) || (offset < 0) || (len < 0) || ((offset + len) > bytes.length))
throw new IndexOutOfBoundsException();
ByteBuffer sendBuffer = ByteBuffer.allocate(PacketSize);
UsbRequest request = new UsbRequest();
Boolean ret = request.initialize(mDeviceConnection, mScorerDevice.mEndpointOut);
if (!ret ) {
return -1;
}
ret = request.queue(sendBuffer, len);
if (!ret ) {
return -1;
}
request = mDeviceConnection.requestWait();
if (request == null) {
return -1;
}
}
最佳答案
这在大多数设备上是不可能的,因为它们不支持 USB 主机模式。有些设备有 USB OTG 支持或 USB 主机端口 ,但到目前为止,这些都是比较少见的设备。
理论上,您可以在 Win/Linux 上编写应用程序以通过 USB 访问手机,但反之则不行。
但是你可以Read This并尝试。
对于与设备通信,请参阅 - How to establish communication between android device and usb.
愿它有帮助...
关于android - 通过 USB 从 Android 应用程序发送和接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377410/