在GATT Android中获得BLE特性的价值

Xau*_*hal 2 android bluetooth-lowenergy android-bluetooth gatt

我正在尝试使用以下代码读取特征并获取其值

private byte[] val;

mBluetoothLeService.readCharacteristic(characteristic);// reads it
val=characteristic.getValue();
String s=new String(val);
Run Code Online (Sandbox Code Playgroud)

我的应用程序崩溃给出错误"应用程序可能在其主线程上做了太多工作."

我也试过了

private String s;
 mBluetoothLeService.readCharacteristic(characteristic);// reads it
    s=characteristic.getStringValue(0);
Run Code Online (Sandbox Code Playgroud)

但错误是一样的

我调试了我的代码以检查它是否获得了值

private String s;
     mBluetoothLeService.readCharacteristic(characteristic);// reads it
        s=characteristic.getStringValue(0);
    system.out.println(s);
Run Code Online (Sandbox Code Playgroud)

它显示正确的值,但在运行代码为Android App时.我犯了同样的错误.

Kik*_*iki 5

如果我没记错,这可能是因为你在主线程上做了太多工作,你应该在一个单独的线程中或者使用Runnable或AsyncTask进行繁重的处理.这个问题可能会对你有所帮助.