2. Java API Installation

Here is a tutorial to install Java on Windows and get access to the Java API for your MetaSensors.

You should have completed the instructions in the previous section related to the Android Installation.

We will be using Android Studio so you should be comfortable with this IDE.

Java compilers are already installed with Android Studio and the Java language can be interpreted by the Android Studio IDE.

2.1. Install MetaWear

You need to add MetaWear to your compile dependencies.

To add the library to your project, first, update the repositories closure to include the MbientLab Ivy Repo in the project’s build.gradle file.

repositories {
    ivy {
        url "https://mbientlab.com/releases/ivyrep"
        layout "gradle"

Then, add the compile element to the dependencies closure in the module’s build.gradle file.

dependencies {
    compile 'com.mbientlab:metawear:3.4.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.

2.2. Usage

Once your project has synced with the updated Gradle files, declare the MetaWear Bluetooth LE service in the module’s AndroidManifest.xml file.

    android:theme="@style/AppTheme" >

    <service android:name="com.mbientlab.metawear.android.BtleService" />
    <!-- Other application info below i.e. activity definitions -->

Lastly, bind the service in your application and retrain a reference to the service’s LocalBinder class. This can be done in any activity or fragment that needs access to a MetaWearBoard object.

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

import com.mbientlab.metawear.android.BtleService;

public class ExampleActivity 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) { }

2.3. Download API Repository

Head over to our Java Github page: https://github.com/mbientlab/MetaWear-SDK-Android

You can clone the repository or simply download as a ZIP file:

>>>  git clone https://github.com/mbientlab/MetaWear-SDK-Android.git

2.4. About Java APIs

The Java APIs are native.

When you download our Repository, you will find:

  1. wrapper -> Scripts

  2. library -> This is where the main MetaWear Java source code resides

  3. Gradle files -> Gradle is an open-source build-automation system

We also have Java API documentation here. Please keep this documentation handy.

2.5. Template App

For those who want to build a simple MetaWear app or are new to Android development, we have provided a template for creating MetaWear apps which we will be using in this guide.

Download the Android template.

Import the project into Android Studio. All the code on this page will be added to the starter module.

2.6. Modules

MetaWear modules are sensors or peripherals that are on the board. Modules are represented by the Module interface and you retrieve references to them with the getModule function. The function may return null if the module is unsupported however all MetaWear boards have an accelerometer so we can safely assume the return value is non-null in this example.

In the onServiceConnected function, retrieve a reference to the Accelerometer module and configure the sensor.

private Accelerometer accelerometer;

public void onServiceConnected(ComponentName name, IBinder service) {
    metawear = ((BtleService.LocalBinder) service).getMetaWearBoard(settings.getBtDevice());

    accelerometer= metawear.getModule(Accelerometer.class);
           .odr(25f)       // Set sampling frequency to 25Hz, or closest valid ODR

2.6.1. UI Elements

Next, add a start and stop button to the fragment layout defined in the fragment_device_setup.xml file.

<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Start" android:id="@+id/acc_start"
    android:layout_alignParentEnd="true" />

<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Stop" android:id="@+id/acc_stop"
    android:layout_alignParentEnd="true" />

2.6.2. Data Stream

Switching back to the DeviceSetupActivityFragment class, override the onViewCreated function to add listeners for the start and stop buttons. The start button will setup a data route, subscribe to the data stream, and start the sensor. The stop button will stop the sensor and remove the data route.

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.acc_start).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            accelerometer.acceleration().addRouteAsync(new RouteBuilder() {
                public void configure(RouteElement source) {
                    source.stream(new Subscriber() {
                        public void apply(Data data, Object... env) {
                            Log.i("MainActivity", data.value(Acceleration.class).toString());
            }).continueWith(new Continuation<Route, Void>() {
                    public Void then(Task<Route> task) throws Exception {
                        return null;
    view.findViewById(R.id.acc_stop).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

2.6.3. Test the App

Load the app onto your Android device and connect to your board. Press the start button to see the acceleration data in Logcat.

When are you want learn more about the inner workings of the APIs and advanced functionalities of the MetaWear, go to the Next Steps.

2.7. Next Steps

Go over to our Developers Section to learn more about Java development.