This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2540 keyfob Accelerometer data conversion

Other Parts Discussed in Thread: CC2540

Hi,

I have CC2540 Mini Development Kit and I want to connect to CC2540 keyfob (as a BLE) using Android app.I am using keyfobdemo profile's Accelerometer Service. I got callback by enabling notification on keyfob, when keyfob moves. Now I want to extract data from BluetoothGattCharacteristic callback parameter such as how much acceleration(movement) done along X-axis or respective axis. So  how I can do that? Please guide me how I can do that conversion.

Thanks.

  • Hello Yogesh,

    I am not fluent in Android, but you should be able to get a good understanding of what you need to do by following this basic example. http://www.byteworks.us/Byte_Works/Blog/Entries/2012/8/20_Controlling_Bluetooth_LE_Devices_with_techBASIC.html

    There should also be an Android App with the source code for connecting to the KeyFob in the Smart Phone Example section of the BLE-wiki.http://processors.wiki.ti.com/index.php/CC254X_Smart_Phone_Examples

    Thanks,

  • Hello greenja,

                  Thank you for reply.From the link http://processors.wiki.ti.com/index.php/CC254X_Smart_Phone_Examples I downloaded the Source code for the “sensorTag” app. According to data conversion for Accelerometer sensor I applied the same logic in my code as follows:

     // Simplified code snippet …

    @Override

    public void onCharacteristicChanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic ) {

               super.onCharacteristicChanged( gatt, characteristic );

               // After notifications are enabled, all updates from the device on characteristic value changes will be posted here.

               // The accelerometer has the range [-2g, 2g] with unit (1/64)g.

               // To convert from unit (1/64)g to unit g we divide by 64.

               // (g = 9.81 m/s^2)

               // The z value is multiplied with -1 to coincide with how we have arbitrarily defined the positive y direction. (illustrated by the apps accelerometer image)

                if( ACCEL_X_UUID.equals( characteristic.getUuid() ) ) {

                     // Handle the X-axis notification.

                      byte[] value = characteristic.getValue();  

                      Integer x = (int) value[0];

                     // Works fine, here gives value for movement along  x direction.

               } else if( ACCEL_Y_UUID.equals( characteristic.getUuid() ) ) {

                     // Handle the Y-axis notification. 

                     byte[] value = characteristic.getValue();  

                     Integer y = (int) value[1];

                     // error : ArrayIndexOutOfBoundsException

               } else if( ACCEL_Z_UUID.equals( characteristic.getUuid() ) ) {

                    // Handle the Z-axis notification.

                    byte[] value = characteristic.getValue();  

                    Integer z = (int) value[2] * -1;

                   // error : ArrayIndexOutOfBoundsException

            }

    }

    Is am I doing any wrong calculation? Can you find out why this issue occurs?

  • The X, Y and Z UUID are independent and so are their characteristics.  You are thinking of the data as being one large array.  They are three independent arrays.  Leave the value at value[0] for all of them.

    Thanks,