2. Java Development

This tutorial will take you through your first Java App.

2.1. Prerequisites

You should already have followed the instructions on the Android Installation and Java Installation page of our tutorial.

2.2. Freefall App

The free fall tutorial showcases advanced features of the MetaWear by using the on-board data processing to convert raw accelerometer data into a free fall metric. We will be building the app from scratch following the instructions outlined in the Android SDK documentation.

Source code for this tutorial is available on GitHub.

2.2.1. Project Setup

We kick off the free fall tutorial by showing you how to setup your project to use the MetaWear Android SDK.

2.2.2. Bluetooth LE Connection

Part B shows you how to connect to your MetaWear using the Android SDK.

2.2.3. Accelerometer Data Stream

Part C sets up a data stream of accelerometer sending live data from the sensor to your mobile device.

2.2.4. Data Processor

Part D utilizes the firmware data processor to manipulate the raw acceleration data on-board. By using the on-board data procesor, you can perform simple computations on the data without having to transmit the raw data.

2.2.5. On-Board Logging

In the last segment of this tutorial, we use the logger to record when the free fall events happened, and download the data at a later time.

For a deeper dive into the Android SDK, checkout the documentation page for links to additional information about the MetaWear SDKs.

2.2.6. Event Handling

Say you want your MetaWear to react to different events, for example, turn on the LED when the switch is pressed and turn off the LED by tapping the board. This is easily accomplished in a data route using the stream component however, it requires that the board remains connected to your Android device at all times. Using the react component, we can instead program the board to respond to these events offline thus removing the need for an external app to handle the events for us.

2.2.7. Route Setup

Before we go around adding react components in our data route, first, lets create the route that uses the Android device to manage events.

metawear.getModule(Switch.class).state().addRouteAsync(new RouteBuilder() {
    @Override
    public void configure(RouteComponent source) {
        source.stream(new Subscriber() {
            @Override
            public void apply(Data data, Object... env) {
                if (data.value(Boolean.class)) {
                    led.editPattern(Led.Color.BLUE, Led.PatternPreset.SOLID).commit();
                    led.play();
                }
            }
        });
    }
}).onSuccessTask(new Continuation<Route, Task<Route>>() {
    @Override
    public Task<Route> then(Task<Route> task) throws Exception {
        return accBosch.tap().addRouteAsync(new RouteBuilder() {
            @Override
            public void configure(RouteComponent source) {
                source.stream(new Subscriber() {
                    @Override
                    public void apply(Data data, Object... env) {
                        led.stop(true);
                    }
                });
            }
        });
    }
});

From here, replace the stream components with react components. For brevity, only the route functions are in the code snippets with the first route corresponding to the switch data.

// Switch reaction route
source.react(new RouteComponent.Action() {
    @Override
    public void execute(DataToken token) {
        led.editPattern(Led.Color.BLUE, Led.PatternPreset.SOLID).commit();
        led.play();
    }
});

// Tap reaction route
source.react(new RouteComponent.Action() {
    @Override
    public void execute(DataToken token) {
        led.stop(true);
    }
});

As you may have noticed, the if statement from the streaming route is not present in the reaction route. This is because only MetaWear commands can be used inside a reaction, that is, no Java language constructs like loops and no functions that reside outside of the MetaWear SDK. The commands wrapped by the execute method are run whenever there is new data or activity from its input source, meaning our reaction based route actually turns on the LED whenever the button state changes i.e. when both a press and release are detected.

To remedy this small bug, we will insert a data processing component in between the switch source and reaction so that only button presses are reacted upon. In this case, we use the comparator filter to check for switch values not equal to 0 (1 = pressed, 0 = released).

Our data route now looks as follows:

metawear.getModule(Switch.class).state().addRouteAsync(new RouteBuilder() {
    @Override
    public void configure(RouteComponent source) {
        source.filter(Comparison.NEQ, 0).react(new RouteComponent.Action() {
            @Override
            public void execute(DataToken token) {
                led.editPattern(Led.Color.BLUE, Led.PatternPreset.SOLID).commit();
                led.play();
            }
        });
    }
}).onSuccessTask(new Continuation<Route, Task<Route>>() {
    @Override
    public Task<Route> then(Task<Route> task) throws Exception {
        return accBosch.tap().addRouteAsync(new RouteBuilder() {
            @Override
            public void configure(RouteComponent source) {
                source.react(new RouteComponent.Action() {
                    @Override
                    public void execute(DataToken token) {
                        led.stop(true);
                    }
                });
            }
        });
    }
});

2.2.8. Cleanup

The MetaWear firmware only has limited resources available for reaction commands and data processing. Before you disconnect from the board or reload the app, remember to remove the routes that were created by calling Route interface’s remove function on the Route objects returned by addRouteAsync.

2.3. Freefall App

Check out the Multiple MetaWear example.

2.4. Sensor Fusion App

Check out the Sensor Fusion example.

2.5. Next steps

Advanced developers building more complex apps can refer to the Android Documentation for explicit installation instructions of the MetaWear library.