private int waitForConnect(HeadsetBase headset) { // Try to connect for 20 seconds int result = 0; for (int i = 0; i < 40 && result == 0; i++) { // waitForAsyncConnect returns 0 on timeout, 1 on success, < 0 on error. result = headset.waitForAsyncConnect(500, mConnectedStatusHandler); if (isInterrupted()) { headset.disconnect(); return EINTERRUPT; } } return result; }