Esempio n. 1
1
  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;
  }
Esempio n. 2
0
  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!");
    }
  }
Esempio n. 3
0
  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;
  }
Esempio n. 4
0
  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!");
  }