private boolean initA3P() { boolean a3pInited = false; if (a3pSession != null && a3pSession.isConnected()) { // Session is still connected, do nothing Log.d(TAG, "Session still connected. Not reconnecting."); return true; } // If we already have an A3PSession, close it before making a new one // Also, clear out previous device ID list if (a3pSession != null) { Log.d(TAG, "Closing previous A3PSession!"); a3pSession.endConnection(); while (!a3pSession.isClosed()) { try { synchronized (this) { this.wait(100); } } catch (InterruptedException e) { // Do nothing if interrupted. } } deviceIDs.clear(); } if (myParcelFD == null) { if (openMyAccessory()) { // Start or restart an A3PSession if (a3pSession != null) { Log.d(TAG, "Copying queues from old A3P Session!"); a3pSession = A3PSession.freshCopy(a3pSession, myParcelFD); } else { Log.d(TAG, "Starting first session!"); a3pSession = new A3PSession(this, myParcelFD); } // Attempt to initiate connection a3pSession.startConnection(); a3pInited = true; } else { Log.e(TAG, "Cannot create A3PSession. openMyAccessory failed!"); a3pInited = false; } } return a3pInited; }
// Initialize the USB private void initUSB() { if (initA3P()) { initThread(); int retries = 0; while (++retries < MAX_RETRIES_FOR_CONN_READY) { if (a3pSession.isConnected()) { Log.d(TAG, "USB ready for communication"); usbInited = true; break; } try { Thread.sleep(500); } catch (InterruptedException iex) { } } } }