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; }
public void shutdown() { if (DEBUG_VERBOSE) Log.d(TAG, "shutdown entered!"); // Kill the A3PSession and wait for it to stop if (a3pSession != null) { Log.d(TAG, "Ending A3PSession and waiting for it to stop"); a3pSession.endConnection(); while (!a3pSession.isClosed()) { try { synchronized (this) { this.wait(100); } } catch (InterruptedException e) { // Do nothing if interrupted. } } } if (workerThread != null) { Log.d(TAG, "Ending worker thread and waiting for it to stop"); // Kill the worker thread and wait for it to stop workerRun = false; workerThread.interrupt(); while (workerThread.isAlive()) { try { synchronized (this) { this.wait(100); } } catch (InterruptedException e) { // Do nothing if interrupted. } } } closeMyAccessory(); if (DEBUG_VERBOSE) Log.d(TAG, "shutdown exited!"); }