【发布时间】:2014-07-12 06:50:44
【问题描述】:
我正在尝试使用以下代码读取特征并获取其值
private byte[] val;
mBluetoothLeService.readCharacteristic(characteristic);// reads it
val=characteristic.getValue();
String s=new String(val);
我的应用程序崩溃并显示错误“应用程序可能在其主线程上做的工作过多。”
我也试过
private String s;
mBluetoothLeService.readCharacteristic(characteristic);// reads it
s=characteristic.getStringValue(0);
但错误是一样的
我已经调试了我的代码以检查它是否获得了值
private String s;
mBluetoothLeService.readCharacteristic(characteristic);// reads it
s=characteristic.getStringValue(0);
system.out.println(s);
显示正确的值,但将代码作为 Android 应用运行。我得到同样的错误。
【问题讨论】:
-
如果我没记错的话,这可能是因为你在主线程上做了太多的工作,你应该在单独的线程中进行繁重的处理,或者使用 Runnable 或 AsyncTask。这个 SO 问题可能会对您有所帮助 link
-
同上@Kyriakos。您应该在另一个线程上运行该代码。如果之后您需要更新 UI,您可以使用
this.runOnUiThread() { ... }相应地更新您的 UI(假设这指的是您的 Activity,或者您可以使用片段中的getActivity()代替它)。您也可以使用new Handler(Looper.getMainLooper()).post(task);,其中任务是对您的 UI 的更新。
标签: android bluetooth-lowenergy android-bluetooth gatt