2

次のコードを使用して、特性を読み取り、その値を取得しようとしています

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 アプリとしてコードを実行すると表示されます。同じエラーが発生します。

4

2 に答える 2

5

私の記憶が正しければ、これはメイン スレッドでの作業が多すぎるためである可能性があります。別のスレッドで、または Runnable または AsyncTask を使用して重い処理を行う必要があります。この SO の質問が役立つ場合があります。

于 2014-07-19T07:33:04.703 に答える
0

Read を呼び出すと、BluetoothGattCallback から特性を取得できます

BluetoothGattCallback mCallback=new BluetoothGattCallback() {

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

    };
};
于 2014-11-10T10:04:42.667 に答える