diff --git a/android/build.gradle b/android/build.gradle index 1afb315..e12da71 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,14 +2,14 @@ group 'com.kiwi.flutterscanbluetooth' version '1.0-SNAPSHOT' buildscript { - ext.kotlin_version = '1.3.41' + ext.kotlin_version = '1.7.20' repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' + classpath 'com.android.tools.build:gradle:7.3.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/ios/Classes/SwiftFlutterScanBluetoothPlugin.swift b/ios/Classes/SwiftFlutterScanBluetoothPlugin.swift index 34332ab..ad406b1 100644 --- a/ios/Classes/SwiftFlutterScanBluetoothPlugin.swift +++ b/ios/Classes/SwiftFlutterScanBluetoothPlugin.swift @@ -18,6 +18,8 @@ extension SwiftFlutterScanBluetoothPlugin: CBCentralManagerDelegate { print("central.state is .poweredOff") case .poweredOn: print("central.state is .poweredOn") + @unknown default: + break; } } @@ -32,7 +34,7 @@ extension SwiftFlutterScanBluetoothPlugin: CBCentralManagerDelegate { } public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin { - var centralManager: CBCentralManager! = CBCentralManager(delegate: nil, queue: nil) + var centralManager: CBCentralManager? var bluetoothState: CBManagerState = .unknown let channel: FlutterMethodChannel var scanTimer: Timer? @@ -40,7 +42,6 @@ public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin { init(_ channel: FlutterMethodChannel) { self.channel = channel super.init() - centralManager.delegate = self } public static func register(with registrar: FlutterPluginRegistrar) { @@ -50,6 +51,11 @@ public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin { } public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + if(centralManager == nil){ + centralManager = CBCentralManager(delegate: nil, queue: nil); + + centralManager?.delegate = self + } if(bluetoothState == .unsupported) { return result(FlutterError.init(code: "error_no_bt", message: nil, details: nil)) } @@ -59,12 +65,12 @@ public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin { switch call.method { case "action_start_scan": - if(centralManager.isScanning) { + if(centralManager?.isScanning ?? false) { stopScan() } - centralManager.scanForPeripherals(withServices: nil, options: nil) + centralManager?.scanForPeripherals(withServices: nil, options: nil) - let bondedDevices = centralManager.retrieveConnectedPeripherals(withServices: []) + let bondedDevices = centralManager?.retrieveConnectedPeripherals(withServices: []) ?? [] var res = [Dictionary]() if(call.arguments as! Bool) { for device in bondedDevices { @@ -81,11 +87,15 @@ public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin { case "action_request_permissions": if(bluetoothState == .unauthorized) { if #available(iOS 13.0, *) { - switch centralManager.authorization { + switch centralManager?.authorization { case .denied, .restricted, .notDetermined: return result(FlutterError.init(code: "error_no_permission", message: nil, details: nil)) case .allowedAlways: break; + case nil: + break; + case .some(_): + break; } } } @@ -98,7 +108,7 @@ public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin { func stopScan() { scanTimer?.invalidate() - centralManager.stopScan() + centralManager?.stopScan() channel.invokeMethod("action_scan_stopped", arguments: nil) } } diff --git a/pubspec.yaml b/pubspec.yaml index 0fbccfe..3dbc44e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_scan_bluetooth description: Flutter plugin to scan Bluetooth devices. -version: 2.1.4 +version: 2.1.5 homepage: https://github.com/kiwi-bop/flutter_scan_bluetooth dependencies: