Skip to content

Commit

Permalink
fix: Android: distinguish between read and notification for future co…
Browse files Browse the repository at this point in the history
…mpletion
  • Loading branch information
hgrf committed Dec 16, 2024
1 parent abc5bcc commit c195c47
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,7 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status)
}

// called for both notifications & reads
public void onCharacteristicReceived(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value, int status)
public void onCharacteristicReceived(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value, int status, boolean fromReadOperation)
{
// GATT Service?
if (uuidStr(characteristic.getService().getUuid()) == "1800") {
Expand All @@ -2306,6 +2306,7 @@ public void onCharacteristicReceived(BluetoothGatt gatt, BluetoothGattCharacteri
if (primaryService != null) {
response.put("primary_service_uuid", uuidStr(primaryService.getUuid()));
}
response.put("from_read_operation", fromReadOperation ? 1 : 0);

invokeMethodUIThread("OnCharacteristicReceived", response);
}
Expand All @@ -2318,7 +2319,7 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
LogLevel level = LogLevel.DEBUG;
log(level, "onCharacteristicChanged:");
log(level, " chr: " + uuidStr(characteristic.getUuid()));
onCharacteristicReceived(gatt, characteristic, value, BluetoothGatt.GATT_SUCCESS);
onCharacteristicReceived(gatt, characteristic, value, BluetoothGatt.GATT_SUCCESS, false);
}

@Override
Expand All @@ -2330,7 +2331,7 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
log(level, "onCharacteristicRead:");
log(level, " chr: " + uuidStr(characteristic.getUuid()));
log(level, " status: " + gattErrorString(status) + " (" + status + ")");
onCharacteristicReceived(gatt, characteristic, value, status);
onCharacteristicReceived(gatt, characteristic, value, status, true);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/FlutterBluePlusPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
@"success": error == nil ? @(1) : @(0),
@"error_string": error ? [error localizedDescription] : @"success",
@"error_code": error ? @(error.code) : @(0),
// TODO: How to determine this on iOS / macOS?
@"from_read_operation": @(1),
} mutableCopy];

// remove if null
Expand Down
3 changes: 2 additions & 1 deletion lib/src/bluetooth_characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class BluetoothCharacteristic {
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid);
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid)
.where((p) => p.fromReadOperation == true);

// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmCharacteristicData> futureResponse = responseStream.first;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/bluetooth_msgs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ class BmCharacteristicData {
final bool success;
final int errorCode;
final String errorString;
final bool fromReadOperation;


BmCharacteristicData({
Expand All @@ -463,6 +464,7 @@ class BmCharacteristicData {
required this.success,
required this.errorCode,
required this.errorString,
required this.fromReadOperation,
});

factory BmCharacteristicData.fromMap(Map<dynamic, dynamic> json) {
Expand All @@ -475,6 +477,7 @@ class BmCharacteristicData {
success: json['success'] != 0,
errorCode: json['error_code'],
errorString: json['error_string'],
fromReadOperation: json.containsKey('from_read_operation') && (json['from_read_operation'] != 0),
);
}
}
Expand Down

0 comments on commit c195c47

Please sign in to comment.