Sunday, February 7, 2016

Mobile Arduino with LightBlue Bean and Android

Punch Through LightBlue Bean - A Bluetooth Low Energy (BLE) Arduino

I accidentally come to Punch Through website and attracted by it tiny BLE Arduino, ordered it and start hacking on it.

LightBlue Bean is a tiny wearable Arduino powered by a watch battery, it comes with a accelerometer, temperature sensor and RGB LED, the best part is that you can program and interface with it wirelessly through BLE.  More about the LightBlue Bean is available here.  It also has with an Android SDK, my hacking exercise this weekend is to write an Android program to interface with my new tiny toy.

Herewith is the step of creating an Android program to connect to the Bean and read the ambient temperature.   The exercise is based on Android Studio and Gradle,


HelloBean - Android program to wirelessly interface to LightBlue Bean


1. Create a simple blank Android application using Android Studio.
2. Add LightBean Android SDK but adding to  HelloBean/app/build.gradle

dependencies {
...
    compile 'com.punchthrough.bean.sdk:sdk:1.0.2'
...
}

3. To keep things simple, HelloBean app just has one single Activity (MainActivity) and a single screen.  We made this single Activity the listener of the Bean discovery and connection action.  The MainActivity class has a list of discovered bean (i.e. beans) and a target bean (i.e. bean).  We start Bean discovery when the App start (i.e. onCreate of the MainActivity) by calling the startDiscovery() of BeanManager.

public class MainActivity extends ...  implements BeanDiscoveryListener, BeanListener {
   final String TAG = "BlueBean";
    final List<Bean> beans = new ArrayList<>();
    Bean bean = null;
    TextView textView =null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 ...
        textView = (TextView)findViewById(R.id.main_text);
        textView.setText("Start Bluebean discovery ...");
        Log.d(TAG,"Start Bluebean discovery ...");
        BeanManager.getInstance().startDiscovery(this);
    }


4.  Add the following methods to implement BeanDiscoveryListener interface.  We connect to the first bean found of the discovery as our target bean by calling bean.connect().

  @Override
    public void onBeanDiscovered(Bean bean, int rssi) {
        Log.d(TAG,"A bean is found: "+bean);
        StringBuffer aBuf= new StringBuffer(textView.getText());
        aBuf.append("\n");
        aBuf.append(""+bean.getDevice().getName()+" address: "+bean.getDevice().getAddress());
        textView.setText(aBuf.toString());
        beans.add(bean);
    }

    @Override
    public void onDiscoveryComplete() {
        StringBuffer aBuf= new StringBuffer(textView.getText());
        aBuf.append("\n");
        aBuf.append("not more Bluebean found");
        textView.setText(aBuf.toString());
        for (Bean bean : beans) {
            Log.d(TAG, "Bean name: "+bean.getDevice().getName());
            Log.d(TAG, "Bean address: "+bean.getDevice().getAddress());
         }
        if(beans.size()>0){
            bean=beans.get(0);
            bean.connect(this,this);
        }
    }

5. Add the following methods to implement BeanListener.  Once the bean is connected, we query the bean's hardware information and it's measured temperature.

  // BeanListener Methods
    @Override
    public void onConnected() {
        Log.d(TAG,"connected to Bean!");
        bean.readDeviceInfo(new Callback<DeviceInfo>() {
            @Override
            public void onResult(DeviceInfo deviceInfo) {
                Log.d(TAG,deviceInfo.hardwareVersion());
                Log.d(TAG,deviceInfo.firmwareVersion());
                Log.d(TAG,deviceInfo.softwareVersion());
            }
        });

         bean.readTemperature(new Callback<Integer>() {
            @Override
            public void onResult(Integer data){
                Log.d(TAG, "Temperature: "+data);
              }
        });
    }

    @Override
    public void onConnectionFailed() {
        Log.d(TAG,"onConnectionFailed");
    }

    @Override
    public void onDisconnected() {
        Log.d(TAG,"onDisconnected");
    }

    @Override
    public void onSerialMessageReceived(byte[] data) {
        Log.d(TAG,"onSerialMessageReceived");
        Log.d(TAG,"data: "+data);
    }

    @Override
    public void onScratchValueChanged(ScratchBank bank, byte[] value) {
        Log.d(TAG,"onScratchValueChanged");
        Log.d(TAG,"bank: "+bank+"\tvalue: "+value);
    }

    @Override
    public void onError(BeanError error) {
        Log.d(TAG,"onError");
        Log.d(TAG,"error: "+error);
    }


As this exercise shows, interfacing to LightBlue Bean using Android through BLE is extremely easy, it's SDK also provide a very simple programming model.  Hacking this tiny wearable Arduino is fun and easy, we are going to have more projects on it.  Stay tune.



2 comments:

  1. Thanks for the guide. I tried to only implement the BeanDiscoveryListener, and added a Toast output to notify me whenever I'm inside onBeanDiscovered. I also added output if beans.size() > 0. It appears that my app can't find any beans even though the Bean Loader have no problem accessing the bean from the same phone.

    Is there anything else that needs to be set in order for this to work? Any special permissions? Something in the sketch running on the Bean?

    ReplyDelete
  2. Hey, thanks for publishing this as well. I downloaded, compiled, and am running HellowBean on a Nexus6p and can't connect to my bean. I have beanloader on this same device and it works. Is there some secret to getting HelloBean to work in this env? Thanks.

    ReplyDelete