Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Data Stream
package com.example.ritu.record;
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.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
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.Accelerometer;
import com.mbientlab.metawear.module.Debug;
import com.mbientlab.metawear.module.Gyro;
public class MainActivity extends AppCompatActivity implements ServiceConnection ,Runnable{
private static final String LOG_TAG1="A",LOG_TAG="G", ACCEL_DATA="accel_data",GYRO_DATA="gyro_data";
private MetaWearBleService.LocalBinder serviceBinder;
private MetaWearBoard mwBoard;
private Accelerometer accelModule;
private Gyro gyroModule;
private Debug debugModule;
Thread t1 = new Thread() {
@Override
public void run() {
synchronized (this){
accelModule.enableAxisSampling();
accelModule.start();
}
}
};
Thread t2 = new Thread() {
@Override
public void run() {
synchronized (this){
gyroModule.start();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getApplicationContext().bindService(new Intent(this, MetaWearBleService.class), this, Context.BIND_AUTO_CREATE);
findViewById(R.id.start_accel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t2.start();
t1.start();
}
});
findViewById(R.id.stop_accel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
accelModule.stop();
gyroModule.stop();
}
});
findViewById(R.id.reset_accel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
debugModule.resetDevice();
}
});
}
@Override
public void onDestroy(){
super.onDestroy();
getApplicationContext().unbindService(this);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serviceBinder = (MetaWearBleService.LocalBinder)service;
final String mwMacAdress= "F7:31:B8:53:9C:8F";
BluetoothManager btManager=(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothDevice btDeveice= btManager.getAdapter().getRemoteDevice(mwMacAdress);
mwBoard=serviceBinder.getMetaWearBoard(btDeveice);
mwBoard.setConnectionStateHandler(new MetaWearBoard.ConnectionStateHandler() {
@Override
public void connected() {
super.connected();
Log.i(LOG_TAG,"Connected");
try {
accelModule = mwBoard.getModule(Accelerometer.class);
accelModule.setAxisSamplingRange(2.f);
accelModule.setOutputDataRate(50.f);
accelModule.routeData().fromAxes().stream(ACCEL_DATA).commit()
.onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
result.subscribe(ACCEL_DATA, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
Log.i(LOG_TAG1, message.getData(CartesianFloat.class).toString());
}
});
}
});
debugModule=mwBoard.getModule(Debug.class);
} catch (UnsupportedModuleException e) {
Log.i(LOG_TAG,"cannot find module",e);
}
try {
gyroModule=mwBoard.getModule(Gyro.class);
gyroModule.setAngularRateRange(125.f);
gyroModule.setOutputDataRate(25.f);
gyroModule.routeData().fromAxes().stream(GYRO_DATA).commit()
.onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
result.subscribe(GYRO_DATA, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
Log.i(LOG_TAG, message.getData(CartesianFloat.class).toString());
}
});
}
});
debugModule=mwBoard.getModule(Debug.class);
} catch (UnsupportedModuleException e){
Log.i(LOG_TAG,"cannot find module", e);
}
}
@Override
public void disconnected() {
Log.i(LOG_TAG,"Disconnected");
}
});
mwBoard.connect();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void run() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment