Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always ask permission when app started #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
24 changes: 17 additions & 7 deletions ios/Classes/SwiftFlutterScanBluetoothPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extension SwiftFlutterScanBluetoothPlugin: CBCentralManagerDelegate {
print("central.state is .poweredOff")
case .poweredOn:
print("central.state is .poweredOn")
@unknown default:
break;
}
}

Expand All @@ -32,15 +34,14 @@ 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?

init(_ channel: FlutterMethodChannel) {
self.channel = channel
super.init()
centralManager.delegate = self
}

public static func register(with registrar: FlutterPluginRegistrar) {
Expand All @@ -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))
}
Expand All @@ -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<String, String>]()
if(call.arguments as! Bool) {
for device in bondedDevices {
Expand All @@ -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;
}
}
}
Expand All @@ -98,7 +108,7 @@ public class SwiftFlutterScanBluetoothPlugin: NSObject, FlutterPlugin {

func stopScan() {
scanTimer?.invalidate()
centralManager.stopScan()
centralManager?.stopScan()
channel.invokeMethod("action_scan_stopped", arguments: nil)
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down