Sunday, February 14, 2016

Android reading LightBlue Bean sensor values

The previous blog shows example of using the LightBlue Bean Android SDK to read the bean's temperature sensor value.  In this blog, I am going to examine reading all other sensor values.  The programming model is simple and consistent, you call the bean.readXXX function with a callback handler, the SDK then invoke the callback with the measured sensor value.

Full source code available in Github

1. Read Battery Level

The following code snippet call to read the Bean's battery level, the callback's onResult is invoked with a BatteryLevel object represents the measured battery level, you can get the battery voltage and percentage of battery level.

       bean.readBatteryLevel(new Callback<BatteryLevel>() {
            @Override
            public void onResult(BatteryLevel result) {
                Log.d(TAG, "result: "+result.getPercentage());
                Log.d(TAG, "Battery percentage: "+result.getPercentage());
                Log.d(TAG, "Battery volt: "+result.getVoltage());
            }
        }
        );

2. Read Arduino's Power State.

The callback is invoked with a Boolean, True indicating the Bean's Arduino is ON and OFF if False.

   bean.readArduinoPowerState(new Callback<Boolean>() {
            @Override
            public void onResult(Boolean result) {
                Log.d(TAG, "result: "+result);
            }
        });

3. Read Acceleration in X, Y and Z - axis

For read acceleration, the parameter passed to the callback handler is an Acceleration object which contains acceleration on X, Y and Z axis, you can retrieve those values by the x(), y() and z() getter of the Acceleration object.

      bean.readAcceleration(new Callback<Acceleration>() {
            @Override
            public void onResult(Acceleration result) {
                Log.d(TAG, "result: "+result);
                Log.d(TAG, "Acceleration on X-axis: "+result.x());
                Log.d(TAG, "Acceleration on y-axis: "+result.y());
                Log.d(TAG, "Acceleration on z-axis: "+result.z());
            }
        });


4. Read AccelerometerRange

As per the Beans' Android SDK, the SDK shall invoke the callback function passing an AccelerometerRange object.  However, I found that the SDK is passing an Integer instead.  As a result, I am always getting Class Cast exception.  

       bean.readAccelerometerRange(new Callback<AccelerometerRange>() {
            @Override
            public void onResult(AccelerometerRange result) {
                Log.d(TAG, "result: "+result);
            }
        });

5. Read the RGB LED

You can also read the LED colour with the Bean's readLed function call, you will then get a LedColor object with contains 3 integers representing the R, G, B intensity of the LED.

      bean.readLed(new Callback<LedColor>() {
            @Override
            public void onResult(LedColor result) {
                Log.d(TAG, "result: "+result);
            }
        });

6. Reading Pins value

The SDK doesn't provide function to read the Pins value, but it shall be very easy to have a Bean's Sketch to put the Pins value to Scratch Data or Virtual Serial Port that an Android App can read.  Let me know if you are interested to see some example code on this.




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.