Question

I have problems when I when to send messages via USB from the board to the devicersa.

Hardware:

  1. Arduino ADK 2011
  2. Samsung Galaxy S3, Android 4.1.2

The problem is the read method in the Android app never terminates and makes the thread get stuck:

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;
                    }
                } ...

The connection works fine since I use the special USB-library. When I connect the device the app opens automatically very well. But with logs I see it never passes the read command. Also the Arduinio monitor says that:

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

The ADK sends messages, to the device in the loop (once):

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);
Was it helpful?

Solution

Now I figured out the problem might be the disconnect. Immedeately after running through the first loop in the Arduiino code it prints disconnect to the monitor. The code in the libraries of AndroidAccessory.cpp is:

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;
}

So in the second loop this method returns false, even though the smartphone is still connected via usb. Do you know why it thinks it is dosconnected after the first loop iteration?

Thanks, FL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top