HomeiOS Developmentios - Easy methods to CBPeripheralManager to proceed promote in background mode?

ios – Easy methods to CBPeripheralManager to proceed promote in background mode?


My purpose is to promote a beacon in background mode.

The anticipated result’s: The beacon proceed to promote in background mode.

The precise result’s: The beacon cease promoting in background mode.

What I’ve tried:

  1. Background process can prolong time for 30 seconds however this isn’t an answer.
import Basis
import CoreLocation
import CoreBluetooth

protocol BeaconBroadcaster {
    
    /// Broadcast to be discover by scanner.
    /// - Parameters:
    ///   - uuid: utilized by broadcast scanner to seek out particular uuid
    ///   - main and minor: utilized by broadcast scanner to distinguish beacon with an identical uuid
    func startBroadcasting(uuid: UUID, main: UInt16, minor: UInt16) -> BeaconBroadcasterStartBroadcastingEvent
    
    /// cease broadcaster
    func stopBroadcasting() -> BeaconBroadcasterStopBroadcastingEvent
}

class BeaconBroadcasterImpl: BeaconBroadcaster {
    non-public var peripheralManager = CBPeripheralManager()
    
    fileprivate init() {
#if DEBUG
        print("(sort(of: self)) (#operate)")
#endif
    }
    
    func startBroadcasting(uuid: UUID, main: UInt16, minor: UInt16) -> BeaconBroadcasterStartBroadcastingEvent {
        if peripheralManager.state == .poweredOff {
            return .BLUETOOTH_NOT_ENABLED
        }
        
        if peripheralManager.state == .unauthorized {
            return .BLUETOOTH_NOT_AUTHORIZED
        }
        
        if peripheralManager.state != .poweredOn {
            return .BLUETOOTH_NOT_AUTHORIZED
        }
        
        if peripheralManager.isAdvertising {
            _ = stopBroadcasting()
        }
        
        let bundleURL = Bundle.primary.bundleIdentifier!
        let constraint = CLBeaconIdentityConstraint(uuid: uuid, main: main, minor: minor)
        let area = CLBeaconRegion(beaconIdentityConstraint: constraint, identifier: bundleURL)
        let peripheralData = area.peripheralData(withMeasuredPower: nil) as? [String: Any]
        peripheralManager.startAdvertising(peripheralData)
        
        return .BROADCASTER_IS_STARTING
    }
    
    func stopBroadcasting() -> BeaconBroadcasterStopBroadcastingEvent {
        if !peripheralManager.isAdvertising {
            return .BROADCASTER_HAS_ALREADY_STOPPED
        }
        
        peripheralManager.stopAdvertising()
        
        return .BROADCASTER_IS_STOPPING
    }
}

extension BeaconBroadcasterImpl {
    
    /// Utilization: use pressure unwrap, it's going to at all times return non-nil worth
    static var shared: BeaconBroadcaster? {
        get {
            if sharedClosure == nil {
                sharedClosure = BeaconBroadcasterImpl()
            }
            
            return sharedClosure
        }
        set {
            sharedClosure = newValue
        }
    }
    
    non-public static var sharedClosure: BeaconBroadcaster?
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments