Welcome back to this week’s #MyoCraft! This time, we’re going to talk about how you can use a really neat feature of Bluetooth Smart (aka Bluetooth LE) for seamless pairing with the Myo armband.

Traditionally, Bluetooth pairing has been… a bit of a pain. In the ideal case, the two devices you were pairing both had a keyboard and you would use them to input a random PIN. This was a key part of the Bluetooth security model and would confirm that the correct devices were connecting together. In the real world, however, most users ended up pairing dumb devices like a Bluetooth headset or stereo gateway. Since there was no way to enter a PIN on those, the user had to remember whichever number had been selected by the manufacturer. Naturally, it would be something extremely “secure”. Like 0000 or 1234.

A needless hassle, basically. That’s why with Bluetooth Smart the pairing process was simplified greatly. You can build an app such that a user simply picks from a list of compatible devices and connects immediately. That’s nice, but even then it may require you to build more UI than you really want to.

Enter the tap:

Attach to Adjacent Demo

The “tap” detection is based on the huge increase in signal strength you get from two Bluetooth radios being very close together. It’s also trivial take advantage of with a Myo armband. Check out our Android sample BackgroundService:

/*
 * Copyright (C) 2014 Thalmic Labs Inc.
 * Distributed under the Myo SDK license agreement. See LICENSE.txt for details.
 */

package com.thalmic.android.sample.background;

import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
import android.util.Log;  
import android.widget.Toast;

import com.thalmic.myo.AbstractDeviceListener;  
import com.thalmic.myo.DeviceListener;  
import com.thalmic.myo.Hub;  
import com.thalmic.myo.Myo;  
import com.thalmic.myo.Pose;

public class BackgroundService extends Service {  
    private static final String TAG = "BackgroundService";

    private Toast mToast;

    // Classes that inherit from AbstractDeviceListener can be used to receive events from Myo devices.
    // If you do not override an event, the default behavior is to do nothing.
    private DeviceListener mListener = new AbstractDeviceListener() {
        @Override
        public void onConnect(Myo myo, long timestamp) {
            showToast(getString(R.string.connected));
        }

        @Override
        public void onDisconnect(Myo myo, long timestamp) {
            showToast(getString(R.string.disconnected));
        }

        // onPose() is called whenever the Myo detects that the person wearing it has changed their pose, for example,
        // making a fist, or not making a fist anymore.
        @Override
        public void onPose(Myo myo, long timestamp, Pose pose) {
            // Show the name of the pose in a toast.
            showToast(getString(R.string.pose, pose.toString()));
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // First, we initialize the Hub singleton with an application identifier.
        Hub hub = Hub.getInstance();
        if (!hub.init(this, getPackageName())) {
            showToast("Couldn't initialize Hub");
            stopSelf();
            return;
        }

        // Disable standard Myo locking policy. All poses will be delivered.
        hub.setLockingPolicy(Hub.LockingPolicy.NONE);

        // Next, register for DeviceListener callbacks.
        hub.addListener(mListener);

        // Finally, scan for Myo devices and connect to the first one found that is very near.
        hub.attachToAdjacentMyo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // We don't want any callbacks when the Service is gone, so unregister the listener.
        Hub.getInstance().removeListener(mListener);

        Hub.getInstance().shutdown();
    }

    private void showToast(String text) {
        Log.w(TAG, text);
        if (mToast == null) {
            mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
        }
        mToast.show();
    }
}

The key bit is the hub.attachToAdjacentMyo() call. Once you’ve called that function, the user just has to tap the LED of their Myo armband to the back of their phone, and your app will get an onConnect event.

You should make sure you both tell your user to do this at the appropriate time, and tell them again when it worked. You can see we are doing this with a Toast in the Android sample, but there are a number of approaches you can take that don’t require any kind of complicated UI.

Also worth noting that, though this was an Android example, the same function exists on iOS. On Mac or PC, the pairing is handled for you by Myo Connect so you don’t need to worry about it.

That’s it for this week! Don’t forget to send us your submissions for #MyoCraft to MyoCraft@thalmic.com, or directly to me on Twitter (@thalmicdev).

See you next time!

Newsletter

Enter your email address and get all latest content delivered to your inbox every now and then.