Skip to content

Commit

Permalink
Prepare LoggerUtil for new BluetoothGattCallback methods (#848)
Browse files Browse the repository at this point in the history
These methods pass characteristic and descriptor values explicitly.
  • Loading branch information
dariuszseweryn authored Feb 14, 2024
1 parent d5c2a86 commit dbb55da
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,21 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
LoggerUtil.logCallback("onCharacteristicRead", gatt, status, characteristic, true);
byte[] characteristicValue = characteristic.getValue();
LoggerUtil.logCallback("onCharacteristicRead", gatt, status, characteristic, characteristicValue);
nativeCallbackDispatcher.notifyNativeReadCallback(gatt, characteristic, status);
super.onCharacteristicRead(gatt, characteristic, status);

if (readCharacteristicOutput.hasObservers() && !propagateErrorIfOccurred(
readCharacteristicOutput, gatt, characteristic, status, BleGattOperationType.CHARACTERISTIC_READ
)) {
readCharacteristicOutput.valueRelay.accept(new ByteAssociation<>(characteristic.getUuid(), characteristic.getValue()));
readCharacteristicOutput.valueRelay.accept(new ByteAssociation<>(characteristic.getUuid(), characteristicValue));
}
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
LoggerUtil.logCallback("onCharacteristicWrite", gatt, status, characteristic, false);
LoggerUtil.logCallback("onCharacteristicWrite", gatt, status, characteristic, null);
nativeCallbackDispatcher.notifyNativeWriteCallback(gatt, characteristic, status);
super.onCharacteristicWrite(gatt, characteristic, status);

Expand All @@ -136,7 +137,8 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
LoggerUtil.logCallback("onCharacteristicChanged", gatt, characteristic, true);
byte[] characteristicValue = characteristic.getValue();
LoggerUtil.logCallback("onCharacteristicChanged", gatt, characteristic, characteristicValue);
nativeCallbackDispatcher.notifyNativeChangedCallback(gatt, characteristic);
super.onCharacteristicChanged(gatt, characteristic);

Expand All @@ -150,27 +152,28 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
new CharacteristicChangedEvent(
characteristic.getUuid(),
characteristic.getInstanceId(),
characteristic.getValue()
characteristicValue
)
);
}
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
LoggerUtil.logCallback("onDescriptorRead", gatt, status, descriptor, true);
byte[] descriptorValue = descriptor.getValue();
LoggerUtil.logCallback("onDescriptorRead", gatt, status, descriptor, descriptorValue);
nativeCallbackDispatcher.notifyNativeDescriptorReadCallback(gatt, descriptor, status);
super.onDescriptorRead(gatt, descriptor, status);

if (readDescriptorOutput.hasObservers()
&& !propagateErrorIfOccurred(readDescriptorOutput, gatt, descriptor, status, BleGattOperationType.DESCRIPTOR_READ)) {
readDescriptorOutput.valueRelay.accept(new ByteAssociation<>(descriptor, descriptor.getValue()));
readDescriptorOutput.valueRelay.accept(new ByteAssociation<>(descriptor, descriptorValue));
}
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
LoggerUtil.logCallback("onDescriptorWrite", gatt, status, descriptor, false);
LoggerUtil.logCallback("onDescriptorWrite", gatt, status, descriptor, null);
nativeCallbackDispatcher.notifyNativeDescriptorWriteCallback(gatt, descriptor, status);
super.onDescriptorWrite(gatt, descriptor, status);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;

import androidx.annotation.Nullable;

import com.polidea.rxandroidble2.LogConstants;
import com.polidea.rxandroidble2.internal.RxBleLog;
import com.polidea.rxandroidble2.internal.operations.Operation;
Expand Down Expand Up @@ -94,30 +97,30 @@ public static void logOperationSkippedBecauseDisposedWhenAboutToRun(Operation op
}

public static void logCallback(String callbackName, BluetoothGatt gatt, int status, BluetoothGattCharacteristic characteristic,
boolean valueMatters) {
@Nullable byte[] valueBytes) {
if (!RxBleLog.isAtLeast(LogConstants.INFO)) {
return;
}
AttributeLogWrapper value = new AttributeLogWrapper(characteristic.getUuid(), characteristic.getValue(), valueMatters);
AttributeLogWrapper value = new AttributeLogWrapper(characteristic.getUuid(), valueBytes);
RxBleLog.i(commonMacMessage(gatt) + commonCallbackMessage() + commonStatusMessage() + commonValueMessage(),
callbackName, status, value);
}

public static void logCallback(String callbackName, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
boolean valueMatters) {
@Nullable byte[] valueBytes) {
if (!RxBleLog.isAtLeast(LogConstants.INFO)) {
return;
}
AttributeLogWrapper value = new AttributeLogWrapper(characteristic.getUuid(), characteristic.getValue(), valueMatters);
AttributeLogWrapper value = new AttributeLogWrapper(characteristic.getUuid(), valueBytes);
RxBleLog.i(commonMacMessage(gatt) + commonCallbackMessage() + commonValueMessage(), callbackName, value);
}

