This is part 3 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
Binding the Service
For us to interact with the MetaWear board, we need to bind the Bluetooth service in our application and keep a reference to it. This is one of the more confusing parts of working with the MetaWear platform, and we’ll continue to explore it as the series unfolds. For now, in your MyActivity.java file replace the code with the following:
The ServiceConnection
we are implementing in this code is defined as:
[An] Interface for monitoring the state of an application service
This means that any Android activity or fragment where you want to access your MetaWear board will need access to the service connection created in the code above.
Logging
Before we go any further, let’s make sure we have a way to log what we are doing. We will need this a lot.
import android.util.Log;
, then declare a constant to identify our logs:
private static final String TAG = "MetaWear";
Let’s run a quick test in the onCreate
method:
For those new to Android, here we are logging at the “info” level, hence Log.i
and we add a description tag to our log so that we can easily filter the logs and find them. To check this is working, run the app and then filter your logcat by the “METAWEAR” term:
Without this filter, the logs will be too noisy to be much use.
Connecting to the Board
The MetaWearBoard class is the central class for communicating with your MetaWear board. To start with, we will connect to our board using the MAC address. For simplicity, use the MetaWear official app to lookup your board’s MAC address (it should in a format like “D5:9C:DC:37:BA:AE”)
We add our Bluetooth imports:
Then declare our variables in our MyActivity
class
However, we can’t connect to the board without setting up a mechanism for managing the connection state. Notifications about the connection state are handled by the ConnectionStateHandler class. You register a handler by calling setConnectionStateHandler
.
We’ll need a few more imports to make this work:
So we add the following to our MyActivity
class:
Now that we have created the handler, we need to pass the handler to our board object. To do this, we create a retrieveBoard
method (note that this is still within the activity):
Notice how this function is seeking out the board with the MAC address and then passing that same board the state handler. We should now call the retrieveBoard
function inside the onServiceConnected
method.
Now we have setup our connection state handler, we’ve retrieved the board based on its MAC address, the last thing we need to do is actually connect to it. For this, we’ll add a simple button to our app, and then add an onClick listener to this button to trigger the connection. If you’ve ever worked with Javascript, the onClick principle is very similar.
Open up the activity_my.xml file and inside the RelativeLayout
tag add the button with xml:
You’ll also notice the Hello World
TextView above the button. We may as well remove that. If you’re new to Android, play around with the text displayed by adjusting the values found in the values/strings.xml file.
Now when we run the app, we should see the button. But it won’t do anything yet. Let’s declare our button with our other variable declarations: private Button connect;
Then add an onClick listener inside our onCreate
method. This is what it looks like:
Go ahead and run the app. When you press the connect button, your logs should first tell you that you have clicked the button, and then show “connected”. If you have a problem connecting, be sure to try the following:
- If you had previously paired with the device close any apps and programs that were paired to it and toggle the bluetooth radio on these devices on and off.
- If this is your first time pairing this device make sure that your device is charged
- Make sure that you have a device that supports bluetooth 4.0.
Good stuff! Now that we’ve connected to our board, we are ready to start playing with its different sensors. Check out part four to see this.
Note You can view all these changes in the github repository on the branch version-0.2