2. Swift

2.1. Swift

This tutorial will take you through your first Swift App.

2.1.1. Prerequisites

Before we get started with code examples, you should be familiar with Swift and creating iOS apps. If you are new to iOS development, download Xcode and use the new project template to build a Hello World app for your iOS device.

You should already have followed the instructions on the Apple Installation page of our tutorial.


You can also build your apps in Objective-C, but it is highly recommended to begin all new development in Swift.

2.1.2. Template App

For those who want to build a simple MetaWear App or are new to iOS development, we have provided a template for creating MetaWear apps that we will be using in this guide. Advanced developers building more complex apps can refer to the iOS Documentation for explicit installation instructions of the MetaWear SDK.

Clone the StarterProject App Template:

>>> git clone --recurse-submodules https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS.git

Alternatively you can also download as a ZIP file from the site: https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS

Open StarterProject/StarterProject.xcworkspace with Xcode.

_images/xcode-1.png UI Elements

To add a start and stop button using interface builder, so open up iOS/Main.storyboard.

Then drag two buttons onto the Device View Controller and connect the buttons to DeviceViewController.swift.

See Apple Docs for detailed instruction on this.

_images/xcode-2.png Data Stream

Switching back to the DeviceViewController class, lets fill in the startPressed and stopPressed functions. The start button will enable the accelerometer data stream. The stop button will disable the stream.

@IBAction func startPressed(sender: AnyObject) {
    let board = device.board
    guard mbl_mw_metawearboard_lookup_module(board, MBL_MW_MODULE_ACCELEROMETER) != MODULE_TYPE_NA else {
        print("No accelerometer")
    let signal = mbl_mw_acc_get_acceleration_data_signal(board)
    mbl_mw_datasignal_subscribe(signal, bridge(obj: self)) { (context, data) in
        let _self: DeviceViewController = bridge(ptr: context!)
        let obj: MblMwCartesianFloat = data!.pointee.valueAs()

@IBAction func stopPressed(sender: AnyObject) {
    let board = device.board
    let signal = mbl_mw_acc_get_acceleration_data_signal(board)
_images/xcode-3.png Test the App

Connect your iOS device to your Mac.


Load the StartApp onto your iOS device and connect to your board.


Press the START button to see acceleration data being outputted to the log.


2.1.3. Sample App

We have created an iOS App for you that runs on an iPhone and includes many examples from graphing sensor data live to turning on and off GPIOs (and everything in between):

Clone the MetaWear-SampleApp-iOS App from Github:

>>> git clone https://github.com/mbientlab/MetaWear-SampleApp-iOS

Alternatively you can also download as a ZIP file from the site: https://github.com/mbientlab/MetaWear-SampleApp-iOS

Open the MetaWearApiTest.xcworkspace file with Xcode.

Make sure you have an iPhone plugged in to your Mac and run it:


Please take the time to get familiar with the code by taking the time to inspect and read each file under the MetaWearApiTest folder:


The App offers examples to:

  1. Read Battery

  2. Read RSSI (Bluetooth signal strength)

  3. Change the antenna power

  4. Read the switch (push-button) state

  5. Turn or blink the LED

  6. Read the temperature (from the on-board SOC sensor or the Bosch BMP280)

  7. Stream, log, graph Sensor Fusion (and change modes between euler, quaternions and more)

  8. Detect flatness

  9. Detect orientation

  10. Detect steps (walking)

  11. Stream, log, graph Accelerometer sensor (and change sampling frequency + settings)

  12. Stream, log, graph Gyroscope sensor (and change sampling frequency + settings)

  13. Stream, log, graph Magnetometer sensor (and change sampling frequency + settings)

  14. Read and set (analog and digital) GPIOs

  15. Buzz the attach coin vibration motor (MMR+ only)

  16. Set Beacon mode (iBeacon)

  17. Read pressure from Barometer sensor (and change settings)

  18. Read luminosity from Ambient Light sensor (and change settings)

  19. Read and Write on the I2C bus (you would need to add an external sensor to the I2C pins)

Here are a few screenshots from the MetaWearApiTest App after it has been connected to a MetaMotionR:

_images/metabase-1.png _images/metabase-2.png Next Steps


Don’t forget that you are responsible for all the necessary Developer accounts and Profiles needed to develop, test/debug, and release your App.

For advanced developers wanting a deeper dive into the iOS SDK, checkout the documentation page for links to additional information about the SDK.

Now go forth and build your own App!