Android Open Source - MovisensGattSensorExample Start Activity






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.activities;
/*w  ww  .j  a  v  a  2 s .co  m*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.movisens.gattsensorexample.R;
import com.movisens.gattsensorexample.application.App;
import com.movisens.gattsensorexample.events.MeasurementStatus;
import com.movisens.gattsensorexample.events.MeasurementStatus.SensorState;
import com.movisens.gattsensorexample.model.CurrentSensorData;
import com.movisens.gattsensorexample.services.SamplingService;
import com.movisens.smartgattlib.characteristics.BatteryLevel;

import de.greenrobot.event.EventBus;

public class StartActivity extends Activity {

  SamplingService mService;
  boolean mBound = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    this.setContentView(R.layout.start_activity);
    Button startSampling = (Button) this.findViewById(R.id.startSampling);
    startSampling.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        SamplingService.start(App.app());
        updateButtons();
      }
    });
    Button stopSampling = (Button) this.findViewById(R.id.stopSampling);
    stopSampling.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        SamplingService.stop(App.app(), false);
        updateButtons();
      }
    });
  }

  @Override
  public void onResume() {
    super.onResume();
    EventBus.getDefault().register(this);
    updateButtons();
    updateSensorData(App.app().getCurrentSensorData());
    updateBatteryLevel(App.app().getCurrentBatteryLevel());
  }

  @Override
  public void onPause() {
    super.onPause();
    EventBus.getDefault().unregister(this);
    finish();
  }

  public void onEventMainThread(CurrentSensorData sensorData) {
    updateSensorData(sensorData);
  }

  private void updateSensorData(CurrentSensorData sensorData) {
    TextView movementAcceleration = (TextView) this
        .findViewById(R.id.movementAcceleration);

    if (sensorData != null) {
      movementAcceleration.setText(getString(
          R.string.movement_acceleration_desc,
          sensorData.movementAcceleration));

    } else {
      movementAcceleration.setText(getString(R.string.waiting));
    }
  }

  public void onEventMainThread(BatteryLevel batteryLevel) {
    updateBatteryLevel(batteryLevel);
  }

  public void onEventMainThread(MeasurementStatus measurementStatus) {
    if (measurementStatus.measurementEnabled == SensorState.False
        && measurementStatus.dataAvailable == SensorState.True) {
      SamplingService.stop(this, false);
      Toast.makeText(
          this,
          "Measurement can't be started, because there is already a measurement on the sensor",
          Toast.LENGTH_LONG).show();
    }
    updateButtons();
  }

  private void updateBatteryLevel(BatteryLevel batteryLevel) {
    TextView batteryLevelText = (TextView) this
        .findViewById(R.id.batteryLevel);
    if (batteryLevel == null) {
      batteryLevelText
          .setText(getString(R.string.battery_level_desc, "?"));
    } else {
      batteryLevelText.setText(getString(R.string.battery_level_desc,
          batteryLevel.getBatteryLevel()));
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem actionItem = menu.add("Preferences");
    actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    actionItem.setIcon(android.R.drawable.ic_menu_preferences);

    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    Intent launchPreferencesIntent = new Intent().setClass(this,
        Preferences.class);

    startActivity(launchPreferencesIntent);
    return true;
  }

  private void updateButtons() {
    Button startSampling = (Button) this.findViewById(R.id.startSampling);
    Button stopSampling = (Button) this.findViewById(R.id.stopSampling);
    TextView sensorName = (TextView) this.findViewById(R.id.sensor_name);
    if (App.app().isSamplingRunning()) {
      startSampling.setVisibility(View.GONE);
      stopSampling.setVisibility(View.VISIBLE);
    } else {
      startSampling.setVisibility(View.VISIBLE);
      stopSampling.setVisibility(View.GONE);
    }
    if (App.app().isSensorSelected()) {
      startSampling.setEnabled(true);
      stopSampling.setEnabled(true);
    } else {
      startSampling.setEnabled(false);
      stopSampling.setEnabled(false);
    }

    sensorName.setText(App.app().getSensor());
  }
}




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