투케이2K

354. (ios/swift5) OperationQueue, @escaping 사용해 CBPeripheralManager 블루투스 활성 상태 응답 값 콜백 (callback) 확인 본문

IOS

354. (ios/swift5) OperationQueue, @escaping 사용해 CBPeripheralManager 블루투스 활성 상태 응답 값 콜백 (callback) 확인

투케이2K 2023. 10. 23. 08:25
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 
 
 

[소스 코드]

    // -----------------------------------------------------------------------------------------
    // MARK: - [블루투스 활성 상태 확인 실시]
    // -----------------------------------------------------------------------------------------
    var checkBluetoothFlag = false
    private let bluetoothOperationQueue = OperationQueue()
    var peripheralManager: CBPeripheralManager! // [실시간 블루투스 신호 활성]
    
    func isBluetoothEnable(callback: @escaping (Bool) -> ()) {
        S_Log._D_(description: "블루투스 활성 상태 확인 실시", data: nil)
        
        /*
        // -----------------------------------------
        // [isBluetoothEnable 메소드 설명]
        // -----------------------------------------
        1. 필요 info plist :
         
         Privacy - Bluetooth Always Usage Description
         Privacy - Bluetooth Peripheral Usage Description
        // -----------------------------------------
        2. 필요 import 및 delegate :
         
         import CoreBluetooth
         
         CBPeripheralManagerDelegate
        // -----------------------------------------
        3. 호출 방법 :
         
         C_StateCheck().isBluetoothEnable(){(result) in
             
             S_Log._D_(description: "블루투스 활성 상태 확인", data: ["\(result)"])
             
         }
         
        // -----------------------------------------
        4. 참고 퍼미션 요청 알림 : 블루투스 활성 상태 확인 권한을 허용하시겠습니까?
        // -----------------------------------------
        */
        
        
        // [초기 플래그 값 초기화]
        self.checkBluetoothFlag = false
        
        
        // [작업 큐에 추가]
        self.bluetoothOperationQueue.isSuspended = true
        let block = { callback(self.checkBluetoothFlag) }
        self.bluetoothOperationQueue.addOperation(block)
        
        
        DispatchQueue.main.async {
         
            // [블루투스 활성 상태 확인 실시]
            self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
            
        }
        
    }
    
    
    
    
    
    
    // -----------------------------------------------------------------------------------------
    // MARK: - [블루투스 활성 상태 응답 확인] : CBPeripheralManagerDelegate : Delegate
    // -----------------------------------------------------------------------------------------
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        
        // -----------------------------------------
        if peripheral.state == .poweredOn {
            S_Log._D_(description: "블루투스 활성 상태 :: poweredOn :: BLE 활성", data: nil)
            self.checkBluetoothFlag = true
            self.bluetoothOperationQueue.isSuspended = false
        }
        // -----------------------------------------
        else if peripheral.state == .poweredOff {
            S_Log._D_(description: "블루투스 활성 상태 :: poweredOff :: BLE 비활성", data: nil)
            self.checkBluetoothFlag = false
            self.bluetoothOperationQueue.isSuspended = false
        }
        // -----------------------------------------
        else {
            S_Log._D_(description: "블루투스 활성 상태 :: else :: State",
                      data: ["peripheral.state :: " + String(describing: peripheral.state)])
            self.checkBluetoothFlag = false
            self.bluetoothOperationQueue.isSuspended = false
        }
        // -----------------------------------------
    }
 

[결과 출력]

 

================================================================
LOG :: CLASS PLACE :: C_StateCheck.swift :: peripheralManagerDidUpdateState(_:) :: 547
-------------------------------------------------
LOG :: NOW TIME :: 2023-10-23 08:16:47
-------------------------------------------------
LOG :: DESCRIPTION :: 블루투스 활성 상태 :: poweredOff :: BLE 비활성
================================================================




================================================================
LOG :: CLASS PLACE :: A_Webview.swift :: testMain() :: 1136
-------------------------------------------------
LOG :: NOW TIME :: 2023-10-23 08:16:47
-------------------------------------------------
LOG :: DESCRIPTION :: 블루투스 활성 상태 확인
-------------------------------------------------
LOG :: false
================================================================

 

반응형
Comments