public static void logCallback(String callbackName, BluetoothGatt gatt, int status, BluetoothGattDescriptor descriptor,
boolean valueMatters) {
byte[] valueBytes) {
if (!RxBleLog.isAtLeast(LogConstants.INFO)) {
return;
}
AttributeLogWrapper value = new AttributeLogWrapper(descriptor.getUuid(), descriptor.getValue(), valueMatters);
AttributeLogWrapper value = new AttributeLogWrapper(descriptor.getUuid(), valueBytes);
RxBleLog.i(commonMacMessage(gatt) + commonCallbackMessage() + commonStatusMessage() + commonValueMessage(),
callbackName, status, value);
}
Expand Down Expand Up @@ -188,12 +191,12 @@ private static String commonValueMessage() {
return ", value=%s";
}

public static AttributeLogWrapper wrap(BluetoothGattCharacteristic characteristic, boolean valueMatters) {
return new AttributeLogWrapper(characteristic.getUuid(), characteristic.getValue(), valueMatters);
public static AttributeLogWrapper wrap(BluetoothGattCharacteristic characteristic, @Nullable byte[] characteristicValue) {
return new AttributeLogWrapper(characteristic.getUuid(), characteristicValue);
}

public static AttributeLogWrapper wrap(BluetoothGattDescriptor descriptor, boolean valueMatters) {
return new AttributeLogWrapper(descriptor.getUuid(), descriptor.getValue(), valueMatters);
public static AttributeLogWrapper wrap(BluetoothGattDescriptor descriptor, @Nullable byte[] descriptorValue) {
return new AttributeLogWrapper(descriptor.getUuid(), descriptorValue);
}

public static String getUuidToLog(UUID uuid) {
Expand All @@ -218,19 +221,17 @@ public static String getUuidSetToLog(Set<UUID> uuidSet) {
public static class AttributeLogWrapper {

private final UUID uuid;
private final byte[] value;
private final boolean valueMatters;
private final @Nullable byte[] value;

public AttributeLogWrapper(UUID uuid, byte[] value, boolean valueMatters) {
public AttributeLogWrapper(UUID uuid, @Nullable byte[] value) {
this.uuid = uuid;
this.value = value;
this.valueMatters = valueMatters;
}

@Override
public String toString() {
return "[uuid='" + getUuidToLog(uuid)
+ (valueMatters ? ("', hexValue=" + bytesToHex(value)) : "'")
+ (value != null ? ("', hexValue=" + bytesToHex(value)) : "'")
+ ']';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void accept(WriteOperationRetryStrategy.LongWriteFailure longWriteFailure
public String toString() {
return "CharacteristicLongWriteOperation{"
+ LoggerUtil.commonMacMessage(bluetoothGatt)
+ ", characteristic=" + LoggerUtil.wrap(bluetoothGattCharacteristic, false)
+ ", characteristic=" + LoggerUtil.wrap(bluetoothGattCharacteristic, null)
+ ", maxBatchSize=" + batchSizeProvider.getPayloadSizeLimit()
+ '}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected boolean startOperation(BluetoothGatt bluetoothGatt) {
public String toString() {
return "CharacteristicReadOperation{"
+ super.toString()
+ ", characteristic=" + LoggerUtil.wrap(bluetoothGattCharacteristic, false)
+ ", characteristic=" + LoggerUtil.wrap(bluetoothGattCharacteristic, null)
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected boolean startOperation(BluetoothGatt bluetoothGatt) {
public String toString() {
return "CharacteristicWriteOperation{"
+ super.toString()
+ ", characteristic=" + new LoggerUtil.AttributeLogWrapper(bluetoothGattCharacteristic.getUuid(), data, true)
+ ", characteristic=" + new LoggerUtil.AttributeLogWrapper(bluetoothGattCharacteristic.getUuid(), data)
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected boolean startOperation(BluetoothGatt bluetoothGatt) {
public String toString() {
return "DescriptorReadOperation{"
+ super.toString()
+ ", descriptor=" + LoggerUtil.wrap(bluetoothGattDescriptor, false)
+ ", descriptor=" + LoggerUtil.wrap(bluetoothGattDescriptor, null)
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected boolean startOperation(BluetoothGatt bluetoothGatt) {
public String toString() {
return "DescriptorWriteOperation{"
+ super.toString()
+ ", descriptor=" + new LoggerUtil.AttributeLogWrapper(bluetoothGattDescriptor.getUuid(), data, true)
+ ", descriptor=" + new LoggerUtil.AttributeLogWrapper(bluetoothGattDescriptor.getUuid(), data)
+ '}';
}
}

0 comments on commit dbb55da

Please sign in to comment.