CoreBluetooth for Swift: Complete Guide to Bluetooth Low Energy
\n\n1. What It Is and Why You Need It
\n\nCoreBluetooth is a framework from Apple that provides an API for interacting with devices via the Bluetooth Low Energy (BLE, Bluetooth 4.0 and higher) protocol. It is part of the iOS SDK and macOS SDK, making it a native tool for development within the Apple ecosystem. Unlike classic Bluetooth, BLE is designed for minimal power consumption, making it ideal for wearable devices, sensors, smart locks, fitness trackers, and IoT gadgets.
\n\nWhy do you need CoreBluetooth? If you are developing an iPhone application that needs to exchange data with a heart rate monitor, control a smart light bulb, or receive readings from a weather station, you will need this framework. It handles all the low-level work: scanning the airwaves, establishing connections, discovering services and characteristics, and reading/writing data. CoreBluetooth operates in two roles: Central (the device that searches and connects) and Peripheral (the device that advertises itself and provides data). In this article, we will focus on the Central role, as it is the most commonly used in applications.
\n\nIt is important to understand that CoreBluetooth is not a third-party library but a system framework. It does not need to be installed via a package manager; it is available "out of the box" in Xcode. However, for proper operation, you must add a description of Bluetooth usage in Info.plist (the key NSBluetoothAlwaysUsageDescription for iOS 13+ or NSBluetoothPeripheralUsageDescription for older versions).
2. Installation
\n\nCoreBluetooth does not require installation via pip or CocoaPods. It is part of the Apple standard library. To add the framework to your project:
\n\n- \n
- Open your project in Xcode. \n
- Select your application's target. \n
- Go to the "General" tab → "Frameworks, Libraries, and Embedded Content". \n
- Click "+" and select
CoreBluetooth.framework. \n
Or simply add the import in your code:
\n\nimport CoreBluetooth\n\nThis will also work for Bluetooth on the iOS 14+ and macOS 11+ simulator, but real BLE devices are only available on a physical device. On the simulator, you can only test scanning and connection logic, not data transfer.
\n\n3. Quick Start — Minimal Working Example
\n\nBelow is minimal code that initializes a central device (Central Manager) and starts scanning all available BLE devices. The result is printed to the console.
\n\nimport UIKit\nimport CoreBluetooth\n\nclass ViewController: UIViewController, CBCentralManagerDelegate {\n\n var centralManager: CBCentralManager!\n\n override func viewDidLoad() {\n super.viewDidLoad()\n // Initialize the Central Manager with a delegate\n centralManager = CBCentralManager(delegate: self, queue: nil)\n }\n\n // MARK: - CBCentralManagerDelegate\n\n func centralManagerDidUpdateState(_ central: CBCentralManager) {\n switch central.state {\n case .poweredOn:\n print("Bluetooth is on. Starting scan...")\n // Start scanning all devices\n centralManager.scanForPeripherals(withServices: nil, options: nil)\n case .poweredOff:\n print("Bluetooth is off")\n case .unauthorized:\n print("App is not authorized for Bluetooth")\n case .unknown:\n print("Bluetooth state is unknown")\n case .unsupported:\n print("Device does not support BLE")\n case .resetting:\n print("Bluetooth is resetting")\n @unknown default:\n print("Unknown state")\n }\n } Also in library
Navigation Component for Kotlin: Complete Guide with Examples
gRPC for Swift (iOS/macOS): Libraries, Setup, and Code Examples
csv — Working with CSV Files in Rust