From 6c459f82baf3565c1916579bbf06b3ac42766a13 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Thu, 7 May 2020 13:55:05 -0400 Subject: [PATCH 1/2] Don't initiliazie CBCentralManager until needed By deferring the initialization of `CBCentralManager`, we are able to defer prompting for Bluetooth permission on iOS until a flutter_blue method is called. Otherwise, the Bluetooth permission prompt appears right when the app starts up. --- ios/Classes/FlutterBluePlugin.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ios/Classes/FlutterBluePlugin.m b/ios/Classes/FlutterBluePlugin.m index 65a73b6d..3f03325b 100644 --- a/ios/Classes/FlutterBluePlugin.m +++ b/ios/Classes/FlutterBluePlugin.m @@ -48,7 +48,6 @@ + (void)registerWithRegistrar:(NSObject*)registrar { FlutterEventChannel* stateChannel = [FlutterEventChannel eventChannelWithName:NAMESPACE @"/state" binaryMessenger:[registrar messenger]]; FlutterBluePlugin* instance = [[FlutterBluePlugin alloc] init]; instance.channel = channel; - instance.centralManager = [[CBCentralManager alloc] initWithDelegate:instance queue:nil]; instance.scannedPeripherals = [NSMutableDictionary new]; instance.servicesThatNeedDiscovered = [NSMutableArray new]; instance.characteristicsThatNeedDiscovered = [NSMutableArray new]; @@ -63,6 +62,9 @@ + (void)registerWithRegistrar:(NSObject*)registrar { } - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { + if (self.centralManager == nil) { + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; + } if ([@"setLogLevel" isEqualToString:call.method]) { NSNumber *logLevelIndex = [call arguments]; _logLevel = (LogLevel)[logLevelIndex integerValue]; From f8ed138dde0a090280affec5092ecaed61cb30fe Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Mon, 18 Oct 2021 15:33:39 +1100 Subject: [PATCH 2/2] iOS: do not call isAvailable during instantiation that triggers popup --- ios/Classes/FlutterBluePlugin.m | 10 ++++++---- lib/src/flutter_blue.dart | 6 ++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ios/Classes/FlutterBluePlugin.m b/ios/Classes/FlutterBluePlugin.m index 3f03325b..2b9de34f 100644 --- a/ios/Classes/FlutterBluePlugin.m +++ b/ios/Classes/FlutterBluePlugin.m @@ -62,14 +62,16 @@ + (void)registerWithRegistrar:(NSObject*)registrar { } - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - if (self.centralManager == nil) { - self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; - } if ([@"setLogLevel" isEqualToString:call.method]) { NSNumber *logLevelIndex = [call arguments]; _logLevel = (LogLevel)[logLevelIndex integerValue]; result(nil); - } else if ([@"state" isEqualToString:call.method]) { + return; + } + if (self.centralManager == nil) { + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; + } + if ([@"state" isEqualToString:call.method]) { FlutterStandardTypedData *data = [self toFlutterData:[self toBluetoothStateProto:self->_centralManager.state]]; result(data); } else if([@"isAvailable" isEqualToString:call.method]) { diff --git a/lib/src/flutter_blue.dart b/lib/src/flutter_blue.dart index 6d2f76e9..40957fdc 100644 --- a/lib/src/flutter_blue.dart +++ b/lib/src/flutter_blue.dart @@ -74,10 +74,8 @@ class FlutterBlue { } _setLogLevelIfAvailable() async { - if (await isAvailable) { - // Send the log level to the underlying platforms. - setLogLevel(logLevel); - } + // Send the log level to the underlying platforms. + setLogLevel(logLevel); } /// Starts a scan for Bluetooth Low Energy devices and returns a stream