Skip to content

Commit

Permalink
[Refactor] replace secondaryServiceUuid with primaryServiceUuid
Browse files Browse the repository at this point in the history
(cherry picked from commit 2064c18)
  • Loading branch information
chipweinberger authored and tnc1997 committed Nov 23, 2024
1 parent afc826c commit d16b7e6
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 331 deletions.
33 changes: 15 additions & 18 deletions packages/flutter_blue_plus/lib/src/bluetooth_characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ final Guid cccdUuid = Guid("00002902-0000-1000-8000-00805f9b34fb");
class BluetoothCharacteristic {
final DeviceIdentifier remoteId;
final Guid serviceUuid;
final Guid? secondaryServiceUuid;
final Guid characteristicUuid;
final Guid? primaryServiceUuid;

BluetoothCharacteristic({
required this.remoteId,
required this.serviceUuid,
this.secondaryServiceUuid,
required this.characteristicUuid,
this.primaryServiceUuid,
});

BluetoothCharacteristic.fromProto(BmBluetoothCharacteristic p)
: remoteId = p.remoteId,
serviceUuid = p.serviceUuid,
secondaryServiceUuid = p.secondaryServiceUuid != null ? p.secondaryServiceUuid! : null,
characteristicUuid = p.characteristicUuid;
characteristicUuid = p.characteristicUuid,
primaryServiceUuid = p.primaryServiceUuid;

/// convenience accessor
Guid get uuid => characteristicUuid;
Expand Down Expand Up @@ -60,6 +60,7 @@ class BluetoothCharacteristic {
.where((p) => p.remoteId == remoteId)
.where((p) => p.serviceUuid == serviceUuid)
.where((p) => p.characteristicUuid == characteristicUuid)
.where((p) => p.primaryServiceUuid == primaryServiceUuid)
.where((p) => p.success == true)
.map((c) => c.value)
.newStreamWithInitialValue(lastValue);
Expand All @@ -71,6 +72,7 @@ class BluetoothCharacteristic {
.where((p) => p.remoteId == remoteId)
.where((p) => p.serviceUuid == serviceUuid)
.where((p) => p.characteristicUuid == characteristicUuid)
.where((p) => p.primaryServiceUuid == primaryServiceUuid)
.where((p) => p.success == true)
.map((c) => c.value);

Expand Down Expand Up @@ -106,13 +108,14 @@ class BluetoothCharacteristic {
remoteId: remoteId,
characteristicUuid: characteristicUuid,
serviceUuid: serviceUuid,
secondaryServiceUuid: secondaryServiceUuid,
primaryServiceUuid: primaryServiceUuid,
);

var responseStream = FlutterBluePlusPlatform.instance.onCharacteristicReceived
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid);
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid);

// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmCharacteristicData> futureResponse = responseStream.first;
Expand Down Expand Up @@ -174,16 +177,17 @@ class BluetoothCharacteristic {
remoteId: remoteId,
characteristicUuid: characteristicUuid,
serviceUuid: serviceUuid,
secondaryServiceUuid: secondaryServiceUuid,
writeType: writeType,
allowLongWrite: allowLongWrite,
value: value,
primaryServiceUuid: primaryServiceUuid,
);

var responseStream = FlutterBluePlusPlatform.instance.onCharacteristicWritten
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid);
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid);

// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmCharacteristicData> futureResponse = responseStream.first;
Expand Down Expand Up @@ -234,10 +238,10 @@ class BluetoothCharacteristic {
var request = BmSetNotifyValueRequest(
remoteId: remoteId,
serviceUuid: serviceUuid,
secondaryServiceUuid: secondaryServiceUuid,
characteristicUuid: characteristicUuid,
forceIndications: forceIndications,
enable: notify,
primaryServiceUuid: primaryServiceUuid,
);

// invoke
Expand All @@ -257,14 +261,7 @@ class BluetoothCharacteristic {
if (FlutterBluePlus._knownServices[remoteId] != null) {
for (var s in FlutterBluePlus._knownServices[remoteId]!.services) {
if (s.serviceUuid == serviceUuid) {
if (secondaryServiceUuid != null) {
// search includedServices (i.e. secondary services)
for (var s2 in s.includedServices) {
if (s2.serviceUuid == secondaryServiceUuid) {
return s2;
}
}
} else {
if (s.primaryServiceUuid == primaryServiceUuid) {
return s;
}
}
Expand All @@ -290,8 +287,8 @@ class BluetoothCharacteristic {
return 'BluetoothCharacteristic{'
'remoteId: $remoteId, '
'serviceUuid: $serviceUuid, '
'secondaryServiceUuid: $secondaryServiceUuid, '
'characteristicUuid: $characteristicUuid, '
'primaryServiceUuid: $primaryServiceUuid, '
'descriptors: $descriptors, '
'properties: $properties, '
'value: $lastValue'
Expand Down
16 changes: 11 additions & 5 deletions packages/flutter_blue_plus/lib/src/bluetooth_descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ class BluetoothDescriptor {
final Guid serviceUuid;
final Guid characteristicUuid;
final Guid descriptorUuid;
final Guid? primaryServiceUuid;

BluetoothDescriptor({
required this.remoteId,
required this.serviceUuid,
required this.characteristicUuid,
required this.descriptorUuid,
this.primaryServiceUuid,
});

BluetoothDescriptor.fromProto(BmBluetoothDescriptor p)
: remoteId = p.remoteId,
serviceUuid = p.serviceUuid,
characteristicUuid = p.characteristicUuid,
descriptorUuid = p.descriptorUuid;
descriptorUuid = p.descriptorUuid,
primaryServiceUuid = p.primaryServiceUuid;

/// convenience accessor
Guid get uuid => descriptorUuid;
Expand Down Expand Up @@ -80,16 +83,17 @@ class BluetoothDescriptor {
var request = BmReadDescriptorRequest(
remoteId: remoteId,
serviceUuid: serviceUuid,
secondaryServiceUuid: null,
characteristicUuid: characteristicUuid,
descriptorUuid: descriptorUuid,
primaryServiceUuid: primaryServiceUuid,
);

Stream<BmDescriptorData> responseStream = FlutterBluePlusPlatform.instance.onDescriptorRead
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.descriptorUuid == request.descriptorUuid);
.where((p) => p.descriptorUuid == request.descriptorUuid)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid);

// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmDescriptorData> futureResponse = responseStream.first;
Expand Down Expand Up @@ -132,17 +136,18 @@ class BluetoothDescriptor {
var request = BmWriteDescriptorRequest(
remoteId: remoteId,
serviceUuid: serviceUuid,
secondaryServiceUuid: null,
characteristicUuid: characteristicUuid,
descriptorUuid: descriptorUuid,
value: value,
primaryServiceUuid: primaryServiceUuid,
);

Stream<BmDescriptorData> responseStream = FlutterBluePlusPlatform.instance.onDescriptorWritten
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.descriptorUuid == request.descriptorUuid);
.where((p) => p.descriptorUuid == request.descriptorUuid)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid);

// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmDescriptorData> futureResponse = responseStream.first;
Expand Down Expand Up @@ -174,6 +179,7 @@ class BluetoothDescriptor {
'serviceUuid: $serviceUuid, '
'characteristicUuid: $characteristicUuid, '
'descriptorUuid: $descriptorUuid, '
'primaryServiceUuid: $primaryServiceUuid'
'lastValue: $lastValue'
'}';
}
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_blue_plus/lib/src/bluetooth_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class OnCharacteristicReceivedEvent {
remoteId: _response.remoteId,
characteristicUuid: _response.characteristicUuid,
serviceUuid: _response.serviceUuid,
secondaryServiceUuid: _response.secondaryServiceUuid);
primaryServiceUuid: _response.primaryServiceUuid);

/// the new data
List<int> get value => _response.value;
Expand All @@ -176,7 +176,7 @@ class OnCharacteristicWrittenEvent {
remoteId: _response.remoteId,
characteristicUuid: _response.characteristicUuid,
serviceUuid: _response.serviceUuid,
secondaryServiceUuid: _response.secondaryServiceUuid);
primaryServiceUuid: _response.primaryServiceUuid);

/// the new data
List<int> get value => _response.value;
Expand Down
21 changes: 10 additions & 11 deletions packages/flutter_blue_plus/lib/src/bluetooth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ part of flutter_blue_plus;
class BluetoothService {
final DeviceIdentifier remoteId;
final Guid serviceUuid;
final bool isPrimary;
final Guid? primaryServiceUuid;
final List<BluetoothCharacteristic> characteristics;
final List<BluetoothService> includedServices;

// for convenience
bool get isPrimary => primaryServiceUuid == null;

// for convenience
bool get isSecondary => primaryServiceUuid != null;

/// convenience accessor
Guid get uuid => serviceUuid;

BluetoothService.fromProto(BmBluetoothService p)
: remoteId = p.remoteId,
serviceUuid = p.serviceUuid,
isPrimary = p.isPrimary,
characteristics = p.characteristics
.map((c) => BluetoothCharacteristic.fromProto(c))
.toList(),
includedServices = p.includedServices
.map((s) => BluetoothService.fromProto(s))
.toList();
primaryServiceUuid = p.primaryServiceUuid,
characteristics = p.characteristics.map((c) => BluetoothCharacteristic.fromProto(c)).toList();

@override
String toString() {
return 'BluetoothService{'
'remoteId: $remoteId, '
'serviceUuid: $serviceUuid, '
'isPrimary: $isPrimary, '
'primaryService: $primaryServiceUuid, '
'characteristics: $characteristics, '
'includedServices: $includedServices'
'}';
}

Expand Down
Loading

0 comments on commit d16b7e6

Please sign in to comment.