Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.1.1
* Android: Fix BluetoothDevice null-safety compile error under Kotlin 2.x
* Fix `BleCharacteristic` equality and `hashCode` to compare `properties` by value

## 2.1.0
* Add optional `queueId` parameter to all APIs
Expand Down
10 changes: 7 additions & 3 deletions lib/src/models/ble_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:universal_ble/src/universal_ble.g.dart';
import 'package:universal_ble/universal_ble.dart';

Expand Down Expand Up @@ -71,14 +71,18 @@ class BleCharacteristic {
bool operator ==(Object other) {
if (other is! BleCharacteristic) return false;
if (other.uuid != uuid) return false;
if (other.properties != properties) return false;
if (!listEquals(other.properties, properties)) return false;
if (other.metaData?.deviceId != metaData?.deviceId) return false;
if (other.metaData?.serviceId != metaData?.serviceId) return false;
return true;
}

@override
int get hashCode => uuid.hashCode ^ properties.hashCode ^ metaData.hashCode;
int get hashCode => Object.hash(
uuid,
Object.hashAll(properties),
metaData,
);
}

class BleDescriptor {
Expand Down
53 changes: 53 additions & 0 deletions test/ble_characteristic_equality_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:universal_ble/universal_ble.dart';

void main() {
group('BleCharacteristic equality', () {
test('equal when uuid and properties have equal content', () {
// Two separate list instances with the same content.
final a = BleCharacteristic('2a00', [
CharacteristicProperty.read,
CharacteristicProperty.notify,
], []);
final b = BleCharacteristic('2a00', [
CharacteristicProperty.read,
CharacteristicProperty.notify,
], []);

expect(a == b, isTrue);
// Equal objects MUST have equal hashCodes (equality contract).
expect(a.hashCode, b.hashCode);
});

test('not equal when properties differ', () {
final a = BleCharacteristic('2a00', [CharacteristicProperty.read], []);
final b = BleCharacteristic('2a00', [CharacteristicProperty.write], []);

expect(a == b, isFalse);
});

test('not equal when uuid differs', () {
final a = BleCharacteristic('2a00', [CharacteristicProperty.read], []);
final b = BleCharacteristic('2a01', [CharacteristicProperty.read], []);

expect(a == b, isFalse);
});

test('usable as Set/Map key with equal-content instances', () {
final a = BleCharacteristic('2a00', [
CharacteristicProperty.read,
CharacteristicProperty.notify,
], []);
final b = BleCharacteristic('2a00', [
CharacteristicProperty.read,
CharacteristicProperty.notify,
], []);

final set = {a};
expect(set.contains(b), isTrue);

final map = {a: true};
expect(map.containsKey(b), isTrue);
});
});
}
Loading