2

I am Trying to read the characteristics and get its value using the following code

private byte[] val;

mBluetoothLeService.readCharacteristic(characteristic);// reads it
val=characteristic.getValue();
String s=new String(val);

My app crashes giving error "The application may be doing too much work on its main thread."

I have also tried

private String s;
 mBluetoothLeService.readCharacteristic(characteristic);// reads it
    s=characteristic.getStringValue(0);

but the error is same

I have debugged my code to check if its getting the value

private String s;
     mBluetoothLeService.readCharacteristic(characteristic);// reads it
        s=characteristic.getStringValue(0);
    system.out.println(s);

which displays the correct value but on Running the code as Android App. I get the same error.

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
Xauð Mùghal
  • 51
  • 1
  • 4
  • 1
    If i remember correctly this might be because you are doing too much work on your main thread, you should do heavy processing in a separate Thread or by using a Runnable or an AsyncTask. This SO question might help you [link](http://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread) – KikiTheMonk Jul 15 '14 at 06:12
  • Ditto @Kyriakos. You should run that code on another thread. If you need to update the UI after that you can use `this.runOnUiThread() { ... }` to update your UI accordingly (assuming this refers to your Activity, or you can use `getActivity()` from a fragment in place of this). You can also use `new Handler(Looper.getMainLooper()).post(task);` where task is the update to your UI. – MCLLC Nov 23 '15 at 14:34

2 Answers2

5

If i remember correctly this might be because you are doing too much work on your main thread, you should do heavy processing in a separate Thread or by using a Runnable or an AsyncTask. This SO question might help you.

Community
  • 1
  • 1
KikiTheMonk
  • 985
  • 10
  • 24
0

when you call Read can get the characterstic from BluetoothGattCallback

BluetoothGattCallback mCallback=new BluetoothGattCallback() {

    public void onCharacteristicRead(BluetoothGatt gatt, 
            BluetoothGattCharacteristic characteristic, int status) {

    };
};
Hamid-Ghasemi
  • 275
  • 2
  • 6