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

Nullsafety #586

Merged
merged 35 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a64021c
Work in progress
okocsis Mar 26, 2021
cd829b2
lib/** nullability compliant;
okocsis Mar 29, 2021
44cf93f
Example:: updated to nullsafe-ready dependencies
okocsis Mar 29, 2021
7cff4d4
Example:: nullsafety + refactor WIP
okocsis Mar 30, 2021
991473f
Example:: nullsafe ready
okocsis Mar 30, 2021
fdac568
Example/test:: nullsafe ready
okocsis Mar 30, 2021
5ba2668
test:: using new mocking mechanism
okocsis Mar 31, 2021
44cb104
Java: added some null checks to make the calls to stop and destroy re…
okocsis Apr 1, 2021
67f1b5a
Example:: additional cleanup and refactor
okocsis Apr 1, 2021
a3277e7
TestFix:: the injected manager instance is not part of the Characteri…
okocsis Apr 5, 2021
ae7d4dc
ble_error seem to have rather nullable fields
okocsis Apr 5, 2021
8b1175b
TestFix: little refactor on characteristic_mixin regarding clearity a…
okocsis Apr 5, 2021
b58f8a0
TestFix:: lib_core_test:: tests now passing
okocsis Apr 5, 2021
682260b
Example:: removed comment-disabled code
okocsis Apr 6, 2021
2292ffc
Test:: all tests Passing now! 🎉
okocsis Apr 6, 2021
ca0cd3c
Enhanced BleError nullability, so that reason always has a default va…
okocsis Apr 6, 2021
46aa6bf
Travis fix
okocsis Apr 7, 2021
50cb727
disabled analysis for tests
okocsis Apr 7, 2021
2b7979b
Travis:: still fixing ios build
okocsis Apr 7, 2021
72f30f2
vscode:: stopped tracking settings file
okocsis Apr 7, 2021
b486789
travis:: removed my additions to ios example script
okocsis Apr 7, 2021
5f5c576
Characteristic:: Service field is now final
okocsis Apr 7, 2021
5f68124
gitignored andoid related staff
okocsis Apr 8, 2021
1da3feb
Add bluetooth-central key to README.md (#562)
wcoder Apr 8, 2021
56756e1
Travis: updated xcode version to 12.2
okocsis Apr 8, 2021
9d148b6
Merge commit '1da3feb70ed4f853145049fe3c5c4ec16080b287' into nullsafety
okocsis Apr 8, 2021
9bd3e05
BleError:: default reason text added
okocsis Apr 8, 2021
5442bfa
ScanResult::
okocsis Apr 8, 2021
fc130ba
CharacteristicsMixin::
okocsis Apr 8, 2021
0d040db
Test:: removed dummy print
okocsis Apr 8, 2021
b47cce9
ScanningMixin:: _prepareScanEventsStream() renamed to just _scanEvents
okocsis Apr 11, 2021
c9c8d60
ScanningMixin:: small refinement on naming stuff
okocsis Apr 12, 2021
86dafd6
Characteristic:: refactor on Futures to always complete with somethin…
okocsis Apr 12, 2021
a46482d
Revert "Characteristic:: refactor on Futures to always complete with …
okocsis Apr 13, 2021
d2fc7de
Merge commit 'aaa2009274a0b6c48a0eeae7565143f87b89ee2c' into nullsafety
okocsis Apr 13, 2021
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
2 changes: 1 addition & 1 deletion lib/characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Characteristic extends InternalCharacteristic {
/// The value can be written only if [isWritableWithResponse] or
/// [isWritableWithoutResponse] is `true` and argument [withResponse] is
/// set accordingly.
Future<void> write(
Future<Characteristic?> write(
mikolak marked this conversation as resolved.
Show resolved Hide resolved
Uint8List value,
bool withResponse, {
String? transactionId,
Expand Down
2 changes: 1 addition & 1 deletion lib/peripheral.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Peripheral {
/// [serviceUuid]. Optional [transactionId] could be used to cancel operation.
///
/// Will result in error if discovery was not done during this connection.
Future<Characteristic> writeCharacteristic(
Future<Characteristic?> writeCharacteristic(
mikolak marked this conversation as resolved.
Show resolved Hide resolved
String serviceUuid,
String characteristicUuid,
Uint8List value,
Expand Down
2 changes: 1 addition & 1 deletion lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Service extends InternalService {
/// [Characteristic.isWritableWithResponse] or
/// [Characteristic.isWritableWithoutResponse] is `true` and
/// [withResponse] is specified accordingly can be written to.
Future<Characteristic> writeCharacteristic(
Future<Characteristic?> writeCharacteristic(
mikolak marked this conversation as resolved.
Show resolved Hide resolved
String characteristicUuid,
Uint8List value,
bool withResponse, {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/_managers_for_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class ManagerForPeripheral {
String transactionId,
);

Future<Characteristic> writeCharacteristicForDevice(
Future<Characteristic?> writeCharacteristicForDevice(
mikolak marked this conversation as resolved.
Show resolved Hide resolved
Peripheral peripheral,
String serviceUuid,
String characteristicUuid,
Expand Down Expand Up @@ -101,7 +101,7 @@ abstract class ManagerForService {
String transactionId,
);

Future<Characteristic> writeCharacteristicForService(
Future<Characteristic?> writeCharacteristicForService(
mikolak marked this conversation as resolved.
Show resolved Hide resolved
Peripheral peripheral,
InternalService service,
String characteristicUuid,
Expand Down Expand Up @@ -145,7 +145,7 @@ abstract class ManagerForCharacteristic {
String transactionId,
);

Future<void> writeCharacteristicForIdentifier(
Future<Characteristic?> writeCharacteristicForIdentifier(
mikolak marked this conversation as resolved.
Show resolved Hide resolved
Peripheral peripheral,
InternalCharacteristic characteristic,
Uint8List value,
Expand Down
12 changes: 7 additions & 5 deletions lib/src/bridge/bluetooth_state_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
part of _internal;

mixin BluetoothStateMixin on FlutterBLE {
final Stream<dynamic> _adapterStateChanges =
const EventChannel(ChannelName.adapterStateChanges)
.receiveBroadcastStream();
final Stream<String> _adapterStateChanges =
const EventChannel(ChannelName.adapterStateChanges)
.receiveBroadcastStream()
.where((event) => event is String)
mikolak marked this conversation as resolved.
Show resolved Hide resolved
.cast<String>();

Future<void> enableRadio(String transactionId) async {
await _methodChannel.invokeMethod(
Expand All @@ -26,7 +28,7 @@ mixin BluetoothStateMixin on FlutterBLE {
}

Future<BluetoothState> state() => _methodChannel
.invokeMethod(MethodName.getState)
.invokeMethod<String>(MethodName.getState)
.then(_mapToBluetoothState);

Stream<BluetoothState> observeBluetoothState(bool emitCurrentValue) async* {
Expand All @@ -37,7 +39,7 @@ mixin BluetoothStateMixin on FlutterBLE {
yield* _adapterStateChanges.map(_mapToBluetoothState);
}

BluetoothState _mapToBluetoothState(dynamic rawValue) {
BluetoothState _mapToBluetoothState(String? rawValue) {
mikolak marked this conversation as resolved.
Show resolved Hide resolved
switch (rawValue) {
case "Unknown":
return BluetoothState.UNKNOWN;
Expand Down
190 changes: 113 additions & 77 deletions lib/src/bridge/characteristics_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ mixin CharacteristicsMixin on FlutterBLE {
String transactionId,
) =>
_methodChannel
.invokeMethod(
.invokeMethod<String>(
MethodName.readCharacteristicForIdentifier,
<String, dynamic>{
ArgumentName.characteristicIdentifier: characteristicIdentifier,
ArgumentName.transactionId: transactionId
},
)
.catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))))
.catchError((errorJson) async =>
throw BleError.fromJson(jsonDecode(errorJson.details))
)
.then((rawValue) {
String rawJsonValue = "";
if (rawValue is String) {
rawJsonValue = rawValue;
if (rawValue == null) {
mikolak marked this conversation as resolved.
Show resolved Hide resolved
return Uint8List.fromList([]);
}
return _parseCharacteristicWithValueWithTransactionIdResponse(
peripheral,
rawJsonValue
rawValue
).value;
});

Expand All @@ -38,7 +38,7 @@ mixin CharacteristicsMixin on FlutterBLE {
String transactionId,
) =>
_methodChannel
.invokeMethod(
.invokeMethod<String>(
MethodName.readCharacteristicForDevice,
<String, dynamic>{
ArgumentName.deviceIdentifier: peripheral.identifier,
Expand All @@ -47,16 +47,16 @@ mixin CharacteristicsMixin on FlutterBLE {
ArgumentName.transactionId: transactionId
},
)
.catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))))
.catchError((errorJson) async =>
throw BleError.fromJson(jsonDecode(errorJson.details))
)
.then((rawValue) {
String rawJsonValue = "";
if (rawValue is String) {
rawJsonValue = rawValue;
if (rawValue == null) {
throw Exception("rawValue cannot be null");
mikolak marked this conversation as resolved.
Show resolved Hide resolved
}
return _parseCharacteristicWithValueWithTransactionIdResponse(
peripheral,
rawJsonValue
rawValue
);
});

Expand All @@ -67,7 +67,7 @@ mixin CharacteristicsMixin on FlutterBLE {
String transactionId,
) =>
_methodChannel
.invokeMethod(
.invokeMethod<String>(
MethodName.readCharacteristicForService,
<String, dynamic>{
ArgumentName.serviceIdentifier: serviceIdentifier,
Expand All @@ -76,87 +76,123 @@ mixin CharacteristicsMixin on FlutterBLE {
},
)
.catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))))
throw BleError.fromJson(jsonDecode(errorJson.details))
)
.then((rawValue) {
String rawJsonValue = "";
if (rawValue is String) {
rawJsonValue = rawValue;
if (rawValue == null) {
throw Exception("rawValue cannot be null");
}
return _parseCharacteristicWithValueWithTransactionIdResponse(
peripheral,
rawJsonValue
rawValue
);
});

Future<void> writeCharacteristicForIdentifier(
Future<Characteristic?> writeCharacteristicForIdentifier(
Peripheral peripheral,
int characteristicIdentifier,
Uint8List value,
bool withResponse,
String transactionId,
) =>
_methodChannel.invokeMethod(
MethodName.writeCharacteristicForIdentifier,
<String, dynamic>{
ArgumentName.characteristicIdentifier: characteristicIdentifier,
ArgumentName.value: value,
ArgumentName.withResponse: withResponse,
ArgumentName.transactionId: transactionId,
},
).catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))));

Future<Characteristic> writeCharacteristicForDevice(
Peripheral peripheral,
String serviceUuid,
String characteristicUuid,
Uint8List value,
bool withResponse,
String transactionId) =>
_methodChannel
.invokeMethod(
MethodName.writeCharacteristicForDevice,
<String, dynamic>{
ArgumentName.deviceIdentifier: peripheral.identifier,
ArgumentName.serviceUuid: serviceUuid,
ArgumentName.characteristicUuid: characteristicUuid,
ArgumentName.value: value,
ArgumentName.withResponse: withResponse,
ArgumentName.transactionId: transactionId,
},
)
.catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))))
.then(
(rawJsonValue) =>
_parseCharacteristicResponse(peripheral, rawJsonValue),
);
) async {
final inv = _methodChannel.invokeMethod<String>(
MethodName.writeCharacteristicForIdentifier,
<String, dynamic>{
ArgumentName.characteristicIdentifier: characteristicIdentifier,
ArgumentName.value: value,
ArgumentName.withResponse: withResponse,
ArgumentName.transactionId: transactionId,
},
).catchError((errorJson) =>
throw BleError.fromJson(jsonDecode(errorJson.details))
).then((rawJsonValue) {
if (withResponse == false) {
return null;
}
if (rawJsonValue == null) {
return null;
}
return _parseCharacteristicResponse(peripheral, rawJsonValue);
});
if (withResponse == false) {
return null;
}
return await inv;
}



Future<Characteristic?> writeCharacteristicForDevice(
Peripheral peripheral,
String serviceUuid,
String characteristicUuid,
Uint8List value,
bool withResponse,
String transactionId
) async {
final inv = _methodChannel.invokeMethod<String>(
MethodName.writeCharacteristicForDevice,
<String, dynamic>{
ArgumentName.deviceIdentifier: peripheral.identifier,
ArgumentName.serviceUuid: serviceUuid,
ArgumentName.characteristicUuid: characteristicUuid,
ArgumentName.value: value,
ArgumentName.withResponse: withResponse,
ArgumentName.transactionId: transactionId,
},
)
.catchError((errorJson) async =>
throw BleError.fromJson(jsonDecode(errorJson.details))
)
.then((rawJsonValue) {
if (withResponse == false) {
return null;
}
if (rawJsonValue == null) {
return null;
}
return _parseCharacteristicResponse(peripheral, rawJsonValue);
});
if (withResponse == false) {
return null;
}
return await inv;
}

Future<Characteristic> writeCharacteristicForService(
Future<Characteristic?> writeCharacteristicForService(
Peripheral peripheral,
int serviceIdentifier,
String characteristicUuid,
Uint8List value,
bool withResponse,
String transactionId,
) =>
_methodChannel
.invokeMethod(
MethodName.writeCharacteristicForService,
<String, dynamic>{
ArgumentName.serviceIdentifier: serviceIdentifier,
ArgumentName.characteristicUuid: characteristicUuid,
ArgumentName.value: value,
ArgumentName.withResponse: withResponse,
ArgumentName.transactionId: transactionId,
},
)
.catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))))
.then(
(rawJsonValue) =>
_parseCharacteristicResponse(peripheral, rawJsonValue),
);
) async {
final inv = _methodChannel.invokeMethod(
MethodName.writeCharacteristicForService,
<String, dynamic>{
ArgumentName.serviceIdentifier: serviceIdentifier,
ArgumentName.characteristicUuid: characteristicUuid,
ArgumentName.value: value,
ArgumentName.withResponse: withResponse,
ArgumentName.transactionId: transactionId,
},
)
.catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))))
.then((rawJsonValue) {
if (withResponse == false) {
return null;
}
if (rawJsonValue == null) {
return null;
}
return _parseCharacteristicResponse(peripheral, rawJsonValue);
});
if (withResponse == false) {
return null;
}
return await inv;
}

Stream<Uint8List> monitorCharacteristicForIdentifier(
Peripheral peripheral,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/internal_ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class InternalBleManager
);

@override
Future<void> writeCharacteristicForIdentifier(
Future<Characteristic?> writeCharacteristicForIdentifier(
Peripheral peripheral,
InternalCharacteristic characteristic,
Uint8List value,
Expand All @@ -248,7 +248,7 @@ class InternalBleManager
);

@override
Future<Characteristic> writeCharacteristicForDevice(
Future<Characteristic?> writeCharacteristicForDevice(
Peripheral peripheral,
String serviceUuid,
String characteristicUuid,
Expand All @@ -265,7 +265,7 @@ class InternalBleManager
);

@override
Future<Characteristic> writeCharacteristicForService(
Future<Characteristic?> writeCharacteristicForService(
Peripheral peripheral,
InternalService service,
String characteristicUuid,
Expand Down
11 changes: 7 additions & 4 deletions test/characteristic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import 'test_util/characteristic_generator.dart';
import 'test_util/descriptor_generator.dart';

@GenerateMocks(
[Peripheral, ManagerForDescriptor, DescriptorWithValue],
customMocks: [
MockSpec<Service>(returnNullOnMissingStub: true),
]
[Peripheral, ManagerForDescriptor, DescriptorWithValue, Service],
// customMocks: [
// MockSpec<Service>(returnNullOnMissingStub: true),
// ]
)
void main() {
final peripheral = MockPeripheral();
Expand All @@ -44,6 +44,9 @@ void main() {
).thenAnswer(
(_) async => MockDescriptorWithValue()
);
when(
managerForCharacteristic.writeCharacteristicForIdentifier(any, any, any, any, any)
).thenAnswer((_) async => null);
final characteristicGenerator =
CharacteristicGenerator(managerForCharacteristic);
final descriptorGenerator =
Expand Down
Loading