0

当我何时通过 USB 将消息从电路板发送到 devicersa 时,我遇到了问题。

硬件:

  1. Arduino ADK 2011
  2. 三星盖乐世 S3,安卓 4.1.2

问题是 Android 应用程序中的 read 方法永远不会终止并使线程卡住:

mFileDescriptor = mUsbManager.openAccessory(accessory);
    if (mFileDescriptor != null) {
        mAccessory  = accessory;
        FileDescriptor fd = mFileDescriptor.getFileDescriptor();
        Log.d(TAG, "openAccessory(): FileDescriptor instanciated. valid " + fd.valid());
        mInputStream = new FileInputStream(fd);
        mOutputStream = new FileOutputStream(fd);

        new Thread(null, new Runnable() {
            @Override
            public void run() {
                int ret = 0;
                byte[] buffer = new byte[255];
                while (ret >= 0 && mInputStream != null) {
                    try {
                        // never terminates
                        ret = mInputStream.read(buffer);
                    } catch (IOException e) {
                        Log.e(TAG, "openAccessory(): Could not read inputStream: " + e);
                        e.printStackTrace();
                        break;
                    }
                } ...

由于我使用了特殊的 USB 库,因此连接工作正常。当我连接设备时,应用程序会很好地自动打开。但是对于日志,我看到它永远不会通过读取命令。Arduino监视器还说:

Device addressed... Requesting device descriptor.
found possible device. swithcing to serial mode
device supports protcol 1 or above
found android acessory device
config desc
interface desc
interface desc
2
4
Sending message...
Done
disconnect

ADK 向循环中的设备发送消息(一次):

sntmsg[0] = COMMAND_TEXT;
sntmsg[1] = TARGET_DEFAULT;
sntmsg[2] = 25;
for (int i = 0; i < 25; i++) {
  sntmsg[3 + i] = hello[i];
}
// schreiben (buffer, length)
Serial.println("Sending message...");
acc.write(sntmsg, 3 + 25);
Serial.println("Done");
done = true;
delay(250);
4

1 回答 1

0

现在我发现问题可能是断开连接。运行完 Arduiino 代码中的第一个循环后,它会立即打印断开与监视器的连接。AndroidAccessory.cpp 的库中的代码是:

bool AndroidAccessory::isConnected(void)
{
    USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff;
    byte err;

    max.Task();
    usb.Task();

    if (!connected &&
        usb.getUsbTaskState() >= USB_STATE_CONFIGURING &&
        usb.getUsbTaskState() != USB_STATE_RUNNING) {
        Serial.print("\nDevice addressed... ");
        Serial.print("Requesting device descriptor.\n");

        err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc);
        if (err) {
            Serial.print("\nDevice descriptor cannot be retrieved. Trying again\n");
            return false;
        }

        if (isAccessoryDevice(devDesc)) {
            Serial.print("found android acessory device\n");

            connected = configureAndroid();
        } else {
            Serial.print("found possible device. swithcing to serial mode\n");
            switchDevice(1);
        }
    } else if (usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
        if (connected)
            Serial.println("disconnect\n");
        connected = false;
    }

    return connected;
}

因此,在第二个循环中,此方法返回 false,即使智能手机仍通过 USB 连接。你知道为什么它认为它在第一次循环迭代后是 dosconnected 吗?

谢谢,佛罗里达州

于 2012-12-31T16:07:38.437 回答