简体   繁体   English

获取GATT Android中BLE特性的价值

[英]Get value of BLE characteristic in GATT Android

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. 会显示正确的值,但会以Android App的形式运行代码。 I get the same error. 我犯了同样的错误。

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. 如果我没记错的话,这可能是因为您在主线程上做的工作太多了,您应该在单独的线程中进行繁重的处理,或者使用Runnable或AsyncTask。 This SO question might help you. 这样的问题可能会对您有所帮助。

when you call Read can get the characterstic from BluetoothGattCallback 当您致电Read时,可以从BluetoothGattCallback获取特征

BluetoothGattCallback mCallback=new BluetoothGattCallback() {

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

    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM