Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity implements ServiceConnection {
- private final String LOG_TAG = "quaternion";
- private final String MW_MAC_ADDRESS = "C6:EE:8B:E0:4B:24";
- private BtleService.LocalBinder serviceBinder;
- private MetaWearBoard board;
- private SensorFusionBosch sensorFusion;
- private Logging logging;
- @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, BtleService.class),
- this, Context.BIND_AUTO_CREATE);
- findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.i(LOG_TAG, "start");
- logging.start(false);
- sensorFusion.quaternion().start();
- sensorFusion.start();
- }
- });
- findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.i(LOG_TAG, "stop");
- sensorFusion.stop();
- sensorFusion.quaternion().stop();
- logging.stop();
- logging.downloadAsync().continueWith(new Continuation<Void, Void>() {
- @Override
- public Void then(Task<Void> task) throws Exception {
- if (task.isFaulted()) {
- Log.i(LOG_TAG, "Download failed");
- } else {
- Log.i(LOG_TAG, "Download complete");
- }
- return null;
- }
- });
- }
- });
- findViewById(R.id.reset).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- board.tearDown();
- }
- });
- }
- @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 = (BtleService.LocalBinder) service;
- Log.i(LOG_TAG, "Service connected");
- retrieveBoard(MW_MAC_ADDRESS);
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- public void retrieveBoard(String macAddr) {
- final BluetoothManager btManager=
- (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
- final BluetoothDevice remoteDevice=
- btManager.getAdapter().getRemoteDevice(macAddr);
- // Create a MetaWear board object for the Bluetooth Device
- board = serviceBinder.getMetaWearBoard(remoteDevice);
- board.connectAsync().onSuccessTask(new Continuation<Void, Task<Route>>() {
- @Override
- public Task<Route> then(Task<Void> task) throws Exception {
- Log.i(LOG_TAG, "Board connected");
- logging = board.getModule(Logging.class);
- sensorFusion = board.getModule(SensorFusionBosch.class);
- sensorFusion.configure()
- .mode(Mode.IMU_PLUS)
- .accRange(AccRange.AR_16G)
- .gyroRange(GyroRange.GR_2000DPS)
- .commit();
- return sensorFusion.quaternion().addRouteAsync(new RouteBuilder() {
- @Override
- public void configure(RouteComponent source) {
- source.log(new Subscriber() {
- @Override
- public void apply(Data data, Object... env) {
- Log.i(LOG_TAG, data.formattedTimestamp() + ": " + data.value(Quaternion.class).toString());
- }
- });
- }
- });
- }
- }).continueWith(new Continuation<Route, Void>() {
- @Override
- public Void then(Task<Route> task) throws Exception {
- if (task.isFaulted()) {
- Log.w(LOG_TAG, "Failed to configure app", task.getError());
- } else {
- Log.i(LOG_TAG, "App configured");
- }
- return null;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement