From 4ed5e9980139a9521fcd4dcdd7a97084875bb7b6 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Thu, 7 May 2020 13:55:05 -0400 Subject: [PATCH] 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];