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.

SensorTag Gyroscope - change sampling period in Android code?

Hello, 

I am working with the SensorTag and Android. I have successfully changed the accelerometer's period using the BluetoothGattCharacteristic class. However, trying the same method on the gyroscope - which seems to have an analogous UUID for changing the period, yields app crashes. Here is my code:

if (sensor == Sensor.ACCELEROMETER) {
    byte transmitPeriod = 10; // in tens of milliseconds
    BluetoothGattCharacteristic characPeriod = serv.getCharacteristic(SensorTag.UUID_ACC_PERI);
    mBtLeService.writeCharacteristic(characPeriod, transmitPeriod);
    mBtLeService.waitIdle(GATT_TIMEOUT);
}
if (sensor == Sensor.GYROSCOPE) {
    byte transmitPeriod = 100; // in tens of milliseconds
    BluetoothGattCharacteristic characPeriod = serv.getCharacteristic(SensorTag.UUID_GYR_PERI);
    mBtLeService.writeCharacteristic(characPeriod, transmitPeriod);
    mBtLeService.waitIdle(GATT_TIMEOUT);
}

When I comment out the gyroscope code, the code runs fine and I see in the app the increased sampling rate of the accelerometer. However, when I add near identical code for the gyroscope, it crashes. Here are the logcat errors in case they help:

10-27 23:56:51.905: E/AndroidRuntime(22220): FATAL EXCEPTION: main
10-27 23:56:51.905: E/AndroidRuntime(22220): java.lang.RuntimeException: Error receiving broadcast Intent { act=ti.android.ble.common.ACTION_GATT_SERVICES_DISCOVERED flg=0x10 (has extras) } in ti.android.ble.sensortag.DeviceActivity$1@426c6050
10-27 23:56:51.905: E/AndroidRuntime(22220): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773)
10-27 23:56:51.905: E/AndroidRuntime(22220): at android.os.Handler.handleCallback(Handler.java:730)
10-27 23:56:51.905: E/AndroidRuntime(22220): at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 23:56:51.905: E/AndroidRuntime(22220): at android.os.Looper.loop(Looper.java:137)
10-27 23:56:51.905: E/AndroidRuntime(22220): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-27 23:56:51.905: E/AndroidRuntime(22220): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 23:56:51.905: E/AndroidRuntime(22220): at java.lang.reflect.Method.invoke(Method.java:525)
10-27 23:56:51.905: E/AndroidRuntime(22220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 23:56:51.905: E/AndroidRuntime(22220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 23:56:51.905: E/AndroidRuntime(22220): at dalvik.system.NativeStart.main(Native Method)
10-27 23:56:51.905: E/AndroidRuntime(22220): Caused by: java.lang.NullPointerException
10-27 23:56:51.905: E/AndroidRuntime(22220): at ti.android.ble.common.BluetoothLeService.writeCharacteristic(BluetoothLeService.java:277)
10-27 23:56:51.905: E/AndroidRuntime(22220): at ti.android.ble.sensortag.DeviceActivity.enableSensors(DeviceActivity.java:357)
10-27 23:56:51.905: E/AndroidRuntime(22220): at ti.android.ble.sensortag.DeviceActivity.displayServices(DeviceActivity.java:305)
10-27 23:56:51.905: E/AndroidRuntime(22220): at ti.android.ble.sensortag.DeviceActivity.access$0(DeviceActivity.java:292)
10-27 23:56:51.905: E/AndroidRuntime(22220): at ti.android.ble.sensortag.DeviceActivity$1.onReceive(DeviceActivity.java:451)
10-27 23:56:51.905: E/AndroidRuntime(22220): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
10-27 23:56:51.905: E/AndroidRuntime(22220): ... 9 more

Changing the gyroscope code above slight to:

if (characPeriod == null)
Toast.makeText(getApplicationContext(), "characteristic null", Toast.LENGTH_LONG).show();
//mBtLeService.writeCharacteristic(characPeriod, transmitPeriod);

I *do* get this toast, so apparently there's no characteristic for UUID_GYR_PERI? 

Does this mean I cannot change the gyroscope's period? Unfortunately, 1s is useless for my application =(

Cheers