예제 #1
0
  public void run() {
    synchronized (syncObject) {
      while (true) {
        try {
          try {

            if ((state == PAUSED) || (state == STOPPED)) {
              if (state == PAUSED) {
                syncObject.wait(pauseTime);
              } else if (state == STOPPED) {
                syncObject.wait();
              }
              state = RUNNING;
              continue;
            }

            // SENSE
            // sense() is a blocking call and returns when
            // the sensing is complete, the sensorConfig object
            // will have the sampling window, cycle information
            if (GlobalConfig.shouldLog()) {
              Log.d(
                  getLogTag(),
                  "Pulling from: " + SensorUtils.getSensorName(sensor.getSensorType()));
            }

            SensorData sensorData = getCurrentSensorData(false);
            // publish sensed data
            publishData(sensorData);

            // SLEEP
            long samplingInterval =
                (Long) sensor.getSensorConfig(PullSensorConfig.POST_SENSE_SLEEP_LENGTH_MILLIS);
            syncObject.wait(samplingInterval);
          } catch (InterruptedException exp) {
            // ignore
          }
        } catch (ESException e) {
          e.printStackTrace();
          try {
            Thread.sleep(30000);
          } catch (Exception exp) {
            exp.printStackTrace();
          }
        }
      }
    }
  }
  private BluetoothSensor(Context context) {
    super(context);
    btDevices = new ArrayList<ESBluetoothDevice>();
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    if (bluetooth == null) {
      if (GlobalConfig.shouldLog()) {
        Log.d(TAG, "Device does not support Bluetooth");
      }
      return;
    }

    // Create a BroadcastReceiver for ACTION_FOUND, sent when a device is
    // discovered
    receiver =
        new BroadcastReceiver() {

          public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
              // Get the BluetoothDevice object from the Intent
              String deviceAddr =
                  ((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE))
                      .getAddress();
              String deviceName =
                  ((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE))
                      .getName();
              int rssi = (int) intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);

              ESBluetoothDevice esBluetoothDevice =
                  new ESBluetoothDevice(System.currentTimeMillis(), deviceAddr, deviceName, rssi);

              if (!(btDevices.contains(esBluetoothDevice))) {
                btDevices.add(esBluetoothDevice);
              }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              cyclesRemaining -= 1;
              if (cyclesRemaining > 0) {
                bluetooth.startDiscovery();
              } else {
                notifySenseCyclesComplete();
              }
            }
          }
        };
  }