CoreBluetooth for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CoreBluetooth for Swift: Complete Guide to Bluetooth Low Energy

\n\n

1. What It Is and Why You Need It

\n\n

CoreBluetooth 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\n

Why 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\n

It 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).

\n\n

2. Installation

\n\n

CoreBluetooth 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
  1. Open your project in Xcode.
  2. \n
  3. Select your application's target.
  4. \n
  5. Go to the "General" tab → "Frameworks, Libraries, and Embedded Content".
  6. \n
  7. Click "+" and select CoreBluetooth.framework.
  8. \n
\n\n

Or simply add the import in your code:

\n\n
import CoreBluetooth
\n\n

This 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\n

3. Quick Start — Minimal Working Example

\n\n

Below 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\n
import 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    }                    

Recommendations