Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.christophergs.metawearguide;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
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 com.mbientlab.metawear.RouteManager;
import com.mbientlab.metawear.UnsupportedModuleException;
import com.mbientlab.metawear.data.CartesianFloat;
import com.mbientlab.metawear.module.Bmi160Accelerometer;
import com.mbientlab.metawear.module.Bmi160Gyro;
import com.mbientlab.metawear.module.Bmi160Gyro.FullScaleRange;
import com.mbientlab.metawear.module.Bmi160Gyro.OutputDataRate;
import com.mbientlab.metawear.module.Led;
import com.mbientlab.metawear.module.Logging;
import com.mbientlab.metawear.processor.Time;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.mbientlab.metawear.AsyncOperation.CompletionHandler;
import static com.mbientlab.metawear.MetaWearBoard.ConnectionStateHandler;
public class
MyActivity extends AppCompatActivity implements ServiceConnection,AdapterView.OnItemSelectedListener{
//CONSTANTS
private final String MW_MAC_ADDRESS= "F7:31:B8:53:9C:8F"; //update with your board's MAC address
private static final String TAG = "MetaWear";
private Button connect;
private Button led_on;
private Button led_off;
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;
//METAWEAR OBJECTS
private MetaWearBleService.LocalBinder serviceBinder;
private Led ledModule;
private Bmi160Accelerometer accelModule;
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);
} 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);
}
});
Switch 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);
final String CSV_HEADER = String.format("sensor,x_axis,y_axis,z_axis,TimeStamp");
/////////save file using data and time///////
Date date = new Date();
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-mm-dd");
final String filename = dateFormat.format(date)+".csv";
final File path = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), filename);
if (isChecked) {
OutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(path, true));
out.write(CSV_HEADER.getBytes());
out.write("\n".getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
accelModule.setOutputDataRate(ACC_FREQ);
accelModule.setAxisSamplingRange(ACC_RANGE);
gyroModule.configure()
.setOutputDataRate(OutputDataRate.ODR_50_HZ)
.setFullScaleRange(FullScaleRange.FSR_500)
.commit();
AsyncOperation<RouteManager> routeManagerResultAccel = accelModule.routeData().fromAxes()
.process(new Time(Time.OutputMode.ABSOLUTE,30)).stream(STREAM_KEY).commit();
AsyncOperation<RouteManager> routeManagerResultGyro = gyroModule.routeData().fromAxes()
.process(new Time(Time.OutputMode.ABSOLUTE,30)).stream(GYRO_STREAM_KEY).commit();
routeManagerResultAccel.onComplete(new CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
result.subscribe(STREAM_KEY, new RouteManager.MessageHandler() {
@Override
public void process(Message msg) {
Log.i(TAG, String.format("Accelerometer: %s,%s", msg.getData(CartesianFloat.class),msg.getTimestamp().getTimeInMillis()));
sensorMsg(String.format(msg.toString()), "accel");
//CSV CODE
String accel_entry = String.format("Accel, %s,%s", msg.getData(CartesianFloat.class).toString(),msg.getTimestamp().getTime());
String csv_accel_entry = accel_entry + ",";
OutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(path, true));
out.write(csv_accel_entry.getBytes());
out.write("\n".getBytes());
out.close();
} catch (Exception e) {
Log.e(TAG, "CSV creation error", e);
}
}
});
}
});
routeManagerResultGyro.onComplete(new CompletionHandler<RouteManager>() {
@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, %s", msg.getData(CartesianFloat.class),msg.getTimestampAsString()));
sensorMsg(String.format(msg.toString()), "gyro");
//CSV CODE
String gyro_entry = String.format("Gyro, %s,%s", msg.getData(CartesianFloat.class).toString(),msg.getTimestamp().getTimeInMillis());
String csv_gyro_entry = gyro_entry + ",";
OutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(path, true));
out.write(csv_gyro_entry.getBytes());
out.write("\n".getBytes());
out.close();
} catch (Exception e) {
Log.e(TAG, "CSV creation error", e);
}
}
});
}
});
accelModule.enableAxisSampling();
accelModule.start();
gyroModule.start();
} else
{
gyroModule.stop();
accelModule.disableAxisSampling();
accelModule.stop();
}
findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!filename.isEmpty());
path.delete();
}
});
}
});
}
@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 clearcsv(){
}
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_my);
///< 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();
}
});
accelData = (TextView) findViewById(R.id.textAccel);
gyroData = (TextView) findViewById(R.id.textGyro);
//
//
// Spinner spinner =(Spinner)findViewById(R.id.spinner);
// // Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.Shots,android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@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);
}
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment