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

Bug : connect and null check error #194

Merged
3 commits merged into from
Nov 16, 2021
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
19 changes: 12 additions & 7 deletions lib/src/ble/ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import 'package:nordic_nrf_mesh/src/ble/ble_manager_callbacks.dart';

const mtuSizeMax = 517;
const maxPacketSize = 20;
final macAddressServiceUuid = Platform.isAndroid ? Uuid.parse('00001400-0000-1000-8000-00805F9B34FB') : Uuid.parse('1400');
final macAddressCharacteristicUuid = Platform.isAndroid ? Uuid.parse('00001401-0000-1000-8000-00805F9B34FB') : Uuid.parse('1401');
final macAddressServiceUuid =
Platform.isAndroid ? Uuid.parse('00001400-0000-1000-8000-00805F9B34FB') : Uuid.parse('1400');
final macAddressCharacteristicUuid =
Platform.isAndroid ? Uuid.parse('00001401-0000-1000-8000-00805F9B34FB') : Uuid.parse('1401');
final meshProxyUuid = Platform.isAndroid ? Uuid.parse('00001828-0000-1000-8000-00805F9B34FB') : Uuid.parse('1828');
final meshProxyDataIn = Platform.isAndroid ? Uuid.parse('00002ADD-0000-1000-8000-00805F9B34FB') : Uuid.parse('2ADD');
final meshProxyDataOut = Platform.isAndroid ? Uuid.parse('00002ADE-0000-1000-8000-00805F9B34FB') : Uuid.parse('2ADE');
final meshProvisioningUuid = Platform.isAndroid ? Uuid.parse('00001827-0000-1000-8000-00805F9B34FB') : Uuid.parse('1827');
final meshProvisioningDataIn = Platform.isAndroid ? Uuid.parse('00002ADB-0000-1000-8000-00805F9B34FB') : Uuid.parse('2ADB');
final meshProvisioningDataOut = Platform.isAndroid ? Uuid.parse('00002ADC-0000-1000-8000-00805F9B34FB') : Uuid.parse('2ADC');
final clientCharacteristicConfigDescriptorUuid = Platform.isAndroid ? Uuid.parse('00002902-0000-1000-8000-00805f9b34fb') : Uuid.parse('2902');
final meshProvisioningUuid =
Platform.isAndroid ? Uuid.parse('00001827-0000-1000-8000-00805F9B34FB') : Uuid.parse('1827');
final meshProvisioningDataIn =
Platform.isAndroid ? Uuid.parse('00002ADB-0000-1000-8000-00805F9B34FB') : Uuid.parse('2ADB');
final meshProvisioningDataOut =
Platform.isAndroid ? Uuid.parse('00002ADC-0000-1000-8000-00805F9B34FB') : Uuid.parse('2ADC');
final clientCharacteristicConfigDescriptorUuid =
Platform.isAndroid ? Uuid.parse('00002902-0000-1000-8000-00805f9b34fb') : Uuid.parse('2902');
final enableNotificationValue = [0x01, 0x00];
const Duration kConnectionTimeout = Duration(seconds: 30);

