package com.example.kai.schrittzaehler; import android.app.Activity; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.content.*; import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import com.mbientlab.metawear.AsyncOperation; import com.mbientlab.metawear.Message; import com.mbientlab.metawear.MetaWearBleService; import com.mbientlab.metawear.MetaWearBoard; import static com.mbientlab.metawear.MetaWearBoard.ConnectionStateHandler; import static com.mbientlab.metawear.AsyncOperation.CompletionHandler; import com.mbientlab.metawear.RouteManager; import com.mbientlab.metawear.UnsupportedModuleException; import com.mbientlab.metawear.data.CartesianFloat; import com.mbientlab.metawear.module.Bmi160Gyro; import com.mbientlab.metawear.module.Bmi160Accelerometer; import com.mbientlab.metawear.module.Led; import com.mbientlab.metawear.module.Accelerometer; import com.mbientlab.metawear.module.Bmi160Gyro.*; import com.mbientlab.metawear.module.Logging; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.OutputStream; public class MainActivity extends AppCompatActivity implements ServiceConnection { //CONSTANTS private final String MW_MAC_ADDRESS= "D9:D0:32:22:AB:2C"; //update with your board's MAC address private static final String TAG = "MetaWear"; private Button connect; private Button disconnect; private Button led_on; private Button led_off; private Button acc_start; private Button acc_stop; private Switch accel_switch; private static final float ACC_RANGE = 8.f, ACC_FREQ = 50.f; private static final String STREAM_KEY = "accel_stream"; private static final String LOG_KEY = "accel_log"; private static final String GYRO_STREAM_KEY = "gyro_stream"; TextView accelData; TextView gyroData; TextView counted_steps; //METAWEAR OBJECTS private MetaWearBleService.LocalBinder serviceBinder; private Led ledModule; private Bmi160Accelerometer accelModule; private Bmi160Accelerometer stepModule; private Bmi160Gyro gyroModule; private MetaWearBoard mwBoard; private Logging loggingModule; public final ConnectionStateHandler stateHandler; { stateHandler = new ConnectionStateHandler() { @Override public void connected() { Log.i(TAG, "Connected"); try { ledModule = mwBoard.getModule(Led.class); accelModule = mwBoard.getModule(Bmi160Accelerometer.class); loggingModule = mwBoard.getModule(Logging.class); gyroModule = mwBoard.getModule(Bmi160Gyro.class); ready(); } catch (UnsupportedModuleException e) { e.printStackTrace(); } led_on = (Button) findViewById(R.id.led_on); led_on.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "Turn on LED"); ledModule.configureColorChannel(Led.ColorChannel.BLUE) .setRiseTime((short) 0).setPulseDuration((short) 1000) .setRepeatCount((byte) -1).setHighTime((short) 500) .setHighIntensity((byte) 16).setLowIntensity((byte) 16) .commit(); ledModule.play(true); } }); led_off = (Button) findViewById(R.id.led_off); led_off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "Turn off LED"); ledModule.stop(true); } }); acc_start = (Button) findViewById(R.id.acc_start); acc_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stepModule.readStepCounter(false); } }); acc_stop = (Button) findViewById(R.id.acc_stop); acc_stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stepModule.resetStepCounter(); } }); accel_switch = (Switch) findViewById(R.id.accel_switch); accel_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.i("Switch State=", "" + isChecked); if (isChecked) { accelModule.setOutputDataRate(ACC_FREQ); accelModule.setAxisSamplingRange(ACC_RANGE); gyroModule.configure() .setOutputDataRate(OutputDataRate.ODR_50_HZ) .setFullScaleRange(FullScaleRange.FSR_500) .commit(); AsyncOperation routeManagerResultAccel = accelModule.routeData().fromAxes().stream(STREAM_KEY).commit(); AsyncOperation routeManagerResultGyro = gyroModule.routeData().fromAxes().stream(GYRO_STREAM_KEY).commit(); routeManagerResultAccel.onComplete(new CompletionHandler() { @Override public void success(RouteManager result) { result.subscribe(STREAM_KEY, new RouteManager.MessageHandler() { @Override public void process(Message msg) { final CartesianFloat axes = msg.getData(CartesianFloat.class); Log.i(TAG, String.format("Accelerometer: %s", axes.toString())); sensorMsg(String.format(axes.toString()), "accel"); } }); } }); routeManagerResultGyro.onComplete(new CompletionHandler() { @Override public void success(RouteManager result) { result.subscribe(GYRO_STREAM_KEY, new RouteManager.MessageHandler() { @Override public void process(Message msg) { final CartesianFloat spinData = msg.getData(CartesianFloat.class); Log.i(TAG, String.format("Gyroscope: %s", spinData.toString())); sensorMsg(String.format(spinData.toString()), "gyro"); } }); } }); accelModule.enableAxisSampling(); accelModule.start(); gyroModule.start(); } else { gyroModule.stop(); accelModule.disableAxisSampling(); accelModule.stop(); } } }); } @Override public void disconnected() { Log.i(TAG, "Connected Lost"); } @Override public void failure(int status, Throwable error) { Log.e(TAG, "Error connecting", error); } }; } public void retrieveBoard() { final BluetoothManager btManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); final BluetoothDevice remoteDevice= btManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS); // Create a MetaWear board object for the Bluetooth Device mwBoard= serviceBinder.getMetaWearBoard(remoteDevice); mwBoard.setConnectionStateHandler(stateHandler); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ///< Bind the service when the activity is created getApplicationContext().bindService(new Intent(this, MetaWearBleService.class), this, Context.BIND_AUTO_CREATE); Log.i(TAG, "log test"); connect=(Button)findViewById(R.id.connect); connect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "Clicked connect"); mwBoard.connect(); } }); disconnect = (Button) findViewById(R.id.disconnect); disconnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "Clicked disconnect"); ledModule.stop(true); // getApplicationContext().unbindService(this); mwBoard.setConnectionStateHandler(null); mwBoard.disconnect(); } }); accelData = (TextView) findViewById(R.id.textAccel); gyroData = (TextView) findViewById(R.id.textGyro); counted_steps = (TextView) findViewById(R.id.stepView); } @Override public void onDestroy() { super.onDestroy(); ///< Unbind the service when the activity is destroyed getApplicationContext().unbindService(this); } @Override public void onServiceConnected(ComponentName name, IBinder service) { ///< Typecast the binder to the service's LocalBinder class serviceBinder = (MetaWearBleService.LocalBinder) service; retrieveBoard(); } @Override public void onServiceDisconnected(ComponentName componentName) { } public void sensorMsg(String msg, final String sensor) { final String reading = msg; runOnUiThread(new Runnable() { @Override public void run() { if (sensor == "accel") { accelData.setText("Accel: " + reading); } else { gyroData.setText("Gyro: " + reading); } } }); } public void ready() { try { stepModule = mwBoard.getModule(Bmi160Accelerometer.class); stepModule.configureStepDetection() // Set sensitivity to normal //.setMode(StepSensitivity.NORMAL) .setSensitivity(Bmi160Accelerometer.StepSensitivity.NORMAL) // Enable step counter .enableStepCounter() .commit(); stepModule.routeData().fromStepCounter(false).stream("step_counter").commit() .onComplete(new CompletionHandler() { @Override public void success(RouteManager result) { result.subscribe("step_counter", new RouteManager.MessageHandler() { @Override public void process(Message msg) { Log.i("MainActivity", "Steps= " + msg.getData(Integer.class)); counted_steps.setText("Steps: " + msg.getData(Integer.class)); } }); } }); stepModule.start(); } catch (UnsupportedModuleException e){ e.printStackTrace(); } } }