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 が返されます。再び機能させるには、アプリを強制終了して再起動する必要があります。なぜこれが機能しないのか、誰にもわかりませんか?前もって感謝します