尽管有claimedInterface
d,有正确的权限等,当 controlTransfer() 被调用时,它返回 -1。Logcat
显示没有什么有趣的。我使用此代码作为指南。
查看源代码,here和here,我知道设备已正确打开(getFileDescriptor() 返回正确的值)。这意味着 ioctl 调用失败。我无法访问 errno。
有任何想法吗?
private static UsbInterface getSerialInterface(UsbDevice device)
{
for (int i = device.getInterfaceCount() - 1; i >= 0; i--)
{
UsbInterface iface = device.getInterface(i);
if (iface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA)
return iface;
}
return null;
}
private byte[] getLineEncoding(int baudRate) {
final byte[] lineEncodingRequest = { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
if (baudRate != 9600)
throw new UnsupportedOperationException("Bad Baud rate.");
}
private boolean openSerialEndpoints(UsbDevice device)
{
mConnection = mManager.openDevice(device);
if (mConnection == null) return false;
UsbInterface iface = getSerialInterface(device);
if (iface == null) {
closeSerialEndpoints();
return false;
}
if (!mConnection.claimInterface(iface, true)) {
Log.e(TAG, "Claiming interface failed!");
closeSerialEndpoints();
return false;
}
Log.d(TAG, "controlTransfer.");
if (mConnection.getFileDescriptor() == -1)
Log.d(TAG, "NOt Opened!!");
// Arduino setup.
if (mConnection.controlTransfer(0x21, 0x22, 0, 0, null, 0, 0) <= 0) {
Log.d(TAG, "Control transfer 1 failed."); // This fails
return false;
}
if (mConnection.controlTransfer(0x21, 0x20, 0, 0, getLineEncoding(9600), 7, 0) <= 0) {
Log.d(TAG, "Control transfer 2 failed.");
return false;
}
}