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.




4 comments:

  1. Hello, my name is Nick. I'm trying to integrate a LightBlue bean into a project to provide the capability to toggle a digital pin via an Android app.

    Could you provide an example code for me to see how you would do it? I'm very new to Android app development.

    Thanks!

    ReplyDelete
  2. Thank you very much for this tutorial. I would appreciate if you could also explain how it is possible to read from a digital pin.

    ReplyDelete
  3. This tutorial was very informative! If possible, it would be great if you could provide some examples on reading from a digital pin. In addition, would the same logic work for reading an analog pin?

    ReplyDelete
    Replies
    1. Please check my latest post on how to read Bean digital and analog pins
      http://arduinohackingfun.blogspot.com/2017/10/android-reading-lightblue-bean-digital.html

      Delete