diff --git a/packages/flutter_reactive_ble/lib/src/reactive_ble.dart b/packages/flutter_reactive_ble/lib/src/reactive_ble.dart index 94f1188c..591b1898 100644 --- a/packages/flutter_reactive_ble/lib/src/reactive_ble.dart +++ b/packages/flutter_reactive_ble/lib/src/reactive_ble.dart @@ -25,9 +25,9 @@ class FlutterReactiveBle { required DeviceScanner deviceScanner, required DeviceConnector deviceConnector, required ConnectedDeviceOperation connectedDeviceOperation, - required Logger debugLogger, required Future initialization, required ReactiveBlePlatform reactiveBlePlatform, + Logger? debugLogger, }) { _deviceScanner = deviceScanner; _deviceConnector = deviceConnector; @@ -90,7 +90,7 @@ class FlutterReactiveBle { late DeviceConnector _deviceConnector; late ConnectedDeviceOperation _connectedDeviceOperator; late DeviceScanner _deviceScanner; - late Logger _debugLogger; + Logger? _debugLogger; /// Initializes this [FlutterReactiveBle] instance and its platform-specific /// counterparts. @@ -433,9 +433,19 @@ class FlutterReactiveBle { /// /// Use [LogLevel.verbose] for full debug output. Make sure to run this only for debugging purposes. /// Use [LogLevel.none] to disable logging. This is also the default. - set logLevel(LogLevel logLevel) => _debugLogger.logLevel = logLevel; + set logLevel(LogLevel logLevel) => _debugLogger?.logLevel = logLevel; - LogLevel get logLevel => _debugLogger.logLevel; + LogLevel get logLevel => _debugLogger?.logLevel ?? LogLevel.none; + + /// Sets the logger. + /// + /// Set to null to disable logging. + set logger(Logger? logger) { + _debugLogger = logger; + _blePlatform.logger = logger; + } + + Logger? get logger => _debugLogger; } /// An instance of this object should not be used after its device has lost its connection. diff --git a/packages/reactive_ble_mobile/lib/src/reactive_ble_mobile_platform.dart b/packages/reactive_ble_mobile/lib/src/reactive_ble_mobile_platform.dart index 2c36a366..952d2908 100644 --- a/packages/reactive_ble_mobile/lib/src/reactive_ble_mobile_platform.dart +++ b/packages/reactive_ble_mobile/lib/src/reactive_ble_mobile_platform.dart @@ -20,8 +20,9 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { _connectedDeviceRawStream = connectedDeviceChannel, _charUpdateRawStream = charUpdateChannel, _bleStatusRawChannel = bleStatusChannel, - _bleDeviceScanRawStream = bleDeviceScanChannel, - _logger = logger; + _bleDeviceScanRawStream = bleDeviceScanChannel{ + super.logger = logger; + } final ArgsToProtobufConverter _argsToProtobufConverter; final ProtobufConverter _protobufConverter; @@ -30,7 +31,6 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { final Stream> _charUpdateRawStream; final Stream> _bleDeviceScanRawStream; final Stream> _bleStatusRawChannel; - final Logger? _logger; Stream? _connectionUpdateStream; Stream? _charValueStream; @@ -43,7 +43,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { .map(_protobufConverter.connectionStateUpdateFrom) .map( (update) { - _logger?.log( + logger?.log( 'Received $ConnectionStateUpdate(deviceId: ${update.deviceId}, connectionState: ${update.connectionState}, failure: ${update.failure})', ); return update; @@ -56,7 +56,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { .map(_protobufConverter.characteristicValueFrom) .map( (update) { - _logger?.log( + logger?.log( 'Received $CharacteristicValue(characteristic: ${update.characteristic}, result: ${update.runtimeType})', ); return update; @@ -67,7 +67,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { Stream get scanStream => _scanResultStream ??= _bleDeviceScanRawStream.map(_protobufConverter.scanResultFrom).map( (scanResult) { - _logger?.log( + logger?.log( 'Received $ScanResult(result: ${scanResult.result})', ); return scanResult; @@ -79,19 +79,19 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { _bleStatusStream ??= _bleStatusRawChannel .map(_protobufConverter.bleStatusFrom) .map((status) { - _logger?.log('Received $BleStatus update: $status'); + logger?.log('Received $BleStatus update: $status'); return status; }); @override Future initialize() { - _logger?.log('Initialize BLE platform'); + logger?.log('Initialize BLE platform'); return _bleMethodChannel.invokeMethod("initialize"); } @override Future deinitialize() { - _logger?.log('Deinitialize BLE platform'); + logger?.log('Deinitialize BLE platform'); return _bleMethodChannel.invokeMethod("deinitialize"); } @@ -101,7 +101,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { required ScanMode scanMode, required bool requireLocationServicesEnabled, }) { - _logger?.log( + logger?.log( 'Scan for devices with services:$withServices, scanMode: $scanMode, requireLocationServicesEnabled: $requireLocationServicesEnabled', ); return _bleMethodChannel @@ -124,7 +124,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { Map>? servicesWithCharacteristicsToDiscover, Duration? connectionTimeout, ) { - _logger?.log( + logger?.log( 'Connect to device: $id, servicesWithCharacteristicsToDiscover: $servicesWithCharacteristicsToDiscover, timeout: $connectionTimeout', ); return _bleMethodChannel @@ -143,7 +143,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Future disconnectDevice(String deviceId) { - _logger?.log( + logger?.log( 'Disconnect device: $deviceId', ); return _bleMethodChannel.invokeMethod( @@ -156,7 +156,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Stream readCharacteristic(CharacteristicInstance characteristic) { - _logger?.log( + logger?.log( 'Read characteristic: $characteristic', ); return _bleMethodChannel @@ -174,7 +174,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { CharacteristicInstance characteristic, List value, ) async { - _logger?.log('Write with response to $characteristic, value: $value'); + logger?.log('Write with response to $characteristic, value: $value'); return _bleMethodChannel .invokeMethod>( "writeCharacteristicWithResponse", @@ -189,7 +189,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { CharacteristicInstance characteristic, List value, ) async { - _logger?.log( + logger?.log( 'Write without response to $characteristic, value: $value', ); return _bleMethodChannel @@ -206,7 +206,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { Stream subscribeToNotifications( CharacteristicInstance characteristic, ) { - _logger?.log('Start subscribing to notifications for $characteristic'); + logger?.log('Start subscribing to notifications for $characteristic'); return _bleMethodChannel .invokeMethod( "readNotifications", @@ -221,7 +221,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { Future stopSubscribingToNotifications( CharacteristicInstance characteristic, ) { - _logger?.log('Stop subscribing to notifications for $characteristic'); + logger?.log('Stop subscribing to notifications for $characteristic'); return _bleMethodChannel .invokeMethod( "stopNotifications", @@ -237,7 +237,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Future requestMtuSize(String deviceId, int? mtu) async { - _logger?.log('Request mtu size for device: $deviceId with mtuSize: $mtu'); + logger?.log('Request mtu size for device: $deviceId with mtuSize: $mtu'); return _bleMethodChannel .invokeMethod>( "negotiateMtuSize", @@ -251,7 +251,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Future requestConnectionPriority( String deviceId, ConnectionPriority priority) { - _logger?.log( + logger?.log( 'Request connection priority for device: $deviceId, priority: $priority'); return _bleMethodChannel .invokeMethod>( @@ -266,7 +266,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Future?>> clearGattCache( String deviceId) { - _logger?.log('Clear gatt cache for device: $deviceId'); + logger?.log('Clear gatt cache for device: $deviceId'); return _bleMethodChannel .invokeMethod>( "clearGattCache", @@ -279,7 +279,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Future> discoverServices(String deviceId) async { - _logger?.log('Discover services for device: $deviceId'); + logger?.log('Discover services for device: $deviceId'); return _bleMethodChannel .invokeMethod>( 'discoverServices', @@ -292,7 +292,7 @@ class ReactiveBleMobilePlatform extends ReactiveBlePlatform { @override Future> getDiscoverServices(String deviceId) async { - _logger?.log('Get discovered services for device: $deviceId'); + logger?.log('Get discovered services for device: $deviceId'); return _bleMethodChannel .invokeMethod>( 'getDiscoveredServices', diff --git a/packages/reactive_ble_platform_interface/lib/src/reactive_ble_platform_interface.dart b/packages/reactive_ble_platform_interface/lib/src/reactive_ble_platform_interface.dart index 6c620641..a1ba6a9c 100644 --- a/packages/reactive_ble_platform_interface/lib/src/reactive_ble_platform_interface.dart +++ b/packages/reactive_ble_platform_interface/lib/src/reactive_ble_platform_interface.dart @@ -1,4 +1,5 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +import 'package:reactive_ble_platform_interface/src/logger.dart'; import 'models.dart'; @@ -13,6 +14,8 @@ abstract class ReactiveBlePlatform extends PlatformInterface { ReactiveBlePlatform() : super(token: _token); static final Object _token = Object(); + Logger? logger; + static ReactiveBlePlatform _instance = _PlaceholderImplementation(); static ReactiveBlePlatform get instance => _instance;