MetaWear Guide Series Part 10
A Better Connection II
This is part 10 of a multipart series showing you how to get started with the MetaWear platform. View the contents of the series to easily skip forwards or backwards
To make our ScannerActivity useful, we need to be able to trigger our MyActivity class (which we built in parts 2-8). We will do this via the mechanism of an Android intent, which is how you start one activity from within a different activity.
Within our MetaWear board’s
connected state handler code, we add the following lines:
In the above code block, we trigger the new intent and pass our board details using the
We will need to have some corresponding code in our MyActivity class for this to work. So in MyActivity, first, add a few variables:
onCreate method, we add the reference to the data that was passed from ScannerActivity, i.e. the reference to the board, and create a new intent for our BLE service:
getApplicationContext().bindService(new Intent(this, MetaWearBleService.class), this, BIND_AUTO_CREATE);
We are no longer going to rely on a hard-coded MAC address to identify the board, so we re-write our
onServiceConnected method, removing the
retrieveBoard method and any calls to it:
Because this is taking place in a separate activity, we now need to move all our
getModule calls, and all our switch code into the
onServiceConnected, removing it from where it was previously in the
connected() block. If you find your app not working, this is probably the reason.
As a result of these changes we no longer need a manual “connect” button, so instead let’s replace it with a “disconnect” button.
First, we update the UI. In activity_my.xml update the connect button as follows:
Now we update the
onClick listener in our
That’s it! Now when we run our app we start out on our ScannerActivity screen, where we can select the board. Upon selecting the board, we are connected and our LED, accelerometer and gyroscope interaction can begin. At any time we can disconnect from the board, at which point we are returned to the scan screen.
As with all the other posts in the series, the code is availabe on github. Check branch version-0.9 in the github repo
If you’ve followed this series all the way through, you should now be ready to use other sensors on your board, and try different combinations of board features. The MetaWear platform is very versatile, and there are many areas I was not able to touch on in this introduction. Hopefully this guide helped to get you over the initial learning curve, so you can focus on building something exciting.
Some suggested resources to help you continue:
- The Android API docs
- The Android Sample app
- The Fragment Navigation Drawer, which is used in the sample app
- The Metawear Community Forum
Finally, a big thank you to the team at mbientlab - you are guys are doing a great job. Keep it up!