From e9cc958af0358e66f9aa92c753789ab46993b8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rejman?= Date: Fri, 28 Feb 2020 13:07:54 +0100 Subject: [PATCH 01/12] Fix capitalization style for UUID and constant names (#437) * Fix capitalization of UUID in Service * Fix capitalization of UUID in ScanResult * Fix capitalization of UUID in Peripheral * Rename bytes to value * Fix capitalization of UUID in metadata strings in ScanResult * Fix constants capitalization in BleError * Revert "Fix capitalization of UUID in metadata strings in ScanResult" This reverts commit 08516ec65b7a5874331fff3813f97d4b2e511dcb. * Fix capitalization of UUID in managers for classes * Fix capitalization of UUID for InternalBleManager * Fix capitalization of UUID in CharacteristicsMixin * Fix capitalization of UUID in DevicesMixin * Rename bytes to value in internal classes --- lib/ble_manager.dart | 2 +- lib/characteristic.dart | 5 +- lib/error/ble_error.dart | 126 +++++++++++----------- lib/peripheral.dart | 34 +++--- lib/scan_result.dart | 13 +-- lib/service.dart | 18 ++-- lib/src/_managers_for_classes.dart | 28 ++--- lib/src/bridge/characteristics_mixin.dart | 44 ++++---- lib/src/bridge/devices_mixin.dart | 4 +- lib/src/internal_ble_manager.dart | 48 ++++----- 10 files changed, 162 insertions(+), 160 deletions(-) diff --git a/lib/ble_manager.dart b/lib/ble_manager.dart index d3595ec2..34fb4734 100644 --- a/lib/ble_manager.dart +++ b/lib/ble_manager.dart @@ -42,7 +42,7 @@ abstract class BleManager { } /// Cancels transaction's return, resulting in [BleError] with - /// [BleError.errorCode] set to [BleErrorCode.OperationCancelled] being returned + /// [BleError.errorCode] set to [BleErrorCode.operationCancelled] being returned /// from transaction's Future. /// /// The operation might be cancelled if it hadn't yet started or be run diff --git a/lib/characteristic.dart b/lib/characteristic.dart index 9bb3a9e8..070de610 100644 --- a/lib/characteristic.dart +++ b/lib/characteristic.dart @@ -36,6 +36,7 @@ class Characteristic extends InternalCharacteristic { /// True if this characteristic can be monitored via notifications. bool isNotifiable; + /// True if this characteristic can be monitored via indications. bool isIndicatable; @@ -70,14 +71,14 @@ class Characteristic extends InternalCharacteristic { /// [isWritableWithoutResponse] is `true` and argument [withResponse] is /// set accordingly. Future write( - Uint8List bytes, + Uint8List value, bool withResponse, { String transactionId, }) => _manager.writeCharacteristicForIdentifier( service.peripheral, this, - bytes, + value, withResponse, transactionId ?? TransactionIdGenerator.getNextId(), ); diff --git a/lib/error/ble_error.dart b/lib/error/ble_error.dart index 1b1ddbd9..dde9b639 100644 --- a/lib/error/ble_error.dart +++ b/lib/error/ble_error.dart @@ -1,15 +1,15 @@ part of flutter_ble_lib; abstract class _BleErrorMetadata { - static const String ERROR_CODE = "errorCode"; - static const String ATT_ERROR_CODE = "attErrorCode"; - static const String ANDROID_ERROR_CODE = "androidErrorCode"; - static const String REASON = "reason"; - static const String DEVICE_ID = "deviceID"; - static const String SERVICE_UUID = "serviceUUID"; - static const String CHARACTERISTIC_UUID = "characteristicUUID"; - static const String DESCRIPTOR_UUID = "descriptorUUID"; - static const String INTERNAL_MESSAGE = "internalMessage"; + static const String errorCode = "errorCode"; + static const String attErrorCode = "attErrorCode"; + static const String androidErrorCode = "androidErrorCode"; + static const String reason = "reason"; + static const String deviceId = "deviceID"; + static const String serviceUuid = "serviceUUID"; + static const String characteristicUuid = "characteristicUUID"; + static const String descriptorUuid = "descriptorUUID"; + static const String internalMessage = "internalMessage"; } class BleError { @@ -20,21 +20,21 @@ class BleError { String reason; String deviceID; - String serviceUUID; - String characteristicUUID; - String descriptorUUID; + String serviceUuid; + String characteristicUuid; + String descriptorUuid; String internalMessage; BleError.fromJson(Map json) - : errorCode = BleErrorCode(json[_BleErrorMetadata.ERROR_CODE]), - attErrorCode = json[_BleErrorMetadata.ATT_ERROR_CODE], - androidErrorCode = json[_BleErrorMetadata.ANDROID_ERROR_CODE], - reason = json[_BleErrorMetadata.REASON], - deviceID = json[_BleErrorMetadata.DEVICE_ID], - serviceUUID = json[_BleErrorMetadata.SERVICE_UUID], - characteristicUUID = json[_BleErrorMetadata.CHARACTERISTIC_UUID], - descriptorUUID = json[_BleErrorMetadata.DESCRIPTOR_UUID], - internalMessage = json[_BleErrorMetadata.INTERNAL_MESSAGE]; + : errorCode = BleErrorCode(json[_BleErrorMetadata.errorCode]), + attErrorCode = json[_BleErrorMetadata.attErrorCode], + androidErrorCode = json[_BleErrorMetadata.androidErrorCode], + reason = json[_BleErrorMetadata.reason], + deviceID = json[_BleErrorMetadata.deviceId], + serviceUuid = json[_BleErrorMetadata.serviceUuid], + characteristicUuid = json[_BleErrorMetadata.characteristicUuid], + descriptorUuid = json[_BleErrorMetadata.descriptorUuid], + internalMessage = json[_BleErrorMetadata.internalMessage]; @override String toString() => "BleError (" @@ -45,57 +45,57 @@ class BleError { "reason: $reason, " "internal message: $internalMessage, " "device ID: $deviceID, " - "service UUID: $serviceUUID, " - "characteristic UUID: $characteristicUUID, " - "descriptor UUID: $descriptorUUID)"; + "service UUID: $serviceUuid, " + "characteristic UUID: $characteristicUuid, " + "descriptor UUID: $descriptorUuid)"; } class BleErrorCode { - static const int UnknownError = 0; - static const int BluetoothManagerDestroyed = 1; - static const int OperationCancelled = 2; - static const int OperationTimedOut = 3; - static const int OperationStartFailed = 4; - static const int InvalidIdentifiers = 5; + static const int unknownError = 0; + static const int bluetoothManagerDestroyed = 1; + static const int operationCancelled = 2; + static const int operationTimedOut = 3; + static const int operationStartFailed = 4; + static const int invalidIdentifiers = 5; - static const int BluetoothUnsupported = 100; - static const int BluetoothUnauthorized = 101; - static const int BluetoothPoweredOff = 102; - static const int BluetoothInUnknownState = 103; - static const int BluetoothResetting = 104; - static const int BluetoothStateChangeFailed = 105; + static const int bluetoothUnsupported = 100; + static const int bluetoothUnauthorized = 101; + static const int bluetoothPoweredOff = 102; + static const int bluetoothInUnknownState = 103; + static const int bluetoothResetting = 104; + static const int bluetoothStateChangeFailed = 105; - static const int DeviceConnectionFailed = 200; - static const int DeviceDisconnected = 201; - static const int DeviceRSSIReadFailed = 202; - static const int DeviceAlreadyConnected = 203; - static const int DeviceNotFound = 204; - static const int DeviceNotConnected = 205; - static const int DeviceMTUChangeFailed = 206; + static const int deviceConnectionFailed = 200; + static const int deviceDisconnected = 201; + static const int deviceRSSIReadFailed = 202; + static const int deviceAlreadyConnected = 203; + static const int deviceNotFound = 204; + static const int deviceNotConnected = 205; + static const int deviceMTUChangeFailed = 206; - static const int ServicesDiscoveryFailed = 300; - static const int IncludedServicesDiscoveryFailed = 301; - static const int ServiceNotFound = 302; - static const int ServicesNotDiscovered = 303; + static const int servicesDiscoveryFailed = 300; + static const int includedServicesDiscoveryFailed = 301; + static const int serviceNotFound = 302; + static const int servicesNotDiscovered = 303; - static const int CharacteristicsDiscoveryFailed = 400; - static const int CharacteristicWriteFailed = 401; - static const int CharacteristicReadFailed = 402; - static const int CharacteristicNotifyChangeFailed = 403; - static const int CharacteristicNotFound = 404; - static const int CharacteristicsNotDiscovered = 405; - static const int CharacteristicInvalidDataFormat = 406; + static const int characteristicsDiscoveryFailed = 400; + static const int characteristicWriteFailed = 401; + static const int characteristicReadFailed = 402; + static const int characteristicNotifyChangeFailed = 403; + static const int characteristicNotFound = 404; + static const int characteristicsNotDiscovered = 405; + static const int characteristicInvalidDataFormat = 406; - static const int DescriptorsDiscoveryFailed = 500; - static const int DescriptorWriteFailed = 501; - static const int DescriptorReadFailed = 502; - static const int DescriptorNotFound = 503; - static const int DescriptorsNotDiscovered = 504; - static const int DescriptorInvalidDataFormat = 505; - static const int DescriptorWriteNotAllowed = 506; + static const int descriptorsDiscoveryFailed = 500; + static const int descriptorWriteFailed = 501; + static const int descriptorReadFailed = 502; + static const int descriptorNotFound = 503; + static const int descriptorsNotDiscovered = 504; + static const int descriptorInvalidDataFormat = 505; + static const int descriptorWriteNotAllowed = 506; - static const int ScanStartFailed = 600; - static const int LocationServicesDisabled = 601; + static const int scanStartFailed = 600; + static const int locationServicesDisabled = 601; int value; diff --git a/lib/peripheral.dart b/lib/peripheral.dart index 5a24b364..c8298878 100644 --- a/lib/peripheral.dart +++ b/lib/peripheral.dart @@ -122,39 +122,39 @@ class Peripheral { /// Reads value of [Characteristic] matching specified UUIDs. /// - /// Returns value of characteristic with [characteristicUUID] for service with - /// [serviceUUID]. Optional [transactionId] could be used to cancel operation. + /// Returns value of characteristic with [characteristicUuid] for service with + /// [serviceUuid]. Optional [transactionId] could be used to cancel operation. /// /// Will result in error if discovery was not done during this connection. Future readCharacteristic( - String serviceUUID, - String characteristicUUID, { + String serviceUuid, + String characteristicUuid, { String transactionId, }) => _manager.readCharacteristicForDevice( this, - serviceUUID, - characteristicUUID, + serviceUuid, + characteristicUuid, transactionId ?? TransactionIdGenerator.getNextId(), ); /// Writes value of [Characteristic] matching specified UUIDs. /// - /// Writes [value] to characteristic with [characteristicUUID] for service with - /// [serviceUUID]. Optional [transactionId] could be used to cancel operation. + /// Writes [value] to characteristic with [characteristicUuid] for service with + /// [serviceUuid]. Optional [transactionId] could be used to cancel operation. /// /// Will result in error if discovery was not done during this connection. Future writeCharacteristic( - String serviceUUID, - String characteristicUUID, + String serviceUuid, + String characteristicUuid, Uint8List value, bool withResponse, { String transactionId, }) => _manager.writeCharacteristicForDevice( this, - serviceUUID, - characteristicUUID, + serviceUuid, + characteristicUuid, value, withResponse, transactionId ?? TransactionIdGenerator.getNextId(), @@ -221,21 +221,21 @@ class Peripheral { /// matching specified UUIDs. /// /// Emits [CharacteristicWithValue] for every observed change of the - /// characteristic specified by [serviceUUID] and [characteristicUUID] + /// characteristic specified by [serviceUuid] and [characteristicUuid] /// If notifications are enabled they will be used in favour of indications. /// Optional [transactionId] could be used to cancel operation. Unsubscribing /// from the stream cancels monitoring. /// /// Will result in error if discovery was not done during this connection. Stream monitorCharacteristic( - String serviceUUID, - String characteristicUUID, { + String serviceUuid, + String characteristicUuid, { String transactionId, }) => _manager.monitorCharacteristicForDevice( this, - serviceUUID, - characteristicUUID, + serviceUuid, + characteristicUuid, transactionId ?? TransactionIdGenerator.getNextId(), ); diff --git a/lib/scan_result.dart b/lib/scan_result.dart index 04921ce4..112b3c65 100644 --- a/lib/scan_result.dart +++ b/lib/scan_result.dart @@ -20,11 +20,12 @@ class ScanResult { /// Signal strength of the peripheral in dBm. int rssi; + /// An indicator whether the peripheral is connectable (iOS only). bool isConnectable; /// A list of UUIDs found in the overflow area of the advertisement data (iOS only). - List overflowServiceUUIDs; + List overflowServiceUuids; /// A packet of data advertised by the peripheral. AdvertisementData advertisementData; @@ -33,7 +34,7 @@ class ScanResult { : peripheral = Peripheral.fromJson(json, manager), rssi = json[_ScanResultMetadata.rssi], isConnectable = json[_ScanResultMetadata.isConnectable], - overflowServiceUUIDs = json[_ScanResultMetadata.overflowServiceUuids], + overflowServiceUuids = json[_ScanResultMetadata.overflowServiceUuids], advertisementData = AdvertisementData._fromJson(json); } @@ -47,7 +48,7 @@ class AdvertisementData { Map serviceData; /// A list of service UUIDs. - List serviceUUIDs; + List serviceUuids; /// The local name of the [Peripheral]. Might be different than /// [Peripheral.name]. @@ -57,18 +58,18 @@ class AdvertisementData { int txPowerLevel; /// A list of solicited service UUIDs. - List solicitedServiceUUIDs; + List solicitedServiceUuids; AdvertisementData._fromJson(Map json) : manufacturerData = _decodeBase64OrNull(json[_ScanResultMetadata.manufacturerData]), serviceData = _getServiceDataOrNull(json[_ScanResultMetadata.serviceData]), - serviceUUIDs = + serviceUuids = _mapToListOfStringsOrNull(json[_ScanResultMetadata.serviceUuids]), localName = json[_ScanResultMetadata.localName], txPowerLevel = json[_ScanResultMetadata.txPowerLevel], - solicitedServiceUUIDs = _mapToListOfStringsOrNull( + solicitedServiceUuids = _mapToListOfStringsOrNull( json[_ScanResultMetadata.solicitedServiceUuids]); static Map _getServiceDataOrNull( diff --git a/lib/service.dart b/lib/service.dart index 90742dcd..2b00d663 100644 --- a/lib/service.dart +++ b/lib/service.dart @@ -30,7 +30,7 @@ class Service extends InternalService { _manager.characteristicsForService(this); /// Writes the [value] to the [Characteristic] identified by - /// [characteristicUUID]. + /// [characteristicUuid]. /// /// It returns a [Future] that completes with the [Characteristic] for the /// convenience of chaining operations. @@ -40,7 +40,7 @@ class Service extends InternalService { /// [Characteristic.isWritableWithoutResponse] is `true` and /// [withResponse] is specified accordingly can be written to. Future writeCharacteristic( - String characteristicUUID, + String characteristicUuid, Uint8List value, bool withResponse, { String transactionId, @@ -48,30 +48,30 @@ class Service extends InternalService { _manager.writeCharacteristicForService( peripheral, this, - characteristicUUID, + characteristicUuid, value, withResponse, transactionId ?? TransactionIdGenerator.getNextId()); - /// Reads the value of a [Characteristic] identified by [characteristicUUID]. + /// Reads the value of a [Characteristic] identified by [characteristicUuid]. /// /// It returns a [Future] that completes with [CharacteristicWithValue], /// which is just a [Characteristic] but with an additonal `value` /// property of type [Uint8List]. Only [Characteristic] where /// [Characteristic.isReadable] is `true` can be read. Future readCharacteristic( - String characteristicUUID, { + String characteristicUuid, { String transactionId, }) => _manager.readCharacteristicForService( peripheral, this, - characteristicUUID, + characteristicUuid, transactionId ?? TransactionIdGenerator.getNextId(), ); /// Returns a [Stream] of values emitted by a [Characteristic] identified by - /// [characteristicUUID]. + /// [characteristicUuid]. /// /// Just like [readCharacteristic()] method, values are emitted as /// [CharacteristicWithValue] objects, which are the same as [Characteristic] @@ -79,13 +79,13 @@ class Service extends InternalService { /// [Characteristic] where [Characteristic.isNotifiable] is `true` can be /// monitored. Stream monitorCharacteristic( - String characteristicUUID, { + String characteristicUuid, { String transactionId, }) => _manager.monitorCharacteristicForService( peripheral, this, - characteristicUUID, + characteristicUuid, transactionId ?? TransactionIdGenerator.getNextId(), ); diff --git a/lib/src/_managers_for_classes.dart b/lib/src/_managers_for_classes.dart index 2459814d..69a92814 100644 --- a/lib/src/_managers_for_classes.dart +++ b/lib/src/_managers_for_classes.dart @@ -48,22 +48,22 @@ abstract class ManagerForPeripheral { Future readCharacteristicForDevice( Peripheral peripheral, String serviceUuid, - String characteristicUUID, + String characteristicUuid, String transactionId, ); Future writeCharacteristicForDevice( Peripheral peripheral, - String serviceUUID, - String characteristicUUID, - Uint8List bytes, + String serviceUuid, + String characteristicUuid, + Uint8List value, bool withResponse, String transactionId); Stream monitorCharacteristicForDevice( Peripheral peripheral, - String serviceUUID, - String characteristicUUID, + String serviceUuid, + String characteristicUuid, String transactionId, ); @@ -86,7 +86,7 @@ abstract class ManagerForPeripheral { String serviceUuid, String characteristicUuid, String descriptorUuid, - Uint8List bytes, + Uint8List value, String transactionId, ); } @@ -97,15 +97,15 @@ abstract class ManagerForService { Future readCharacteristicForService( Peripheral peripheral, InternalService service, - String characteristicUUID, + String characteristicUuid, String transactionId, ); Future writeCharacteristicForService( Peripheral peripheral, InternalService service, - String characteristicUUID, - Uint8List bytes, + String characteristicUuid, + Uint8List value, bool withResponse, String transactionId, ); @@ -113,7 +113,7 @@ abstract class ManagerForService { Stream monitorCharacteristicForService( Peripheral peripheral, InternalService service, - String characteristicUUID, + String characteristicUuid, String transactionId, ); @@ -133,7 +133,7 @@ abstract class ManagerForService { Service service, String characteristicUuid, String descriptorUuid, - Uint8List bytes, + Uint8List value, String transactionId, ); } @@ -148,7 +148,7 @@ abstract class ManagerForCharacteristic { Future writeCharacteristicForIdentifier( Peripheral peripheral, InternalCharacteristic characteristic, - Uint8List bytes, + Uint8List value, bool withResponse, String transactionId, ); @@ -185,7 +185,7 @@ abstract class ManagerForDescriptor { Future writeDescriptorForIdentifier( Descriptor descriptor, - Uint8List bytes, + Uint8List value, String transactionId, ); } diff --git a/lib/src/bridge/characteristics_mixin.dart b/lib/src/bridge/characteristics_mixin.dart index 535ef0da..1f1bfcd5 100644 --- a/lib/src/bridge/characteristics_mixin.dart +++ b/lib/src/bridge/characteristics_mixin.dart @@ -28,7 +28,7 @@ mixin CharacteristicsMixin on FlutterBLE { Future readCharacteristicForDevice( Peripheral peripheral, String serviceUuid, - String characteristicUUID, + String characteristicUuid, String transactionId, ) => _methodChannel @@ -37,7 +37,7 @@ mixin CharacteristicsMixin on FlutterBLE { { ArgumentName.deviceIdentifier: peripheral.identifier, ArgumentName.serviceUuid: serviceUuid, - ArgumentName.characteristicUuid: characteristicUUID, + ArgumentName.characteristicUuid: characteristicUuid, ArgumentName.transactionId: transactionId }, ) @@ -52,7 +52,7 @@ mixin CharacteristicsMixin on FlutterBLE { Future readCharacteristicForService( Peripheral peripheral, int serviceIdentifier, - String characteristicUUID, + String characteristicUuid, String transactionId, ) => _methodChannel @@ -60,7 +60,7 @@ mixin CharacteristicsMixin on FlutterBLE { MethodName.readCharacteristicForService, { ArgumentName.serviceIdentifier: serviceIdentifier, - ArgumentName.characteristicUuid: characteristicUUID, + ArgumentName.characteristicUuid: characteristicUuid, ArgumentName.transactionId: transactionId }, ) @@ -75,7 +75,7 @@ mixin CharacteristicsMixin on FlutterBLE { Future writeCharacteristicForIdentifier( Peripheral peripheral, int characteristicIdentifier, - Uint8List bytes, + Uint8List value, bool withResponse, String transactionId, ) => @@ -83,7 +83,7 @@ mixin CharacteristicsMixin on FlutterBLE { MethodName.writeCharacteristicForIdentifier, { ArgumentName.characteristicIdentifier: characteristicIdentifier, - ArgumentName.value: bytes, + ArgumentName.value: value, ArgumentName.withResponse: withResponse, ArgumentName.transactionId: transactionId, }, @@ -92,9 +92,9 @@ mixin CharacteristicsMixin on FlutterBLE { Future writeCharacteristicForDevice( Peripheral peripheral, - String serviceUUID, - String characteristicUUID, - Uint8List bytes, + String serviceUuid, + String characteristicUuid, + Uint8List value, bool withResponse, String transactionId) => _methodChannel @@ -102,9 +102,9 @@ mixin CharacteristicsMixin on FlutterBLE { MethodName.writeCharacteristicForDevice, { ArgumentName.deviceIdentifier: peripheral.identifier, - ArgumentName.serviceUuid: serviceUUID, - ArgumentName.characteristicUuid: characteristicUUID, - ArgumentName.value: bytes, + ArgumentName.serviceUuid: serviceUuid, + ArgumentName.characteristicUuid: characteristicUuid, + ArgumentName.value: value, ArgumentName.withResponse: withResponse, ArgumentName.transactionId: transactionId, }, @@ -119,8 +119,8 @@ mixin CharacteristicsMixin on FlutterBLE { Future writeCharacteristicForService( Peripheral peripheral, int serviceIdentifier, - String characteristicUUID, - Uint8List bytes, + String characteristicUuid, + Uint8List value, bool withResponse, String transactionId, ) => @@ -129,8 +129,8 @@ mixin CharacteristicsMixin on FlutterBLE { MethodName.writeCharacteristicForService, { ArgumentName.serviceIdentifier: serviceIdentifier, - ArgumentName.characteristicUuid: characteristicUUID, - ArgumentName.value: bytes, + ArgumentName.characteristicUuid: characteristicUuid, + ArgumentName.value: value, ArgumentName.withResponse: withResponse, ArgumentName.transactionId: transactionId, }, @@ -172,7 +172,7 @@ mixin CharacteristicsMixin on FlutterBLE { Stream monitorCharacteristicForDevice( Peripheral peripheral, String serviceUuid, - String characteristicUUID, + String characteristicUuid, String transactionId, ) { void Function() startMonitoring = () => _methodChannel.invokeMethod( @@ -180,14 +180,14 @@ mixin CharacteristicsMixin on FlutterBLE { { ArgumentName.deviceIdentifier: peripheral.identifier, ArgumentName.serviceUuid: serviceUuid, - ArgumentName.characteristicUuid: characteristicUUID, + ArgumentName.characteristicUuid: characteristicUuid, ArgumentName.transactionId: transactionId, }, ); bool Function(CharacteristicWithValueAndTransactionId) characteristicsFilter = (characteristic) => - equalsIgnoreAsciiCase(characteristicUUID, characteristic.uuid) && + equalsIgnoreAsciiCase(characteristicUuid, characteristic.uuid) && equalsIgnoreAsciiCase(serviceUuid, characteristic.service.uuid) && equalsIgnoreAsciiCase( transactionId ?? "", characteristic.transactionId ?? ""); @@ -203,21 +203,21 @@ mixin CharacteristicsMixin on FlutterBLE { Stream monitorCharacteristicForService( Peripheral peripheral, int serviceIdentifier, - String characteristicUUID, + String characteristicUuid, String transactionId, ) { void Function() startMonitoring = () => _methodChannel.invokeMethod( MethodName.monitorCharacteristicForService, { ArgumentName.serviceIdentifier: serviceIdentifier, - ArgumentName.characteristicUuid: characteristicUUID, + ArgumentName.characteristicUuid: characteristicUuid, ArgumentName.transactionId: transactionId, }, ); bool Function(CharacteristicWithValueAndTransactionId) characteristicFilter = (characteristic) => - equalsIgnoreAsciiCase(characteristicUUID, characteristic.uuid) && + equalsIgnoreAsciiCase(characteristicUuid, characteristic.uuid) && serviceIdentifier == characteristic.service._id && equalsIgnoreAsciiCase( transactionId ?? "", characteristic.transactionId ?? ""); diff --git a/lib/src/bridge/devices_mixin.dart b/lib/src/bridge/devices_mixin.dart index ca2895e8..872e145e 100644 --- a/lib/src/bridge/devices_mixin.dart +++ b/lib/src/bridge/devices_mixin.dart @@ -12,10 +12,10 @@ mixin DevicesMixin on FlutterBLE { }); } - Future> connectedDevices(List serviceUUIDs) async { + Future> connectedDevices(List serviceUuids) async { return _methodChannel .invokeMethod(MethodName.connectedDevices, { - ArgumentName.uuids: serviceUUIDs, + ArgumentName.uuids: serviceUuids, }).then((peripheralsJson) { print("connected devices json: $peripheralsJson"); return _parsePeripheralsJson(peripheralsJson); diff --git a/lib/src/internal_ble_manager.dart b/lib/src/internal_ble_manager.dart index 42814da6..5234e89c 100644 --- a/lib/src/internal_ble_manager.dart +++ b/lib/src/internal_ble_manager.dart @@ -184,8 +184,8 @@ class InternalBleManager } @override - Future> connectedPeripherals(List serviceUUIDs) { - return _bleLib.connectedDevices(serviceUUIDs ?? []); + Future> connectedPeripherals(List serviceUuids) { + return _bleLib.connectedDevices(serviceUuids ?? []); } @override @@ -201,12 +201,12 @@ class InternalBleManager Future readCharacteristicForDevice( Peripheral peripheral, String serviceUuid, - String characteristicUUID, + String characteristicUuid, String transactionId) => _bleLib.readCharacteristicForDevice( peripheral, serviceUuid, - characteristicUUID, + characteristicUuid, transactionId, ); @@ -214,12 +214,12 @@ class InternalBleManager Future readCharacteristicForService( Peripheral peripheral, InternalService service, - String characteristicUUID, + String characteristicUuid, String transactionId) => _bleLib.readCharacteristicForService( peripheral, service._id, - characteristicUUID, + characteristicUuid, transactionId, ); @@ -227,13 +227,13 @@ class InternalBleManager Future writeCharacteristicForIdentifier( Peripheral peripheral, InternalCharacteristic characteristic, - Uint8List bytes, + Uint8List value, bool withResponse, String transactionId) => _bleLib.writeCharacteristicForIdentifier( peripheral, characteristic._id, - bytes, + value, withResponse, transactionId, ); @@ -241,16 +241,16 @@ class InternalBleManager @override Future writeCharacteristicForDevice( Peripheral peripheral, - String serviceUUID, - String characteristicUUID, - Uint8List bytes, + String serviceUuid, + String characteristicUuid, + Uint8List value, bool withResponse, String transactionId) => _bleLib.writeCharacteristicForDevice( peripheral, - serviceUUID, - characteristicUUID, - bytes, + serviceUuid, + characteristicUuid, + value, withResponse, transactionId, ); @@ -259,15 +259,15 @@ class InternalBleManager Future writeCharacteristicForService( Peripheral peripheral, InternalService service, - String characteristicUUID, - Uint8List bytes, + String characteristicUuid, + Uint8List value, bool withResponse, String transactionId) => _bleLib.writeCharacteristicForService( peripheral, service._id, - characteristicUUID, - bytes, + characteristicUuid, + value, withResponse, transactionId, ); @@ -275,14 +275,14 @@ class InternalBleManager @override Stream monitorCharacteristicForDevice( Peripheral peripheral, - String serviceUUID, - String characteristicUUID, + String serviceUuid, + String characteristicUuid, String transactionId, ) => _bleLib.monitorCharacteristicForDevice( peripheral, - serviceUUID, - characteristicUUID, + serviceUuid, + characteristicUuid, transactionId, ); @@ -290,13 +290,13 @@ class InternalBleManager Stream monitorCharacteristicForService( Peripheral peripheral, InternalService service, - String characteristicUUID, + String characteristicUuid, String transactionId, ) => _bleLib.monitorCharacteristicForService( peripheral, service._id, - characteristicUUID, + characteristicUuid, transactionId, ); From 7fefee1074bfa555d52367e0b08b6e30eb6658b9 Mon Sep 17 00:00:00 2001 From: Bartosz Wilk Date: Wed, 4 Mar 2020 14:34:27 +0100 Subject: [PATCH 02/12] Add unit tests for Service (#439) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * unit test for BleManager * [descriptor] override equals & hashcode functions * [tests] add: mock classes for managers * [service] add: service tests * [tests] create characteristics and descriptors using separate generators * [service][tests] cover generating transactionId when it's not specified * [service][tests] clear mocks interactions after each test * [service][tests] add missing test for getting all descriptors for specified characteristic Co-authored-by: Paweł Byszewski <4048063+pawelByszewski@users.noreply.github.com> --- lib/descriptor.dart | 15 + test/ble_manager_test.dart | 16 + test/mock/manager_mock.dart | 8 + test/service_test.dart | 298 +++++++++++++++++++ test/test_util/characteristic_generator.dart | 28 ++ test/test_util/descriptor_generator.dart | 23 ++ 6 files changed, 388 insertions(+) create mode 100644 test/ble_manager_test.dart create mode 100644 test/mock/manager_mock.dart create mode 100644 test/service_test.dart create mode 100644 test/test_util/characteristic_generator.dart create mode 100644 test/test_util/descriptor_generator.dart diff --git a/lib/descriptor.dart b/lib/descriptor.dart index 9b938721..5e2e265a 100644 --- a/lib/descriptor.dart +++ b/lib/descriptor.dart @@ -33,6 +33,21 @@ class Descriptor extends InternalDescriptor { value, transactionId ?? TransactionIdGenerator.getNextId(), ); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is Descriptor && + runtimeType == other.runtimeType && + _manager == other._manager && + characteristic == other.characteristic && + uuid == other.uuid; + + @override + int get hashCode => + _manager.hashCode ^ + characteristic.hashCode ^ + uuid.hashCode; } class DescriptorWithValue extends Descriptor with WithValue { diff --git a/test/ble_manager_test.dart b/test/ble_manager_test.dart new file mode 100644 index 00000000..267d54a6 --- /dev/null +++ b/test/ble_manager_test.dart @@ -0,0 +1,16 @@ + +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; +import 'package:test/test.dart'; + +void main() { + test('Ble manager must be singleton', () { + //given + final firstInstanceOfBlemanager = BleManager(); + + //when + final secondInstanceOfBleManager = BleManager(); + + //then + expect(secondInstanceOfBleManager, firstInstanceOfBlemanager); + }); +} \ No newline at end of file diff --git a/test/mock/manager_mock.dart b/test/mock/manager_mock.dart new file mode 100644 index 00000000..3c9721b5 --- /dev/null +++ b/test/mock/manager_mock.dart @@ -0,0 +1,8 @@ +import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; +import 'package:mockito/mockito.dart'; + +class ManagerForServiceMock extends Mock implements ManagerForService {} + +class ManagerForCharacteristicMock extends Mock implements ManagerForCharacteristic {} + +class ManagerForDescriptorMock extends Mock implements ManagerForDescriptor {} diff --git a/test/service_test.dart b/test/service_test.dart new file mode 100644 index 00000000..01b9319a --- /dev/null +++ b/test/service_test.dart @@ -0,0 +1,298 @@ +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; +import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; +import 'package:mockito/mockito.dart'; +import 'package:test/test.dart'; + +import 'mock/manager_mock.dart'; +import 'mock/mock_peripheral.dart'; +import 'test_util/characteristic_generator.dart'; +import 'test_util/descriptor_generator.dart'; + +void main() { + Peripheral peripheral = PeripheralMock(); + ManagerForService managerForService = ManagerForServiceMock(); + ManagerForCharacteristic managerForCharacteristic = + ManagerForCharacteristicMock(); + ManagerForDescriptor managerForDescriptor = ManagerForDescriptorMock(); + CharacteristicGenerator characteristicGenerator = + CharacteristicGenerator(managerForCharacteristic); + DescriptorGenerator descriptorGenerator = + DescriptorGenerator(managerForDescriptor); + + Service service = Service.fromJson({ + "serviceId": 1, + "serviceUuid": "testUuid", + }, peripheral, managerForService); + + CharacteristicWithValue createCharacteristic(int seed) => + characteristicGenerator.create(seed, service); + + DescriptorWithValue createDescriptor(int seed) => + descriptorGenerator.create(seed, createCharacteristic(seed)); + + tearDown(() { + [ + peripheral, + managerForService, + managerForCharacteristic, + managerForDescriptor, + ].forEach(clearInteractions); + }); + + test("characteristics returns characteristics provided by manager", () async { + //given + when(managerForService.characteristicsForService(service)) + .thenAnswer((_) => Future.value([ + createCharacteristic(0), + createCharacteristic(1), + createCharacteristic(2), + ])); + + //when + var characteristics = await service.characteristics(); + + //then + expect( + characteristics, + equals([ + createCharacteristic(0), + createCharacteristic(1), + createCharacteristic(2), + ])); + }); + + test("descriptorsForCharacteristic returns characteristics provided by manager", () async { + //given + when(managerForService.descriptorsForService(service, "123")) + .thenAnswer((_) => Future.value([ + createDescriptor(0), + createDescriptor(1), + createDescriptor(2), + ])); + + //when + var descriptors = await service.descriptorsForCharacteristic("123"); + + //then + expect( + descriptors, + equals([ + createDescriptor(0), + createDescriptor(1), + createDescriptor(2), + ])); + }); + + test("readCharacteristic returns expected characteristic", () async { + //given + when(managerForService.readCharacteristicForService( + peripheral, service, "123", "456")) + .thenAnswer((_) => Future.value(createCharacteristic(0))); + + //when + var characteristic = + await service.readCharacteristic("123", transactionId: "456"); + + //then + expect(characteristic, equals(createCharacteristic(0))); + }); + + test("readCharacteristic reads characteristic using manager", () { + //when + service.readCharacteristic("123", transactionId: "456"); + + //then + verify( + managerForService.readCharacteristicForService( + peripheral, service, "123", "456"), + ); + }); + + test( + "readCharacteristic reads characteristic with generated transactionId using manager", + () { + //when + service.readCharacteristic("123", transactionId: null); + + //then + verify( + managerForService.readCharacteristicForService( + peripheral, service, "123", argThat(isNotNull)), + ); + }); + + test("readDescriptor returns expected descriptor", () async { + //given + when(managerForService.readDescriptorForService( + service, "123", "456", "789")) + .thenAnswer((_) => Future.value(createDescriptor(0))); + + //when + var characteristic = + await service.readDescriptor("123", "456", transactionId: "789"); + + //then + expect(characteristic, equals(createDescriptor(0))); + }); + + test("readDescriptor reads characteristic using manager", () { + //when + service.readDescriptor("123", "456", transactionId: "789"); + + //then + verify( + managerForService.readDescriptorForService(service, "123", "456", "789"), + ); + }); + + test( + "readDescriptor reads descriptor with generated transactionId using manager", + () { + //when + service.readDescriptor("123", "456", transactionId: null); + + //then + verify( + managerForService.readDescriptorForService( + service, "123", "456", argThat(isNotNull)), + ); + }); + + test("writeCharacteristic returns expected characteristic", () async { + //given + when(managerForService.writeCharacteristicForService(peripheral, service, + "123", Uint8List.fromList([1, 2, 3, 4]), false, "456")) + .thenAnswer((_) => Future.value(createCharacteristic(0))); + + //when + var characteristic = await service.writeCharacteristic( + "123", + Uint8List.fromList([1, 2, 3, 4]), + false, + transactionId: "456", + ); + + //then + expect(characteristic, equals(createCharacteristic(0))); + }); + + test("writeCharacteristic writes characteristic using manager", () { + //when + service.writeCharacteristic("123", Uint8List.fromList([1, 2, 3, 4]), false, + transactionId: "456"); + + //then + verify( + managerForService.writeCharacteristicForService(peripheral, service, + "123", Uint8List.fromList([1, 2, 3, 4]), false, "456"), + ); + }); + + test( + "writeCharacteristic writes characteristic with generated transactionId using manager", + () { + //when + service.writeCharacteristic("123", Uint8List.fromList([1, 2, 3, 4]), false, + transactionId: null); + + //then + verify( + managerForService.writeCharacteristicForService(peripheral, service, + "123", Uint8List.fromList([1, 2, 3, 4]), false, argThat(isNotNull)), + ); + }); + + test("writeDescriptor returns expected descriptor", () async { + //given + when(managerForService.writeDescriptorForService( + service, "123", "456", Uint8List.fromList([1, 2, 3, 4]), "789")) + .thenAnswer((_) => Future.value(createDescriptor(0))); + + //when + var characteristic = await service.writeDescriptor( + "123", "456", Uint8List.fromList([1, 2, 3, 4]), + transactionId: "789"); + + //then + expect(characteristic, equals(createDescriptor(0))); + }); + + test("writeDescriptor writes descriptor using manager", () { + //when + service.writeDescriptor("123", "456", Uint8List.fromList([1, 2, 3, 4]), + transactionId: "789"); + + //then + verify( + managerForService.writeDescriptorForService( + service, "123", "456", Uint8List.fromList([1, 2, 3, 4]), "789"), + ); + }); + + test( + "writeDescriptor writes descriptor with generated transactionId using manager", + () { + //when + service.writeDescriptor("123", "456", Uint8List.fromList([1, 2, 3, 4]), + transactionId: null); + + //then + verify( + managerForService.writeDescriptorForService(service, "123", "456", + Uint8List.fromList([1, 2, 3, 4]), argThat(isNotNull)), + ); + }); + + test("monitorCharacteristic streams expected characteristic", () { + //given + var streamController = StreamController(); + when(managerForService.monitorCharacteristicForService( + peripheral, service, "123", "456")) + .thenAnswer((_) => streamController.stream); + + //when + var characteristicNotifications = + service.monitorCharacteristic("123", transactionId: "456"); + streamController.sink.add(createCharacteristic(0)); + streamController.sink.add(createCharacteristic(1)); + streamController.sink.add(createCharacteristic(2)); + streamController.close(); + + //then + expect( + characteristicNotifications, + emitsInOrder([ + emits(equals(createCharacteristic(0))), + emits(equals(createCharacteristic(1))), + emits(equals(createCharacteristic(2))), + emitsDone + ])); + }); + + test("monitorCharacteristic monitors characteristic using manager", () { + //when + service.monitorCharacteristic("123", transactionId: "456"); + + //then + verify( + managerForService.monitorCharacteristicForService( + peripheral, service, "123", "456"), + ); + }); + + test( + "monitorCharacteristic monitors characteristic with generated transactionId using manager", + () { + //when + service.monitorCharacteristic("123", transactionId: null); + + //then + verify( + managerForService.monitorCharacteristicForService( + peripheral, service, "123", argThat(isNotNull)), + ); + }); +} diff --git a/test/test_util/characteristic_generator.dart b/test/test_util/characteristic_generator.dart new file mode 100644 index 00000000..6b979c6b --- /dev/null +++ b/test/test_util/characteristic_generator.dart @@ -0,0 +1,28 @@ +import 'dart:convert'; + +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; +import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; + +class CharacteristicGenerator { + ManagerForCharacteristic managerForCharacteristic; + + CharacteristicGenerator(this.managerForCharacteristic); + + Map _createRawCharacteristic(int seed) => { + "characteristicUuid": seed.toString(), + "id": seed, + "isReadable": seed % 2 == 0, + "isWritableWithResponse": seed % 2 == 0, + "isWritableWithoutResponse": seed % 2 == 0, + "isNotifiable": seed % 2 == 0, + "isIndicatable": seed % 2 == 0, + "value": base64Encode([seed]) + }; + + CharacteristicWithValue create(int seed, Service service) => + CharacteristicWithValue.fromJson( + _createRawCharacteristic(seed), + service, + managerForCharacteristic, + ); +} diff --git a/test/test_util/descriptor_generator.dart b/test/test_util/descriptor_generator.dart new file mode 100644 index 00000000..f836854c --- /dev/null +++ b/test/test_util/descriptor_generator.dart @@ -0,0 +1,23 @@ +import 'dart:convert'; + +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; +import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; + +class DescriptorGenerator { + ManagerForDescriptor managerForDescriptor; + + DescriptorGenerator(this.managerForDescriptor); + + Map _createRawDescriptor(int seed) => { + "descriptorId": seed, + "descriptorUuid": seed.toString(), + "value": base64Encode([seed]) + }; + + DescriptorWithValue create(int seed, Characteristic characteristic) => + DescriptorWithValue.fromJson( + _createRawDescriptor(seed), + characteristic, + managerForDescriptor, + ); +} From 9f69699a10bb3f2cd1adfbd0d91e4899ef02067b Mon Sep 17 00:00:00 2001 From: Bartosz Wilk Date: Tue, 10 Mar 2020 16:01:25 +0100 Subject: [PATCH 03/12] Descriptor unit tests (#441) * unit test for BleManager * [descriptor] override equals & hashcode functions * [tests] add: mock classes for managers * [tests] create characteristics and descriptors using separate generators * [descriptor] add: tests * [descriptor] fix: test names * [tests] move all mocks declarations to one aggregate file * [ble-manager][test] add matcher to always check objects' references * [descriptor][test] add tests that check uniquity of transactionId Co-authored-by: pawelByszewski --- test/ble_manager_test.dart | 2 +- test/descriptor_test.dart | 118 ++++++++++++++++++++ test/mock/mock_peripheral.dart | 4 - test/mock/{manager_mock.dart => mocks.dart} | 7 ++ test/service_test.dart | 3 +- test/src/bridge/lib_core_test.dart | 2 +- 6 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 test/descriptor_test.dart delete mode 100644 test/mock/mock_peripheral.dart rename test/mock/{manager_mock.dart => mocks.dart} (59%) diff --git a/test/ble_manager_test.dart b/test/ble_manager_test.dart index 267d54a6..e4470b31 100644 --- a/test/ble_manager_test.dart +++ b/test/ble_manager_test.dart @@ -11,6 +11,6 @@ void main() { final secondInstanceOfBleManager = BleManager(); //then - expect(secondInstanceOfBleManager, firstInstanceOfBlemanager); + expect(secondInstanceOfBleManager, same(firstInstanceOfBlemanager)); }); } \ No newline at end of file diff --git a/test/descriptor_test.dart b/test/descriptor_test.dart new file mode 100644 index 00000000..a38de706 --- /dev/null +++ b/test/descriptor_test.dart @@ -0,0 +1,118 @@ +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; +import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; +import 'package:mockito/mockito.dart'; +import 'package:test/test.dart'; + +import 'mock/mocks.dart'; +import 'test_util/descriptor_generator.dart'; + +void main() { + ManagerForDescriptor managerForDescriptor = ManagerForDescriptorMock(); + DescriptorGenerator descriptorGenerator = + DescriptorGenerator(managerForDescriptor); + + DescriptorWithValue createDescriptor(int seed) => + descriptorGenerator.create(seed, CharacteristicMock()); + + Descriptor descriptor = createDescriptor(123); + + tearDown(() { + clearInteractions(managerForDescriptor); + }); + + test("read returns expected value", () async { + //given + when(managerForDescriptor.readDescriptorForIdentifier(descriptor, "456")) + .thenAnswer((_) => Future.value(Uint8List.fromList([1, 2, 3, 4]))); + + //when + var value = await descriptor.read(transactionId: "456"); + + //then + expect(value, equals(Uint8List.fromList([1, 2, 3, 4]))); + }); + + test( + "read invokes manager with expected params when transactionId is specified", + () { + //when + descriptor.read(transactionId: "456"); + + //then + verify( + managerForDescriptor.readDescriptorForIdentifier(descriptor, "456"), + ); + }); + + test( + "read invokes manager with expected params when transactionId is not specified", + () { + //when + descriptor.read(); + + //then + verify( + managerForDescriptor.readDescriptorForIdentifier( + descriptor, argThat(isNotNull)), + ); + }); + + test( + "read invokes manager with unique transactionId when transactionId is not specified", + () { + //when + descriptor.read(); + descriptor.read(); + + //then + var transactionIds = verify( + managerForDescriptor.readDescriptorForIdentifier( + descriptor, captureThat(isNotNull)), + ).captured; + expect(transactionIds[0], isNot(equals(transactionIds[1]))); + }); + + test( + "write invokes manager with expected params when transactionId is specified", + () { + //when + descriptor.write(Uint8List.fromList([1, 2, 3, 4]), transactionId: "456"); + + //then + verify( + managerForDescriptor.writeDescriptorForIdentifier( + descriptor, Uint8List.fromList([1, 2, 3, 4]), "456"), + ); + }); + + test( + "write invokes manager with expected params when transactionId is not specified", + () { + //when + descriptor.write(Uint8List.fromList([1, 2, 3, 4])); + + //then + verify( + managerForDescriptor.writeDescriptorForIdentifier( + descriptor, Uint8List.fromList([1, 2, 3, 4]), argThat(isNotNull)), + ); + }); + + test( + "write invokes manager with unique transactionId when transactionId is not specified", + () { + //when + descriptor.write(Uint8List.fromList([1, 2, 3, 4])); + descriptor.write(Uint8List.fromList([1, 2, 3, 4])); + + //then + var transactionIds = verify( + managerForDescriptor.writeDescriptorForIdentifier(descriptor, + Uint8List.fromList([1, 2, 3, 4]), captureThat(isNotNull))) + .captured; + expect(transactionIds[0], isNot(equals(transactionIds[1]))); + }); +} diff --git a/test/mock/mock_peripheral.dart b/test/mock/mock_peripheral.dart deleted file mode 100644 index 94843f4e..00000000 --- a/test/mock/mock_peripheral.dart +++ /dev/null @@ -1,4 +0,0 @@ -import 'package:flutter_ble_lib/flutter_ble_lib.dart'; -import 'package:mockito/mockito.dart'; - -class PeripheralMock extends Mock implements Peripheral {} \ No newline at end of file diff --git a/test/mock/manager_mock.dart b/test/mock/mocks.dart similarity index 59% rename from test/mock/manager_mock.dart rename to test/mock/mocks.dart index 3c9721b5..9d342fad 100644 --- a/test/mock/manager_mock.dart +++ b/test/mock/mocks.dart @@ -1,3 +1,4 @@ +import 'package:flutter_ble_lib/flutter_ble_lib.dart'; import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; import 'package:mockito/mockito.dart'; @@ -6,3 +7,9 @@ class ManagerForServiceMock extends Mock implements ManagerForService {} class ManagerForCharacteristicMock extends Mock implements ManagerForCharacteristic {} class ManagerForDescriptorMock extends Mock implements ManagerForDescriptor {} + +class ServiceMock extends Mock implements Service {} + +class PeripheralMock extends Mock implements Peripheral {} + +class CharacteristicMock extends Mock implements Characteristic {} diff --git a/test/service_test.dart b/test/service_test.dart index 01b9319a..10ead0e7 100644 --- a/test/service_test.dart +++ b/test/service_test.dart @@ -6,8 +6,7 @@ import 'package:flutter_ble_lib/src/_managers_for_classes.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; -import 'mock/manager_mock.dart'; -import 'mock/mock_peripheral.dart'; +import 'mock/mocks.dart'; import 'test_util/characteristic_generator.dart'; import 'test_util/descriptor_generator.dart'; diff --git a/test/src/bridge/lib_core_test.dart b/test/src/bridge/lib_core_test.dart index e691098a..15e9bd93 100644 --- a/test/src/bridge/lib_core_test.dart +++ b/test/src/bridge/lib_core_test.dart @@ -10,8 +10,8 @@ import 'package:flutter_ble_lib/src/_constants.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; -import '../../mock/mock_peripheral.dart'; import '../../json/ble_error_jsons.dart'; +import '../../mock/mocks.dart'; const flutterBleLibMethodChannelName = 'flutter_ble_lib'; const monitorCharacteristicEventChannelName = From 79ebf4423969dedaca3cca0b9220504d121aab80 Mon Sep 17 00:00:00 2001 From: Tomasz Bogusz Date: Tue, 31 Mar 2020 12:34:30 +0200 Subject: [PATCH 04/12] [iOS] Fixed casting of Bool arguments received from dart (#451) --- CHANGELOG.md | 4 ++++ ios/Classes/FlutterBleLibPlugin.m | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d1ea6d8..10d4fe35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.2.4 + +* Fix issue with characteristic write without response operation on iOS + ## 2.2.3 * Fix issue with duplicated or malformed notification values diff --git a/ios/Classes/FlutterBleLibPlugin.m b/ios/Classes/FlutterBleLibPlugin.m index 818ff193..07e26441 100644 --- a/ios/Classes/FlutterBleLibPlugin.m +++ b/ios/Classes/FlutterBleLibPlugin.m @@ -230,7 +230,7 @@ - (void)isDeviceConnected:(FlutterMethodCall *)call result:(FlutterResult)result } - (void)observeConnectionState:(FlutterMethodCall *)call result:(FlutterResult)result { - BOOL emitCurrentValue = (BOOL)call.arguments[ARGUMENT_KEY_EMIT_CURRENT_VALUE]; + BOOL emitCurrentValue = ((NSNumber *)call.arguments[ARGUMENT_KEY_EMIT_CURRENT_VALUE]).boolValue; if (emitCurrentValue == YES) { Resolve resolve = ^(id isConnected) { if ((BOOL)isConnected == YES) { @@ -343,7 +343,7 @@ - (void)writeCharacteristicForDevice:(FlutterMethodCall *)call result:(FlutterRe serviceUUID:call.arguments[ARGUMENT_KEY_SERVICE_UUID] characteristicUUID:call.arguments[ARGUMENT_KEY_CHARACTERISTIC_UUID] valueBase64:[self base64encodedStringFromBytes:call.arguments[ARGUMENT_KEY_VALUE]] - response:(BOOL)call.arguments[ARGUMENT_KEY_WITH_RESPONSE] + response:((NSNumber *)call.arguments[ARGUMENT_KEY_WITH_RESPONSE]).boolValue transactionId:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_TRANSACTION_ID]] resolve:[self resolveForReadWriteCharacteristic:result transactionId:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_TRANSACTION_ID]]] @@ -354,7 +354,7 @@ - (void)writeCharacteristicForService:(FlutterMethodCall *)call result:(FlutterR [_adapter writeCharacteristicForService:[call.arguments[ARGUMENT_KEY_SERVICE_ID] doubleValue] characteristicUUID:call.arguments[ARGUMENT_KEY_CHARACTERISTIC_UUID] valueBase64:[self base64encodedStringFromBytes:call.arguments[ARGUMENT_KEY_VALUE]] - response:(BOOL)call.arguments[ARGUMENT_KEY_WITH_RESPONSE] + response:((NSNumber *)call.arguments[ARGUMENT_KEY_WITH_RESPONSE]).boolValue transactionId:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_TRANSACTION_ID]] resolve:[self resolveForReadWriteCharacteristic:result transactionId:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_TRANSACTION_ID]]] @@ -364,7 +364,7 @@ - (void)writeCharacteristicForService:(FlutterMethodCall *)call result:(FlutterR - (void)writeCharacteristic:(FlutterMethodCall *)call result:(FlutterResult)result { [_adapter writeCharacteristic:[call.arguments[ARGUMENT_KEY_CHARACTERISTIC_IDENTIFIER] doubleValue] valueBase64:[self base64encodedStringFromBytes:call.arguments[ARGUMENT_KEY_VALUE]] - response:(BOOL)call.arguments[ARGUMENT_KEY_WITH_RESPONSE] + response:((NSNumber *)call.arguments[ARGUMENT_KEY_WITH_RESPONSE]).boolValue transactionId:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_TRANSACTION_ID]] resolve:[self resolveForReadWriteCharacteristic:result transactionId:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_TRANSACTION_ID]]] From 9ca6b54f824110e6cad10356db5fa209bc734fc9 Mon Sep 17 00:00:00 2001 From: Dustin Graham Date: Sat, 4 Apr 2020 05:53:56 -0700 Subject: [PATCH 05/12] Remove root level `Flutter User Facing API.dart` (#455) * rename root level file naming interfered with certain build_runner code generators * remove flutter_user_facing_api.dart --- Flutter User Facing API.dart | 100 ----------------------------------- 1 file changed, 100 deletions(-) delete mode 100644 Flutter User Facing API.dart diff --git a/Flutter User Facing API.dart b/Flutter User Facing API.dart deleted file mode 100644 index 579572ed..00000000 --- a/Flutter User Facing API.dart +++ /dev/null @@ -1,100 +0,0 @@ -import 'dart:typed_data'; - -abstract class BluetoothLowEnergyManager { - Future createClient({String restoreStateIdentifier, Function(List) handleRestoredConnectedPeripherals}); - // Future createCentralManager() //must create separate native client and whole communication stack; TODO down the road - // Future createPeripheralManager() //must create separate native client and whole communication stack; TODO down the road - - Future destroyClient(); - - Future enableRadio(); - Future disableRadio(); - - Future cancelTransaction(String transactionId); - - Future setLogLevel(LogLevel logLevel); - Future logLevel(); - - Future state(); - Stream observeState({bool emitCurrentValue = true}); -} - -// class CentralManager { //TODO down the road -// Future createClient({String restoreStateIdentifier, Function(List) handleRestoredConnectedDevice}) -// Future destroyClient() - -// Future enableRadio() -// Future disableRadio() - -// Future cancelTransaction(String transactionId) - -// Future setLogLevel(LogLevel logLevel) -// Future logLevel() - -// Future state() -// Stream observeState({bool emitCurrentValue = true}) - -// Stream startDeviceScan({int scanMode = 0, int callbackType = 1, List uuids = void})* -// Future stopDeviceScan() - -// Future> devices(String[] ids) -// Future> connectedDevicesWithServices(String[] serviceUUIDs) -// } - -// class PeripheralManager {} //TODO down the road - -abstract class Peripheral { //was Device - Future connect({ bool isAutoConnect, int requestMtu, bool refreshGatt, Duration timeout }); - Future isConnected(); - Stream onConnectionStateChanged(); //TODO: create connectionState enum - Future disconnectOrCancelConnection(); - - Future requestMTU(int mtu, { String transactionId }); - Future readRSSI({ String transactionId }); - - Future discoverAllServicesAndCharacteristics({ String transactionId }); - Future> services(); - Future> characteristics(String serviceUUID); - Future> descriptors(String serviceUUID, String characteristicUUID); - - Future writeCharacteristic(String serviceUUID, String characteristicUUID, Uint8List bytes, bool withResponse, { String transactionId }); - Future readCharacteristic(String serviceUUID, String characteristicUUID, { String transactionId }); - Stream monitorCharacteristic(String serviceUUID, String characteristicUUID, { String transactionId }); - - Future writeDescriptor(String serviceUUID, String characteristicUUID, String descriptorUUID, Uint8List bytes, { String transactionId }); - Future readDescriptor(String serviceUUID, String characteristicUUID, String descriptorUUID, { String transactionId }); -} - -abstract class Service { - Future> characteristics(); - Future> descriptors(String characteristicUUID); - - Future writeCharacteristic(String characteristicUUID, Uint8List bytes, bool withResponse, { String transactionId }); - Future readCharacteristic(String characteristicUUID, { String transactionId }); - Stream monitorCharacteristic(String characteristicUUID, { String transactionId }); - - Future writeDescriptor(String characteristicUUID, String descriptorUUID, Uint8List bytes, { String transactionId }); - Future readDescriptor(String characteristicUUID, String descriptorUUID, { String transactionId }); -} - -abstract class Characteristic { - Future> descriptors(String characteristicUUID); - - Future write(Uint8List bytes, bool withResponse, { String transactionId }); - Future read({ String transactionId }); - Stream monitor({ String transactionId }); - - Future writeDescriptor(String descriptorUUID, Uint8List bytes, { String transactionId }); - Future readDescriptor(String descriptorUUID, { String transactionId }); -} - -abstract class CharacteristicWithValue extends Characteristic {} - -abstract class Descriptor { - Future writeDescriptor(Uint8List bytes, { String transactionId }); - Future readDescriptor({ String transactionId }); -} - -abstract class BluetoothState {} -abstract class LogLevel {} -abstract class ConnectionState {} \ No newline at end of file From 1ffe735a00ed5cc1423edc88785fd746fe0bfef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kojdecki?= Date: Mon, 11 May 2020 09:20:27 +0200 Subject: [PATCH 06/12] Handle destroyClient call on iOS (#461) * Handle destroyClient call on iOS * Add missing semicolon --- ios/Classes/FlutterBleLibPlugin.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ios/Classes/FlutterBleLibPlugin.m b/ios/Classes/FlutterBleLibPlugin.m index 07e26441..de3e5f04 100644 --- a/ios/Classes/FlutterBleLibPlugin.m +++ b/ios/Classes/FlutterBleLibPlugin.m @@ -69,6 +69,8 @@ + (void)registerWithRegistrar:(NSObject *)registrar { - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { if ([METHOD_NAME_CREATE_CLIENT isEqualToString:call.method]) { [self createClient:call result:result]; + } else if ([METHOD_NAME_DESTROY_CLIENT isEqualToString:call.method]) { + [self destroyClient]; } else if ([METHOD_NAME_ENABLE_RADIO isEqualToString:call.method]) { [self enable:call result:result]; } else if ([METHOD_NAME_DISABLE_RADIO isEqualToString:call.method]) { From 8a7605dc3375ca5b8b7a2457aecc3a85d706fd58 Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Tue, 2 Jun 2020 00:23:36 +1000 Subject: [PATCH 07/12] [Android] Fix race condition in ConnectionStateStreamHanderl (#468) (#469) * [Android] Fix race condition in ConnectionStateStreamHanderl (#468) Add extra null check and synchronized block in async callback * [Android] Move json generation back into try catch block (#468) --- .../event/ConnectionStateStreamHandler.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/polidea/flutter_ble_lib/event/ConnectionStateStreamHandler.java b/android/src/main/java/com/polidea/flutter_ble_lib/event/ConnectionStateStreamHandler.java index a506753f..4e183991 100644 --- a/android/src/main/java/com/polidea/flutter_ble_lib/event/ConnectionStateStreamHandler.java +++ b/android/src/main/java/com/polidea/flutter_ble_lib/event/ConnectionStateStreamHandler.java @@ -27,13 +27,20 @@ synchronized public void onCancel(Object o) { synchronized public void onNewConnectionState(final ConnectionStateChange connectionState) { if (eventSink != null) { + final ConnectionStateStreamHandler that = this; new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { - try { - eventSink.success(connectionStateChangeJsonConverter.toJson(connectionState)); - } catch (JSONException e) { - eventSink.error("-1", e.getMessage(), e.getStackTrace()); + synchronized (that) { + // Check again for null - by the time we get into this async runnable our eventSink + // may have been canceled + if (eventSink != null) { + try { + eventSink.success(connectionStateChangeJsonConverter.toJson(connectionState)); + } catch (JSONException e) { + eventSink.error("-1", e.getMessage(), e.getStackTrace()); + } + } } } }); From 7cdbabe90bf1017cb369393815b91702327e0963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kojdecki?= Date: Wed, 10 Jun 2020 17:35:57 +0200 Subject: [PATCH 08/12] Update pubspec format (#418) * Remove author field * Add supported platforms * Enforce Flutter version --- pubspec.yaml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index ec93ec18..8907c5d0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,12 @@ name: flutter_ble_lib description: FlutterBle Library is a flutter library that supports BLE operations. It uses MultiPlatformBleAdapter as a native backend.. version: 2.2.4 -author: "Polidea " homepage: https://github.com/Polidea/FlutterBleLib environment: sdk: ">=2.1.0 <3.0.0" + # Flutter versions prior to 1.10 did not support the flutter.plugin.platforms map. + flutter: ">=1.10.0 <2.0.0" dependencies: collection: ^1.14.11 @@ -30,8 +31,12 @@ flutter: # be modified. They are used by the tooling to maintain consistency when # adding or updating assets for this project. plugin: - androidPackage: com.polidea.flutter_ble_lib - pluginClass: FlutterBleLibPlugin + platforms: + android: + package: com.polidea.flutter_ble_lib + pluginClass: FlutterBleLibPlugin + ios: + pluginClass: FlutterBleLibPlugin # To add assets to your plugin package, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg From 938ddb9131cbadb312e626ed1a08cc3b277ca100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kojdecki?= Date: Tue, 23 Jun 2020 15:44:59 +0200 Subject: [PATCH 09/12] Fix example (#484) * Fix example * Fix CI --- .gitignore | 3 +- .travis.yml | 2 +- example/ios/Podfile.lock | 35 ------------------- .../lib/devices_list/devices_list_view.dart | 7 ++-- 4 files changed, 7 insertions(+), 40 deletions(-) delete mode 100644 example/ios/Podfile.lock diff --git a/.gitignore b/.gitignore index 96fd13e0..a8aec041 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ res/values/strings_en.arb lib/generated/ example/lib/generated/ example/.flutter-plugins-dependencies -.dart_tool/ \ No newline at end of file +.dart_tool/ +example/ios/Podfile.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 3ad2bb2b..25478c46 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,7 +47,7 @@ _android_job_template: &android_job_template _ios_job_template: &ios_job_template language: objective-c os: osx - osx_image: xcode11 + osx_image: xcode11.6 xcode_workspave: example/ios/Runner.xcworkspace xcode_scheme: Runner before_script: diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock deleted file mode 100644 index a6a5adb4..00000000 --- a/example/ios/Podfile.lock +++ /dev/null @@ -1,35 +0,0 @@ -PODS: - - Flutter (1.0.0) - - flutter_ble_lib (2.2.3): - - Flutter - - MultiplatformBleAdapter (= 0.1.5) - - MultiplatformBleAdapter (0.1.5) - - "permission_handler (4.2.0+hotfix.3)": - - Flutter - -DEPENDENCIES: - - Flutter (from `Flutter`) - - flutter_ble_lib (from `.symlinks/plugins/flutter_ble_lib/ios`) - - permission_handler (from `.symlinks/plugins/permission_handler/ios`) - -SPEC REPOS: - trunk: - - MultiplatformBleAdapter - -EXTERNAL SOURCES: - Flutter: - :path: Flutter - flutter_ble_lib: - :path: ".symlinks/plugins/flutter_ble_lib/ios" - permission_handler: - :path: ".symlinks/plugins/permission_handler/ios" - -SPEC CHECKSUMS: - Flutter: 0e3d915762c693b495b44d77113d4970485de6ec - flutter_ble_lib: 20e79f0b1d78d921d9ed68ab4451e190029bc3d9 - MultiplatformBleAdapter: 3c4391d428382738a47662ae1f665a29ce78ff39 - permission_handler: 40520ab8ad1bb78a282b832464e995ec87f77ec6 - -PODFILE CHECKSUM: 1b66dae606f75376c5f2135a8290850eeb09ae83 - -COCOAPODS: 1.8.4 diff --git a/example/lib/devices_list/devices_list_view.dart b/example/lib/devices_list/devices_list_view.dart index ff4177a0..e5ebadc1 100644 --- a/example/lib/devices_list/devices_list_view.dart +++ b/example/lib/devices_list/devices_list_view.dart @@ -19,6 +19,7 @@ class DevicesListScreen extends StatefulWidget { class DeviceListScreenState extends State { DevicesBloc _devicesBloc; StreamSubscription _appStateSubscription; + bool _shouldRunOnResume = true; @override void didUpdateWidget(DevicesListScreen oldWidget) { @@ -39,13 +40,13 @@ class DeviceListScreenState extends State { Fimber.d("navigate to details"); _onPause(); await Navigator.pushNamed(context, "/details"); - _shouldRunOnResume = true; + setState(() { + _shouldRunOnResume = true; + }); Fimber.d("back from details"); }); } - bool _shouldRunOnResume = true; - @override void didChangeDependencies() { super.didChangeDependencies(); From 7a632aea233d0f897c0d3dae7a56f61d895cd395 Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Fri, 3 Jul 2020 18:33:50 +0200 Subject: [PATCH 10/12] Fixed race condition in scan peripherals (#490) * Fixed race condition in scan peripherals Under certain circumstances a problem can be reported by the native side very quickly. Since monitoring for callbacks started after the call, those responses could be delivered before anyone would start to listen. --- lib/src/bridge/scanning_mixin.dart | 53 +++++++++++++++++------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/src/bridge/scanning_mixin.dart b/lib/src/bridge/scanning_mixin.dart index 9b099f1c..d05a9809 100644 --- a/lib/src/bridge/scanning_mixin.dart +++ b/lib/src/bridge/scanning_mixin.dart @@ -1,11 +1,19 @@ part of _internal; mixin ScanningMixin on FlutterBLE { - Stream _scanEvents; + Stream _scanEvents; void _prepareScanEventsStream() { - _scanEvents = - const EventChannel(ChannelName.scanningEvents).receiveBroadcastStream(); + _scanEvents = const EventChannel(ChannelName.scanningEvents) + .receiveBroadcastStream() + .handleError( + (errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details)), + test: (error) => error is PlatformException, + ) + .map( + (scanResultJson) => + ScanResult.fromJson(jsonDecode(scanResultJson), _manager), + ); } Stream startDeviceScan( @@ -13,30 +21,29 @@ mixin ScanningMixin on FlutterBLE { int callbackType, List uuids, bool allowDuplicates, - ) async* { - _methodChannel.invokeMethod( - MethodName.startDeviceScan, - { - ArgumentName.scanMode: scanMode, - ArgumentName.callbackType: callbackType, - ArgumentName.uuids: uuids, - ArgumentName.allowDuplicates: allowDuplicates, - }, - ); - + ) { if (_scanEvents == null) { _prepareScanEventsStream(); } - yield* _scanEvents.handleError( - (errorJson) { - throw BleError.fromJson(jsonDecode(errorJson.details)); - }, - test: (error) => error is PlatformException, - ).map((scanResultJson) => ScanResult.fromJson( - jsonDecode(scanResultJson), - _manager, - )); + StreamController streamController = StreamController.broadcast( + onListen: () => _methodChannel.invokeMethod( + MethodName.startDeviceScan, + { + ArgumentName.scanMode: scanMode, + ArgumentName.callbackType: callbackType, + ArgumentName.uuids: uuids, + ArgumentName.allowDuplicates: allowDuplicates, + }, + ), + onCancel: () => stopDeviceScan(), + ); + + streamController + .addStream(_scanEvents, cancelOnError: true) + .then((_) => streamController?.close()); + + return streamController.stream; } Future stopDeviceScan() async { From 631e6ca1d89106fbb423367841834b2ce4a2f871 Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Mon, 6 Jul 2020 13:37:21 +0200 Subject: [PATCH 11/12] Updated changelog. --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ef6d943..36a34e29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.2.6 + +* Fixed scan quick failures not being reported to the listener (race condition in scanning_mixin.dart) + ## 2.2.5 * add missing handling of destroyClient call on iOS From d23f2cdde85ae4ad459121db59986520d8f34bec Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Mon, 6 Jul 2020 13:40:51 +0200 Subject: [PATCH 12/12] Bump version to 2.2.6 --- android/build.gradle | 2 +- ios/flutter_ble_lib.podspec | 2 +- pubspec.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 2423646c..a397b0eb 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,5 +1,5 @@ group 'com.polidea.flutter_ble_lib' -version '2.2.5' +version '2.2.6' buildscript { repositories { diff --git a/ios/flutter_ble_lib.podspec b/ios/flutter_ble_lib.podspec index aa03a4bf..51f47c01 100644 --- a/ios/flutter_ble_lib.podspec +++ b/ios/flutter_ble_lib.podspec @@ -3,7 +3,7 @@ # Pod::Spec.new do |s| s.name = 'flutter_ble_lib' - s.version = '2.2.5' + s.version = '2.2.6' s.summary = 'A new flutter plugin project.' s.description = <<-DESC A new flutter plugin project. diff --git a/pubspec.yaml b/pubspec.yaml index 9c1d1ebc..beb0c30c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_ble_lib description: FlutterBle Library is a flutter library that supports BLE operations. It uses MultiPlatformBleAdapter as a native backend.. -version: 2.2.5 +version: 2.2.6 homepage: https://github.com/Polidea/FlutterBleLib environment: