[swift] Swift로 디바이스의 Bluetooth LE 지원 여부 확인하기

Bluetooth Low Energy (LE)는 저전력으로 동작하는 디바이스 간 통신을 지원하는 기술입니다. Swift를 사용하여 iOS 디바이스에서 Bluetooth LE를 지원하는지 확인하는 방법에 대해 알아보겠습니다.

CBCentralManager 활용

iOS에서 Bluetooth LE를 사용하기 위해 CBCentralManager 클래스를 활용할 수 있습니다. 이 클래스는 중앙 디바이스로서 Bluetooth LE 장치를 스캔하고 연결할 수 있는 기능을 제공합니다.

먼저, CBCentralManager 객체를 생성하고 state 속성을 확인하여 Bluetooth LE 지원 여부를 확인할 수 있습니다. 아래의 코드 예시를 참고해주세요.

import CoreBluetooth

let centralManager = CBCentralManager()

switch centralManager.state {
case .poweredOn:
    print("Bluetooth LE 지원 가능")
case .poweredOff:
    print("Bluetooth LE 지원 불가")
case .unsupported:
    print("Bluetooth LE 미지원")
case .unauthorized:
    print("Bluetooth LE 권한 없음")
case .unknown:
    print("Bluetooth 상태 알 수 없음")
@unknown default:
    print("알 수 없는 오류 발생")
}

위의 코드에서 CBCentralManagerstate 속성을 확인하여 Bluetooth LE의 지원 가능 여부를 확인할 수 있습니다. poweredOn은 Bluetooth LE를 지원하고, poweredOff는 지원하지 않는 상태를 나타냅니다.

위의 코드 예시를 실행하여 Bluetooth LE 지원 여부를 확인할 수 있습니다.

참고 자료