【问题标题】:Get value of BLE characteristic in GATT Android在 GATT Android 中获取 BLE 特性的值
【发布时间】: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


【解决方案1】:

如果我没记错的话,这可能是因为您在主线程上做了太多工作,您应该在单独的线程中进行繁重的处理,或者使用 Runnable 或 AsyncTask。 This SO question might help you.

【讨论】:

    【解决方案2】:

    当你调用 Read 可以从 BluetoothGattCallback 中获取特征

    BluetoothGattCallback mCallback=new BluetoothGattCallback() {
    
        public void onCharacteristicRead(BluetoothGatt gatt, 
                BluetoothGattCharacteristic characteristic, int status) {
    
        };
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多