Project Setup

Before you begin coding your app, you will need to add the MetaWear API to your project. If you are new to Android and not familiar with Gradle, background services, and Android Bluetooth functions, we have povided an app template that has taken care of all the steps outlined on this page.

Compile Dependency

To add the library as a compile dependency to your project, first update the repositories closure to include the MbientLab Ivy repository in the project’s build.gradle file.

repositories {
    ivy {
        url ""
        layout "gradle"

Then, add a compile element to module’s build.gradle file and sync the project.

dependencies {
    compile 'com.mbientlab:metawear:3.7.0'

If you are using SDK v3.3 or newer, you will need to enable Java 8 feature support the module’s build.gradle file. See this [page]( in the Android Studio user guide.

Service Setup

Once the IDE has synced with the updated Gradle files, declare the BtleService in the module’s AndroidManifest.xml file.

    android:theme="@style/AppTheme" >

    <service android:name="" />
    <!-- Other application info -->

Next, bind the service in your application and retain a reference to the LocalBinder class. This should be done in any activity or fragment that needs to access a MetaWearBoard object.

import android.content.*;
import android.os.Bundle;
import android.os.IBinder;


public class MainActivity extends Activity implements ServiceConnection {
    private BtleService.LocalBinder serviceBinder;

    protected void onCreate(Bundle savedInstanceState) {

        // Bind the service when the activity is created
        getApplicationContext().bindService(new Intent(this, BtleService.class),
                this, Context.BIND_AUTO_CREATE);

    public void onDestroy() {

        // Unbind the service when the activity is destroyed

    public void onServiceConnected(ComponentName name, IBinder service) {
        // Typecast the binder to the service's LocalBinder class
        serviceBinder = (BtleService.LocalBinder) service;

    public void onServiceDisconnected(ComponentName componentName) { }

Finding Your Device

The last thing to do is retrieve the BluetoothDevice object corresponding to your MetaWear board. If you know the board’s MAC address, you can directly retrieve a BluetoothDevice object with getRemoteDevice, otherwise, you will have to initiate a Bluetooth LE scan to find the board.

When you have your BluetoothDevice object, call getMetaWearBoard to retrieve a MetaWearBoard object for the device.

// Other required imports
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;

public class MainActivity extends Activity implements ServiceConnection {
    private final String MW_MAC_ADDRESS= "EC:2C:09:81:22:AC";
    private MetaWearBoard board;

    public void retrieveBoard() {
        final BluetoothManager btManager=
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        final BluetoothDevice remoteDevice=

        // Create a MetaWear board object for the Bluetooth Device
        board= serviceBinder.getMetaWearBoard(remoteDevice);