Advertisement
Guest User

Untitled

a guest
May 27th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. public class MainActivity extends Activity implements ServiceConnection {
  2.     private final String LOG_TAG = "quaternion";
  3.     private final String MW_MAC_ADDRESS = "C6:EE:8B:E0:4B:24";
  4.  
  5.     private BtleService.LocalBinder serviceBinder;
  6.     private MetaWearBoard board;
  7.     private SensorFusionBosch sensorFusion;
  8.     private Logging logging;
  9.  
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.  
  15.         // Bind the service when the activity is created
  16.         getApplicationContext().bindService(new Intent(this, BtleService.class),
  17.                 this, Context.BIND_AUTO_CREATE);
  18.  
  19.         findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
  20.             @Override
  21.             public void onClick(View v) {
  22.                 Log.i(LOG_TAG, "start");
  23.                 logging.start(false);
  24.                 sensorFusion.quaternion().start();
  25.                 sensorFusion.start();
  26.             }
  27.         });
  28.  
  29.         findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View v) {
  32.                 Log.i(LOG_TAG, "stop");
  33.                 sensorFusion.stop();
  34.                 sensorFusion.quaternion().stop();
  35.                 logging.stop();
  36.  
  37.                 logging.downloadAsync().continueWith(new Continuation<Void, Void>() {
  38.                     @Override
  39.                     public Void then(Task<Void> task) throws Exception {
  40.                         if (task.isFaulted()) {
  41.                             Log.i(LOG_TAG, "Download failed");
  42.                         } else {
  43.                             Log.i(LOG_TAG, "Download complete");
  44.                         }
  45.                         return null;
  46.                     }
  47.                 });
  48.             }
  49.         });
  50.  
  51.         findViewById(R.id.reset).setOnClickListener(new View.OnClickListener() {
  52.             @Override
  53.             public void onClick(View v) {
  54.                 board.tearDown();
  55.             }
  56.         });
  57.     }
  58.  
  59.     @Override
  60.     public void onDestroy() {
  61.         super.onDestroy();
  62.  
  63.         // Unbind the service when the activity is destroyed
  64.         getApplicationContext().unbindService(this);
  65.     }
  66.  
  67.     @Override
  68.     public void onServiceConnected(ComponentName name, IBinder service) {
  69.         // Typecast the binder to the service's LocalBinder class
  70.         serviceBinder = (BtleService.LocalBinder) service;
  71.  
  72.         Log.i(LOG_TAG, "Service connected");
  73.  
  74.         retrieveBoard(MW_MAC_ADDRESS);
  75.     }
  76.  
  77.     @Override
  78.     public void onServiceDisconnected(ComponentName name) {
  79.  
  80.     }
  81.  
  82.     public void retrieveBoard(String macAddr) {
  83.         final BluetoothManager btManager=
  84.                 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  85.         final BluetoothDevice remoteDevice=
  86.                 btManager.getAdapter().getRemoteDevice(macAddr);
  87.  
  88.         // Create a MetaWear board object for the Bluetooth Device
  89.         board = serviceBinder.getMetaWearBoard(remoteDevice);
  90.         board.connectAsync().onSuccessTask(new Continuation<Void, Task<Route>>() {
  91.             @Override
  92.             public Task<Route> then(Task<Void> task) throws Exception {
  93.  
  94.                 Log.i(LOG_TAG, "Board connected");
  95.  
  96.                 logging = board.getModule(Logging.class);
  97.                 sensorFusion = board.getModule(SensorFusionBosch.class);
  98.                 sensorFusion.configure()
  99.                         .mode(Mode.IMU_PLUS)
  100.                         .accRange(AccRange.AR_16G)
  101.                         .gyroRange(GyroRange.GR_2000DPS)
  102.                         .commit();
  103.  
  104.                 return sensorFusion.quaternion().addRouteAsync(new RouteBuilder() {
  105.                     @Override
  106.                     public void configure(RouteComponent source) {
  107.                         source.log(new Subscriber() {
  108.                                     @Override
  109.                                     public void apply(Data data, Object... env) {
  110.                                         Log.i(LOG_TAG, data.formattedTimestamp() + ": " + data.value(Quaternion.class).toString());
  111.                                     }
  112.                                 });
  113.                     }
  114.                 });
  115.             }
  116.         }).continueWith(new Continuation<Route, Void>() {
  117.  
  118.             @Override
  119.             public Void then(Task<Route> task) throws Exception {
  120.                 if (task.isFaulted()) {
  121.                     Log.w(LOG_TAG, "Failed to configure app", task.getError());
  122.                 } else {
  123.                     Log.i(LOG_TAG, "App configured");
  124.                 }
  125.                 return null;
  126.             }
  127.         });
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement