Android Open Source - MovisensGattSensorExample Sampling Service






From Project

Back to project page MovisensGattSensorExample.

License

The source code is released under:

GNU General Public License

If you think the Android project MovisensGattSensorExample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.movisens.gattsensorexample.services;
//from  w w  w.ja  v  a  2  s  .  c  om
import static com.movisens.gattsensorexample.application.App.app;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.movisens.gattsensorexample.R;
import com.movisens.gattsensorexample.activities.Preferences;
import com.movisens.gattsensorexample.activities.SensorConnected;
import com.movisens.gattsensorexample.activities.SensorDisconnected;
import com.movisens.gattsensorexample.events.BLEEvent;
import com.movisens.gattsensorexample.events.MeasurementStatus;
import com.movisens.gattsensorexample.events.MeasurementStatus.SensorState;
import com.movisens.gattsensorexample.events.SensorStatusEvent;
import com.movisens.gattsensorexample.events.SensorStatusEvent.SensorStatus;
import com.movisens.gattsensorexample.model.CurrentSensorData;
import com.movisens.gattsensorexample.sensors.MovisensSensor;
import com.movisens.gattsensorexample.utils.BleUtils;
import com.movisens.movisensgattlib.characteristics.MeasurementEnabled;
import com.movisens.movisensgattlib.characteristics.MovementAcceleration;
import com.movisens.smartgattlib.characteristics.BatteryLevel;
import com.movisens.smartgattlib.characteristics.HeartRateMeasurement;

import de.greenrobot.event.EventBus;

public class SamplingService extends Service {

  private static final String TAG = SamplingService.class.getSimpleName();
  private static final String COMMAND = "command";
  private static final String COMMAND_START = "start";
  private static final String COMMAND_RANDOM_TRIGGER = "randomTrigger";
  private static final String COMMAND_STOP_SENSOR = "stopSensor";

  private boolean isRunning = false;
  private static PowerManager.WakeLock mWakeLock;
  private EventBus mEventBus;
  private BluetoothAdapter mBluetoothAdapter = null;
  private Context context;
  private MovisensSensor movisensSensor;

  public static void start(Context context) {
    Editor editor = app().getPrefs().edit();
    editor.putBoolean(Preferences.SAMPLING_RUNNING, true);
    editor.commit();

    Intent msgIntent = new Intent(context, SamplingService.class);
    msgIntent.putExtra(SamplingService.COMMAND,
        SamplingService.COMMAND_START);
    context.startService(msgIntent);
  }

  public static void randomTrigger(Context context) {
    Intent msgIntent = new Intent(context, SamplingService.class);
    msgIntent.putExtra(SamplingService.COMMAND,
        SamplingService.COMMAND_RANDOM_TRIGGER);
    context.startService(msgIntent);
  }

  public static void stopSensor(Context context) {
    Intent msgIntent = new Intent(context, SamplingService.class);
    msgIntent.putExtra(SamplingService.COMMAND,
        SamplingService.COMMAND_STOP_SENSOR);
    context.startService(msgIntent);
  }

  public static void stop(Context context, boolean shutdown) {
    if (!shutdown) {
      Editor editor = app().getPrefs().edit();
      editor.putBoolean(Preferences.SAMPLING_RUNNING, false);
      editor.commit();
    }
    Intent msgIntent = new Intent(context, SamplingService.class);
    context.stopService(msgIntent);
  }

  @Override
  public void onCreate() {
    super.onCreate();
    mEventBus = EventBus.getDefault();
    context = getBaseContext();

    if (!BleUtils.hasBluetoothLE(context)) {
      String message = "Bluetooth Low Energy is not supported on this phone !";
      app().showToast(message, Toast.LENGTH_LONG);
      Log.w(TAG, message);
      return;
    } else {
      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      if (mBluetoothAdapter == null) {
        String message = "Bluetooth is not available!";
        app().showToast(message, Toast.LENGTH_LONG);
        Log.w(TAG, message);
        return;
      } else {
        movisensSensor = new MovisensSensor();
      }
    }
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
      if (COMMAND_START.equals(intent.getStringExtra(COMMAND))) {
        if (!isRunning) {
          isRunning = true;
          mEventBus.register(this);
          showIconDisconnected();
          movisensSensor.start(context);
        }
      }
    }
    return Service.START_STICKY;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    if (movisensSensor != null) {
      movisensSensor.stop();
    }
    if (mWakeLock != null) {
      mWakeLock.release();
    }
    mEventBus.unregister(this);
  }

  public void onEventMainThread(SensorStatusEvent event) {
    if (event.getStatus().equals(SensorStatus.Connected)) {
      showIconConnected();
    } else {
      showIconDisconnected();
    }
    Log.d(TAG, "Sensor status: " + event.getStatus().toString());
  }

  public void onEventMainThread(BLEEvent event) {
    Log.d(TAG, "BLE EVENT:" + event.getMessage());
  }

  public void onEventMainThread(HeartRateMeasurement hrm) {
    Log.d(TAG, "HeartRate: " + hrm.getHr() + " bpm, " + hrm.getSensorWorn());

    Log.d(TAG, hrm.getEe() + " kJ");

    for (Float rr : hrm.getRrInterval()) {
      Log.d(TAG, "RR: " + rr + "s");
    }
  }

  public void onEventMainThread(CurrentSensorData sensorData) {

  }

  public void onEventMainThread(MovementAcceleration movementAccleration) {
    Log.d(TAG, "MovementAccleration: " + movementAccleration.getValue()
        + " g");
  }

  public void onEventMainThread(BatteryLevel batteryLevel) {

  }

  public void onEventMainThread(MeasurementEnabled measurementEnabled) {
    if (measurementEnabled.getValue() == false) {
      SamplingService.stop(this, false);
    }
  }

  public void onEventMainThread(MeasurementStatus measurementStatus) {
    if (measurementStatus.measurementEnabled == SensorState.False
        && measurementStatus.dataAvailable == SensorState.True) {
      SamplingService.stop(this, false);
      Log.d(TAG, "Measurement stopped");
    }
  }

  private void showIconConnected() {
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, SensorConnected.class), 0);
    Notification noti = new NotificationCompat.Builder(context)
        .setContentTitle(getText(R.string.notification_title))
        .setContentText(getText(R.string.sensor_connected))
        .setContentIntent(pendingIntent)
        .setSmallIcon(R.drawable.ic_stat_connected).build();
    stopForeground(true);
    startForeground(1, noti);
  }

  private void showIconDisconnected() {
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, SensorDisconnected.class), 0);
    Notification noti = new NotificationCompat.Builder(context)
        .setContentTitle(getText(R.string.notification_title))
        .setContentText(getText(R.string.sensor_disconnected))
        .setContentIntent(pendingIntent)
        .setSmallIcon(R.drawable.ic_stat_disconnected).build();
    stopForeground(true);
    startForeground(1, noti);
  }

  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }
}




Java Source Code List

com.movisens.gattsensorexample.activities.DeviceScanActivity.java
com.movisens.gattsensorexample.activities.Preferences.java
com.movisens.gattsensorexample.activities.SensorConnected.java
com.movisens.gattsensorexample.activities.SensorDisconnected.java
com.movisens.gattsensorexample.activities.StartActivity.java
com.movisens.gattsensorexample.activities.TimePreference.java
com.movisens.gattsensorexample.application.App.java
com.movisens.gattsensorexample.events.BLEEvent.java
com.movisens.gattsensorexample.events.MeasurementStatus.java
com.movisens.gattsensorexample.events.SensorStatusEvent.java
com.movisens.gattsensorexample.model.CurrentSensorData.java
com.movisens.gattsensorexample.receivers.SystemStateReceiver.java
com.movisens.gattsensorexample.receivers.UpdateAppReceiver.java
com.movisens.gattsensorexample.sensors.MovisensSensor.java
com.movisens.gattsensorexample.services.BluetoothLeService.java
com.movisens.gattsensorexample.services.SamplingService.java
com.movisens.gattsensorexample.utils.BleQueue.java
com.movisens.gattsensorexample.utils.BleUtils.java