Skip to content

Commit

Permalink
Don't initiliazie CBCentralManager until needed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rohansingh committed Jun 3, 2020
1 parent 63a1002 commit 4ed5e99
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ios/Classes/FlutterBluePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)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];
Expand All @@ -63,6 +62,9 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)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];
Expand Down

0 comments on commit 4ed5e99

Please sign in to comment.