Example #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;
  }
Example #2
0
  /**
   * Sensor register
   *
   * @return True if sensor is successfully registered or has already been registered
   */
  public boolean sensorRegister(String id_to_add, DriverType sensorType) {
    Log.d(TAG, "In sensor register.");
    if (sensorType == null) {
      Log.d(TAG, "Did not find sensor type for sensor " + id_to_add);
      return false;
    }

    if (mDiscoverableDeviceList == null || mDiscoverableDeviceList.size() == 0) {
      // re init once again
      initializeSensors();
    }

    if (mDiscoverableDeviceList == null || mDiscoverableDeviceList.size() == 0) {
      // still no sensors?
      Log.e(
          TAG,
          "No device ids, check usb connection!  FAILED to register '"
              + sensorType
              + "' sensor with passed id '"
              + id_to_add
              + "'.");
      return false;
    }

    boolean foundSensor = false;

    // TODO: Add a map from string id to string description

    Iterator<DiscoverableDevice> discoverableDeviceIterator = mDiscoverableDeviceList.iterator();
    while (discoverableDeviceIterator.hasNext()) {
      USBDiscoverableDevice newDevice = (USBDiscoverableDevice) discoverableDeviceIterator.next();
      if (newDevice.getDeviceId().equals(id_to_add) && newDevice.connectionLost == false) {
        foundSensor = true;
      }
    }

    if (foundSensor) {
      if (mSensorManager.getSensor(id_to_add) != null) return true;
      // Need to add the sensor
      if (mSensorManager.addSensor(id_to_add, sensorType)) {
        Log.d(TAG, "Added usb sensor to sensor manager.");

        Intent i = new Intent();
        i.setAction(ServiceConstants.USB_STATE_CHANGE);
        mContext.sendBroadcast(i);
        return true;
      } else {
        Log.e(TAG, "Failed to add usb sensor to sensor manager");
      }
    }

    Log.d(
        TAG,
        "Can't find sensor: '" + id_to_add + "' of type '" + sensorType + "' registration failed.");
    return false;
  }