public List<DiscoverableDevice> getDiscoverableSensor() { List<DiscoverableDevice> deviceList = null; // Reinit the USB (if needed), then ask for a list of devices if (!usbInited) initUSB(); // If we have an a3pSession, try to get a device list from the ADK board if (a3pSession != null && !a3pSession.isClosed()) { deviceIDs.clear(); Log.d(TAG, "Sending enumerate sensors req"); int retries = 0; while (deviceIDs.size() == 0 && (retries < MAX_RETRIES_FOR_DISCOVERY)) { sendDeviceListRequest(); retries++; if (DEBUG_VERBOSE) Log.d(TAG, "blocking for notify. retry cnt: " + retries); synchronized (deviceIDs) { try { deviceIDs.wait(2000); Log.d(TAG, "woke up with " + deviceIDs.size()); } catch (InterruptedException iex) { iex.printStackTrace(); } } } if (deviceIDs.size() == 0) { Log.e(TAG, "Finding deviceIDs failed!"); Log.d(TAG, "<--- Exiting getDiscoverableSensor()"); return null; } Log.d(TAG, "Finding deviceIDs succeeded!"); deviceList = new ArrayList<DiscoverableDevice>(); Iterator<String> deviceListIterator = deviceIDs.iterator(); while (deviceListIterator.hasNext()) { String sensorID = deviceListIterator.next(); USBDiscoverableDevice newDiscoverableDevice = new USBDiscoverableDevice(sensorID, this.mSensorManager); newDiscoverableDevice.connectionLost = false; deviceList.add(newDiscoverableDevice); Log.d(TAG, "updating sensor state in sensormanager"); mSensorManager.updateSensorState(sensorID, DetailedSensorState.CONNECTED); Log.d(TAG, "sensorID " + sensorID + " set to connected in DB"); } this.mDiscoverableDeviceList.clear(); this.mDiscoverableDeviceList.addAll(deviceList); Log.d(TAG, deviceList.size() + " sensors found"); } else { Log.d(TAG, "a3pSession is null or closed. cannot discover sensors "); } Log.d(TAG, "<--- Exiting getDiscoverableSensor()"); return deviceList; }
public void sensorWrite(String id, byte[] message) { boolean sessionInited = true; if (id.equals(A3P_DEVICE_ID) || (mSensorManager.getSensor(id) != null)) { // TODO: Discuss whether reviving automatically is good or bad. // Try to revive a dead a3pSession before proceeding if (a3pSession == null || a3pSession.isClosed()) { sessionInited = initA3P(); } if (sessionInited) { long ts = Calendar.getInstance().getTimeInMillis(); USBPayload payload = new USBPayload(message, ts, Long.parseLong(id), false); try { a3pSession.enqueuePayloadToSend(payload); } catch (IllegalStateException e) { Log.e(TAG, "Connection is currently closed! Packet cannot be enqueued!"); e.printStackTrace(); } } else { Log.e(TAG, "initA3P failed! Packet cannot be enqueued!"); } } else { Log.d(TAG, "SensorID: " + id + " unknown. Packet cannot be enqueued!"); } }
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!"); }
// 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) { } } } }
public void run() { String LOG_TAG = ArduinoSubChannel.TAG + "worker"; while (workerRun) { if (a3pSession != null) { USBPayload nextPayload = a3pSession.getNextPayload(); if (nextPayload == null) { try { synchronized (this) { this.wait(100); } } catch (InterruptedException e) { // Do nothing for now. } } else { // Process the payload here! if (DEBUG_VERBOSE) { Log.d(LOG_TAG, "Received payload for sensorID: " + nextPayload.getSensorID()); } // If this is a sensor enumeration packet, update our configuration information // Note: this currently allows USBManager to be reconfigured while running // which seems to make sense, since we're hoping to try to reconnect to // the Arduino while live (although we may need a new A3PSession) // // We may later replace this processing with a virtual 'sensor 0' // if (nextPayload.getSensorID() == 0) { Log.d(LOG_TAG, "We got a system control packet!"); String payloadString = nextPayload.payloadAsString(); if (payloadString.length() >= 1 && payloadString.charAt(0) == '!') { Log.d(LOG_TAG, "Processing identification string: " + payloadString); // Split the ID string on the ; character, ignoring the first character String[] ids = payloadString.substring(1).split(";"); for (int i = 0; i < ids.length; i++) { int temp = ids[i].indexOf(","); ids[i] = ids[i].substring(0, temp); try { ids[i] = "" + Long.parseLong(ids[i]); // Trim off leading zeros or whitespace } catch (NumberFormatException e) { Log.e(LOG_TAG, "Error parsing sensor id: " + ids[i]); } Log.d(LOG_TAG, "Found sensor id: " + ids[i]); } synchronized (deviceIDs) { deviceIDs.clear(); deviceIDs.addAll(Arrays.asList(ids)); Log.d(LOG_TAG, "notifying with devices " + deviceIDs.size()); deviceIDs.notifyAll(); } } } else { SensorDataPacket sdp; if (nextPayload.isReadingSeries()) { sdp = new SensorDataPacket( nextPayload.getRawBytes(), nextPayload.getSeriesTimestamp(), nextPayload.getNumOfReadingsInSeries()); } else { sdp = new SensorDataPacket( nextPayload.getRawBytes(), nextPayload.getAndroidTimeStamp()); } mSensorManager.addSensorDataPacket(String.valueOf(nextPayload.getSensorID()), sdp); } } } } }