From 841a90f1c19b9e8be9e8592cc8359a4ec25725ce Mon Sep 17 00:00:00 2001 From: DesBaiHaoYang Date: Wed, 30 Sep 2020 12:24:48 +0800 Subject: [PATCH 1/2] change state --- ios/Classes/FlutterBluetoothBasicPlugin.m | 26 ++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/ios/Classes/FlutterBluetoothBasicPlugin.m b/ios/Classes/FlutterBluetoothBasicPlugin.m index de60571..63405f3 100644 --- a/ios/Classes/FlutterBluetoothBasicPlugin.m +++ b/ios/Classes/FlutterBluetoothBasicPlugin.m @@ -6,6 +6,9 @@ @interface FlutterBluetoothBasicPlugin () @property(nonatomic, retain) FlutterMethodChannel *channel; @property(nonatomic, retain) BluetoothPrintStreamHandler *stateStreamHandler; @property(nonatomic) NSMutableDictionary *scannedPeripherals; +@property(nonatomic,assign) NSInteger stateIndex; +@property(nonatomic,assign) NSInteger bluetoothState; +; @end @implementation FlutterBluetoothBasicPlugin @@ -23,7 +26,8 @@ + (void)registerWithRegistrar:(NSObject*)registrar { BluetoothPrintStreamHandler* stateStreamHandler = [[BluetoothPrintStreamHandler alloc] init]; [stateChannel setStreamHandler:stateStreamHandler]; instance.stateStreamHandler = stateStreamHandler; - + instance.stateIndex = -1; + instance.bluetoothState = 1; [registrar addMethodCallDelegate:instance channel:channel]; } @@ -33,35 +37,42 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if ([@"state" isEqualToString:call.method]) { result(nil); } else if([@"isAvailable" isEqualToString:call.method]) { - - result(@(YES)); + bool isAvailable = self.bluetoothState ==1; + result(@(isAvailable)); } else if([@"isConnected" isEqualToString:call.method]) { result(@(NO)); + } + else if([@"connectStateInteger" isEqualToString:call.method]) { + result(@(self.stateIndex)); } else if([@"isOn" isEqualToString:call.method]) { result(@(YES)); }else if([@"startScan" isEqualToString:call.method]) { NSLog(@"getDevices method -> %@", call.method); [self.scannedPeripherals removeAllObjects]; - if (Manager.bleConnecter == nil) { [Manager didUpdateState:^(NSInteger state) { switch (state) { case CBCentralManagerStateUnsupported: + self.bluetoothState = 4; NSLog(@"The platform/hardware doesn't support Bluetooth Low Energy."); break; case CBCentralManagerStateUnauthorized: + self.bluetoothState = 3; NSLog(@"The app is not authorized to use Bluetooth Low Energy."); break; case CBCentralManagerStatePoweredOff: + self.bluetoothState = 2; NSLog(@"Bluetooth is currently powered off."); break; case CBCentralManagerStatePoweredOn: [self startScan]; + self.bluetoothState = 1; NSLog(@"Bluetooth power on"); break; case CBCentralManagerStateUnknown: - default: + + default: self.bluetoothState = -1; break; } }]; @@ -139,23 +150,28 @@ -(void)updateConnectState:(ConnectState)state { switch (state) { case CONNECT_STATE_CONNECTING: NSLog(@"status -> %@", @"Connecting ..."); + self.stateIndex = 0; ret = @0; break; case CONNECT_STATE_CONNECTED: NSLog(@"status -> %@", @"Connection success"); ret = @1; + self.stateIndex = 1; break; case CONNECT_STATE_FAILT: NSLog(@"status -> %@", @"Connection failed"); ret = @0; + self.stateIndex = 2; break; case CONNECT_STATE_DISCONNECT: NSLog(@"status -> %@", @"Disconnected"); + self.stateIndex = 3; ret = @0; break; default: NSLog(@"status -> %@", @"Connection timed out"); ret = @0; + self.stateIndex = -1; break; } From 94af896935e28b4631bd8334465b1080eb041af0 Mon Sep 17 00:00:00 2001 From: DesBaiHaoYang Date: Wed, 30 Sep 2020 12:29:21 +0800 Subject: [PATCH 2/2] commite --- lib/src/bluetooth_manager.dart | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/src/bluetooth_manager.dart b/lib/src/bluetooth_manager.dart index c37faf4..785fcb5 100644 --- a/lib/src/bluetooth_manager.dart +++ b/lib/src/bluetooth_manager.dart @@ -30,8 +30,8 @@ class BluetoothManager { static BluetoothManager get instance => _instance; - // Future get isAvailable async => - // await _channel.invokeMethod('isAvailable').then((d) => d); + Future get isAvailable async => + await _channel.invokeMethod('isAvailable'); // Future get isOn async => // await _channel.invokeMethod('isOn').then((d) => d); @@ -39,6 +39,9 @@ class BluetoothManager { Future get isConnected async => await _channel.invokeMethod('isConnected'); + Future get connectStateInteger async => + await _channel.invokeMethod('connectStateInteger'); + BehaviorSubject _isScanning = BehaviorSubject.seeded(false); Stream get isScanning => _isScanning.stream;