Expand Down Expand Up @@ -143,7 +149,6 @@ abstract class BleManager<E extends BleManagerCallbacks> {
_connectCompleter.completeError(e);
}
},
test: (e) => e is BleManagerException,
);
break;
case DeviceConnectionState.disconnecting:
Expand Down
24 changes: 17 additions & 7 deletions lib/src/ble/ble_mesh_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class BleMeshManager<T extends BleMeshManagerCallbacks> extends BleManager<T> {

String _toMacAddress(final List<int> bytes) => bytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(':');

Future<String> getServiceMacId() async{
if(hasExpectedService(macAddressServiceUuid)){
Future<String> getServiceMacId() async {
if (hasExpectedService(macAddressServiceUuid)) {
final service = _discoveredServices.firstWhere((service) => service.serviceId == macAddressServiceUuid);
if (hasExpectedCharacteristicUuid(service, macAddressCharacteristicUuid)) {
final macIdChar = await bleInstance.readCharacteristic(QualifiedCharacteristic(characteristicId: macAddressCharacteristicUuid, serviceId: macAddressServiceUuid, deviceId: device!.id));
final macIdChar = await bleInstance.readCharacteristic(QualifiedCharacteristic(
characteristicId: macAddressCharacteristicUuid, serviceId: macAddressServiceUuid, deviceId: device!.id));
final macIdList = macIdChar.reversed.toList();
final macId = _toMacAddress(macIdList);
return macId.toUpperCase();
Expand Down Expand Up @@ -111,10 +112,19 @@ class BleMeshManager<T extends BleMeshManagerCallbacks> extends BleManager<T> {
}

StreamSubscription<List<int>> getDataOutSubscription(QualifiedCharacteristic qCharacteristic) =>
bleInstance.subscribeToCharacteristic(qCharacteristic).where((data) => data.isNotEmpty == true).listen(
(data) =>
callbacks!.onDataReceivedController.add(BleMeshManagerCallbacksDataReceived(device!, mtuSize, data)),
onError: (e, s) {
bleInstance.subscribeToCharacteristic(qCharacteristic).where((data) => data.isNotEmpty == true).listen((data) {
if (!(callbacks?.onDataReceivedController.isClosed == true) &&
callbacks!.onDataReceivedController.hasListener) {
callbacks!.onDataReceivedController.add(BleMeshManagerCallbacksDataReceived(device!, mtuSize, data));
} else {
if (!connectCompleter.isCompleted) {
// will notify for error as the connection could not be properly established
const _msg = 'no callback ready to receive data event';
_log(_msg);
connectCompleter.completeError(const BleManagerException(BleManagerFailureCode.callbacks, _msg));
}
}
}, onError: (e, s) {
const _msg = 'error in device data stream';
_log('$_msg : $e\n$s');
if (!(callbacks?.onErrorController.isClosed == true) && callbacks!.onErrorController.hasListener) {
Expand Down
16 changes: 8 additions & 8 deletions lib/src/mesh_manager_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class MeshManagerApi {
int keyIndex = 0,
}) async {
final status = _onV2MagicLevelSetStatusController.stream.firstWhere(
(element) => element.source == address,
(element) => element.source == address,
orElse: () => const MagicLevelSetStatusData(-1, -1, -1, -1, -1, -1),
);
await _methodChannel.invokeMethod('sendV2MagicLevel', {
Expand All @@ -470,7 +470,7 @@ class MeshManagerApi {
int keyIndex = 0,
}) async {
final status = _onV2MagicLevelGetStatusController.stream.firstWhere(
(element) => element.source == address,
(element) => element.source == address,
orElse: () => const MagicLevelGetStatusData(-1, -1, -1, -1, -1, -1),
);
await _methodChannel.invokeMethod('getV2MagicLevel', {
Expand Down Expand Up @@ -595,7 +595,7 @@ class MeshManagerApi {
int keyIndex = 0,
}) async {
final status = _onLightLightnessStatusController.stream.firstWhere(
(element) => element.source == address,
(element) => element.source == address,
orElse: () => const LightLightnessStatusData(-1, -1, -1, -1, -1, -1),
);
await _methodChannel.invokeMethod('sendLightLightness', {
Expand All @@ -616,7 +616,7 @@ class MeshManagerApi {
int keyIndex = 0,
}) async {
final status = _onLightCtlStatusController.stream.firstWhere(
(element) => element.source == address,
(element) => element.source == address,
orElse: () => const LightCtlStatusData(-1, -1, -1, -1, -1, -1, -1, -1),
);
await _methodChannel.invokeMethod('sendLightCtl', {
Expand All @@ -639,7 +639,7 @@ class MeshManagerApi {
int keyIndex = 0,
}) async {
final status = _onLightHslStatusController.stream.firstWhere(
(element) => element.source == address,
(element) => element.source == address,
orElse: () => const LightHslStatusData(-1, -1, -1, -1, -1, -1, -1),
);
await _methodChannel.invokeMethod('sendLightHsl', {
Expand Down Expand Up @@ -722,9 +722,9 @@ class MeshManagerApi {
final status = _onConfigNodeResetStatusController.stream
.where((element) => element.source == unicastAddress)
.timeout(const Duration(seconds: 3),
onTimeout: (sink) => sink.add(
const ConfigNodeResetStatus(-1, -1, false),
))
onTimeout: (sink) => sink.add(
const ConfigNodeResetStatus(-1, -1, false),
))
.first;
await _methodChannel.invokeMethod('deprovision', {'unicastAddress': unicastAddress});
return status;
Expand Down