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

[Android] distinguish read and notification for future completion #1118

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 @@ -115,7 +115,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.receiveEvent != BmReceiveEventEnum.notify);

// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmCharacteristicData> futureResponse = responseStream.first;
Expand Down
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, int receiveEvent)
{
// 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("receive_event", receiveEvent);

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, 2); // see BmReceiveEventEnum
}

@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, 1); // see BmReceiveEventEnum
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ - (void)peripheral:(CBPeripheral *)peripheral
@"success": error == nil ? @(1) : @(0),
@"error_string": error ? [error localizedDescription] : @"success",
@"error_code": error ? @(error.code) : @(0),
@"receive_event": @(0), // see BmReceiveEventEnum
} mutableCopy];

// remove if null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ class BmReadCharacteristicRequest {
}
}

enum BmReceiveEventEnum {
unknown, // 0
read, // 1
notify, // 2
}
Copy link
Owner

@chipweinberger chipweinberger Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets just have this:

enum BmReceiveType {
  read, // 1
  notify, // 2
}

and then make it nullable, and have a BmReceiveType? _parseReceiveTypeFromInteger function


class BmCharacteristicData {
final DeviceIdentifier remoteId;
final Guid serviceUuid;
Expand All @@ -468,6 +474,7 @@ class BmCharacteristicData {
final bool success;
final int errorCode;
final String errorString;
final BmReceiveEventEnum receiveEvent;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets make this nullable



BmCharacteristicData({
Expand All @@ -479,6 +486,7 @@ class BmCharacteristicData {
required this.success,
required this.errorCode,
required this.errorString,
this.receiveEvent = BmReceiveEventEnum.unknown,
Copy link
Owner

@chipweinberger chipweinberger Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets make it required, for consistency

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving it optional means that we need not make this a breaking change and would be better if possible.

We could default to BmReceiveEventEnum.unknown or we could make the field nullable as done for pin.

#1119 (comment)

Copy link
Owner

@chipweinberger chipweinberger Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking is fine tho. I still dont really understand why we wouldn't want to break.

now is the perfect time to break since we have no other users of this interface.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

factory BmCharacteristicData.fromMap(Map<dynamic, dynamic> json) {
Expand All @@ -491,6 +499,7 @@ class BmCharacteristicData {
success: json['success'] != 0,
errorCode: json['error_code'],
errorString: json['error_string'],
receiveEvent: BmReceiveEventEnum.values[json['receive_event']],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets make an explicit 'parse' helper function

);
}
}
Expand Down