HomeiOS Developmentios - Difficulty with stopping Bluetooth scanning in Swift static library

ios – Difficulty with stopping Bluetooth scanning in Swift static library


I’ve constructed a static library in Swift that handles Bluetooth Low Power (BLE) operations like scanning, connection, and authentication. Once I import this library into my undertaking and use it to scan for units for 2 seconds, if units are discovered, I connect with the closest gadget. Nonetheless, after scanning for 2 seconds, when I attempt to cease scanning utilizing the centralManager.stopScan() methodology, it returns nil.

This is how I am utilizing the library for my part:

import MYStaticLib

@ObservedObject personal var myBLEController = BluetoothViewController()

DispatchQueue.principal.asyncAfter(deadline: .now() + 2.0) {
    myBLEController.stopScanning()
    if myBLEController.devicesFound.depend > 0 {
        print("Units Discovered : (myBLEController.devicesFound.depend)")
        print("Gadget With Minimal Distance : (myBLEController.devicesDistance.min()!)")
        print("Gadget With Minimal Distance Index : (myBLEController.returnLowestValueIndex())")
        myBLEController.gadget = myBLEController.devicesFound[myBLEController.devicesDistance.returnLowestValueIndex()]
        self.join(dev: myBLEController.gadget!, uida: uida)
    }
}

And here is the code for the BluetoothViewController class inside the library:

class BluetoothViewController: UIViewController, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {

    personal var centralManager: CBCentralManager!
    var gadget: CBPeripheral?
    @Revealed var devicesFound : [CBPeripheral] = []
    var devicesDistance : Array<Int> = []

    func startScanning () {
        if isBluetoothOn {
            devicesFound = []
            centralManager.scanForPeripherals(withServices: [])
        }
    }

    func stopScanning (){
        centralManager.stopScan()
    }

    func join(dev:CBPeripheral, uida : String){
        centralManager.join(dev,choices: nil)
    }

}

extension BluetoothViewController {
    // All Delegate strategies
    func centralManagerDidUpdateState(_ central: CBCentralManager) {}
    // didDiscover
    // didConnect
}

The difficulty I am going through is that if I create this controller inside the app, it really works advantageous. Nonetheless, once I use it inside the library, it throws an exception.

Can anybody assist me perceive what is perhaps inflicting this difficulty and the way I can resolve it?

Tags: swift, bluetooth-lowenergy, static-library, centralmanager, coredatadelegate

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments