3

Bluetooth LE デバイスと通信する必要があるアプリケーションに取り組んでいます。

これは CharacteristicNotification を設定するために使用するコードです

public boolean setCharacteristicNotification(
    BluetoothGattCharacteristic characteristic, boolean enable) {

if(mBluetoothAdapter == null || mBluetoothGatt == null) {
    Log.w(TAG, "BluetoothAdapter not initialized");
    return false;
}

Log.v(TAG, "setCharacteristicNotification(): uuid=" + characteristic.getUuid() + " enabled=" + enable);

boolean notifications = mBluetoothGatt.setCharacteristicNotification(characteristic, enable);

if(notifications) {
    Log.v(TAG, "setCharacteristicNotification(): Notifications are enabled");
}
else {
    Log.w(TAG, "setCharacteristicNotification(): Notifications are not enabled for characteristic " + characteristic);
}

BluetoothGattDescriptor desc = characteristic.getDescriptor(
        UUID.fromString(FBGattAttributes.CHARACTERISTIC_CLIENT_CONFIG));

desc.setValue(enable ?
                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
                new byte[]{0x00, 0x00}
);

boolean ok = mBluetoothGatt.writeDescriptor(desc);

if(ok){
    Log.v(TAG, "wrote descriptor value for notification: ok=" + ok);
}else{
    Log.w(TAG, "writeDescriptor failed: we will not get notifications=" + ok);
}


return ok;
}

このコードでは「mBluetoothGatt.writeDescriptor(desc);」時々 false を返します。それが BluetoothGatt から通知を受け取ることができない理由です。この問題を解決する方法がわかりません。

この問題は、OS 5.02 の LG G2 と 4.4 でのみ発生しました。問題はそれほど頻繁ではありませんでしたが、更新後は、初回を除いて毎回「false」になっていました。接続後に初めて通知を設定しようとすると、機能し、切断して接続しようとすると、常に False が返されます。再び機能させるには、アプリを強制終了して再起動する必要があります。なぜこれが機能しないのか、誰にもわかりませんか?前もって感謝します

4

1 に答える 1

4

Lollypop では、BluetoothGatt.java が更新され、アプリケーションが一度に複数の非同期操作を要求するのを防ぐ「デバイスがビジー」ブロックが含まれるようになりました。writeDescriptor から返されるものについては、BluetoothGatt.java :1029 を参照してください。

これを回避するには、コマンド キューを設定する必要がありました。基本的に、私のロジックが行うことは、失敗した非同期呼び出しをキューに入れ、BluetoothGattCallback で再試行を実行することです。

于 2015-07-28T21:18:52.180 に答える