Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distinguish between read and notification for future completion #1080

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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