Skip to content

Commit

Permalink
🐛 user correct uuid string in characteristicStartWith
Browse files Browse the repository at this point in the history
  • Loading branch information
Malte2036 committed Mar 22, 2024
1 parent c0b84cb commit c6030ff
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 11 deletions.
21 changes: 11 additions & 10 deletions lib/src/ftms_bluetooth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class FTMSBluetooth {
return;
}

print('Found FTMS device data characteristic: ${characteristicData.uuid}');
print(
'Found FTMS device data characteristic: ${characteristicData.uuid.str128}');

characteristicData.onValueReceived.listen((List<int> data) {
if (data.isEmpty) return;
Expand All @@ -56,7 +57,7 @@ class FTMSBluetooth {
return null;
}

print('Found Feature characteristic: ${characteristicData.uuid}');
print('Found Feature characteristic: ${characteristicData.uuid.str128}');

var data = await characteristicData.read();
print('featureData: $data');
Expand All @@ -79,7 +80,8 @@ class FTMSBluetooth {
return;
}

print('Found Machine Status characteristic: ${characteristicData.uuid}');
print(
'Found Machine Status characteristic: ${characteristicData.uuid.str128}');

characteristicData.onValueReceived.listen((data) {
if (data.isEmpty) {
Expand Down Expand Up @@ -136,10 +138,9 @@ class FTMSBluetooth {
return service != null;
}

static bool _characteristicStartWith(
static bool characteristicStartWith(
BluetoothCharacteristic characteristic, String startsWithValue) {
return characteristic.uuid
.toString()
return characteristic.uuid.str128
.toUpperCase()
.startsWith(startsWithValue.toUpperCase());
}
Expand All @@ -152,7 +153,7 @@ class FTMSBluetooth {
required bool characteristicWrite,
}) {
var chars = ftmsService.characteristics
.where((element) => _characteristicStartWith(element, startsWithUUID))
.where((element) => characteristicStartWith(element, startsWithUUID))
.toList();

if (chars.isEmpty) {
Expand All @@ -162,15 +163,15 @@ class FTMSBluetooth {
BluetoothCharacteristic char = chars[0];

if (characteristicRead && !char.properties.read) {
throw 'read not supported on characteristic: ${char.uuid}';
throw 'read not supported on characteristic: ${char.uuid.str128}';
}

if (characteristicNotify && !char.properties.notify) {
throw 'notify not supported on characteristic: ${char.uuid}';
throw 'notify not supported on characteristic: ${char.uuid.str128}';
}

if (characteristicWrite && !char.properties.write) {
throw 'write not supported on characteristic: ${char.uuid}';
throw 'write not supported on characteristic: ${char.uuid.str128}';
}

return char;
Expand Down
20 changes: 19 additions & 1 deletion test/ftms_bluetooth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:mockito/mockito.dart';
import 'ftms_bluetooth_test.mocks.dart';

// generate mock files: dart run build_runner build
@GenerateMocks([BluetoothDevice, BluetoothService])
@GenerateMocks([BluetoothDevice, BluetoothService, BluetoothCharacteristic])
void main() {
test("test getDeviceDataTypeByBluetoothId", () {
var res = FTMSBluetooth.getDeviceDataTypeByBluetoothId("22:22:A4:A6:02:00");
Expand Down Expand Up @@ -42,4 +42,22 @@ void main() {
expect(res, null);
});
});

group("test characteristicStartWith", () {
test("test characteristicStartWith with right uuid", () {
var char = MockBluetoothCharacteristic();
when(char.uuid).thenReturn(Guid("00005678-0000-0000-0000-000000000000"));

var res = FTMSBluetooth.characteristicStartWith(char, "00005678");
expect(res, true);
});

test("test characteristicStartWith with wrong uuid", () {
var char = MockBluetoothCharacteristic();
when(char.uuid).thenReturn(Guid("00005678-0000-0000-0000-000000000000"));

var res = FTMSBluetooth.characteristicStartWith(char, "00009999");
expect(res, false);
});
});
}
186 changes: 186 additions & 0 deletions test/ftms_bluetooth_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ class _FakeGuid_1 extends _i1.SmartFake implements _i2.Guid {
);
}

class _FakeBluetoothDevice_2 extends _i1.SmartFake
implements _i2.BluetoothDevice {
_FakeBluetoothDevice_2(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

class _FakeCharacteristicProperties_3 extends _i1.SmartFake
implements _i2.CharacteristicProperties {
_FakeCharacteristicProperties_3(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

/// A class which mocks [BluetoothDevice].
///
/// See the documentation for Mockito's code generation for more information.
Expand Down Expand Up @@ -425,3 +447,167 @@ class MockBluetoothService extends _i1.Mock implements _i2.BluetoothService {
),
) as _i2.DeviceIdentifier);
}

/// A class which mocks [BluetoothCharacteristic].
///
/// See the documentation for Mockito's code generation for more information.
class MockBluetoothCharacteristic extends _i1.Mock
implements _i2.BluetoothCharacteristic {
MockBluetoothCharacteristic() {
_i1.throwOnMissingStub(this);
}

@override
_i2.DeviceIdentifier get remoteId => (super.noSuchMethod(
Invocation.getter(#remoteId),
returnValue: _FakeDeviceIdentifier_0(
this,
Invocation.getter(#remoteId),
),
) as _i2.DeviceIdentifier);

@override
_i2.Guid get serviceUuid => (super.noSuchMethod(
Invocation.getter(#serviceUuid),
returnValue: _FakeGuid_1(
this,
Invocation.getter(#serviceUuid),
),
) as _i2.Guid);

@override
_i2.Guid get characteristicUuid => (super.noSuchMethod(
Invocation.getter(#characteristicUuid),
returnValue: _FakeGuid_1(
this,
Invocation.getter(#characteristicUuid),
),
) as _i2.Guid);

@override
_i2.Guid get uuid => (super.noSuchMethod(
Invocation.getter(#uuid),
returnValue: _FakeGuid_1(
this,
Invocation.getter(#uuid),
),
) as _i2.Guid);

@override
_i2.BluetoothDevice get device => (super.noSuchMethod(
Invocation.getter(#device),
returnValue: _FakeBluetoothDevice_2(
this,
Invocation.getter(#device),
),
) as _i2.BluetoothDevice);

@override
_i2.CharacteristicProperties get properties => (super.noSuchMethod(
Invocation.getter(#properties),
returnValue: _FakeCharacteristicProperties_3(
this,
Invocation.getter(#properties),
),
) as _i2.CharacteristicProperties);

@override
List<_i2.BluetoothDescriptor> get descriptors => (super.noSuchMethod(
Invocation.getter(#descriptors),
returnValue: <_i2.BluetoothDescriptor>[],
) as List<_i2.BluetoothDescriptor>);

@override
List<int> get lastValue => (super.noSuchMethod(
Invocation.getter(#lastValue),
returnValue: <int>[],
) as List<int>);

@override
_i4.Stream<List<int>> get lastValueStream => (super.noSuchMethod(
Invocation.getter(#lastValueStream),
returnValue: _i4.Stream<List<int>>.empty(),
) as _i4.Stream<List<int>>);

@override
_i4.Stream<List<int>> get onValueReceived => (super.noSuchMethod(
Invocation.getter(#onValueReceived),
returnValue: _i4.Stream<List<int>>.empty(),
) as _i4.Stream<List<int>>);

@override
bool get isNotifying => (super.noSuchMethod(
Invocation.getter(#isNotifying),
returnValue: false,
) as bool);

@override
_i2.DeviceIdentifier get deviceId => (super.noSuchMethod(
Invocation.getter(#deviceId),
returnValue: _FakeDeviceIdentifier_0(
this,
Invocation.getter(#deviceId),
),
) as _i2.DeviceIdentifier);

@override
_i4.Stream<List<int>> get value => (super.noSuchMethod(
Invocation.getter(#value),
returnValue: _i4.Stream<List<int>>.empty(),
) as _i4.Stream<List<int>>);

@override
_i4.Stream<List<int>> get onValueChangedStream => (super.noSuchMethod(
Invocation.getter(#onValueChangedStream),
returnValue: _i4.Stream<List<int>>.empty(),
) as _i4.Stream<List<int>>);

@override
_i4.Future<List<int>> read({int? timeout = 15}) => (super.noSuchMethod(
Invocation.method(
#read,
[],
{#timeout: timeout},
),
returnValue: _i4.Future<List<int>>.value(<int>[]),
) as _i4.Future<List<int>>);

@override
_i4.Future<void> write(
List<int>? value, {
bool? withoutResponse = false,
bool? allowLongWrite = false,
int? timeout = 15,
}) =>
(super.noSuchMethod(
Invocation.method(
#write,
[value],
{
#withoutResponse: withoutResponse,
#allowLongWrite: allowLongWrite,
#timeout: timeout,
},
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);

@override
_i4.Future<bool> setNotifyValue(
bool? notify, {
int? timeout = 15,
bool? forceIndications = false,
}) =>
(super.noSuchMethod(
Invocation.method(
#setNotifyValue,
[notify],
{
#timeout: timeout,
#forceIndications: forceIndications,
},
),
returnValue: _i4.Future<bool>.value(false),
) as _i4.Future<bool>);
}

0 comments on commit c6030ff

Please sign in to comment.