iOS Core Bluetooth: Building BLE Apps for iPhone
Using Core Bluetooth framework for BLE
iOS Core Bluetooth Guide
Core Bluetooth is Apple's BLE framework for iOS, macOS, watchOS, and tvOS. It uses a delegate-based API centered on CBCentralManager (the central role) and CBPeripheralManager (the peripheral role).
CBCentralManager Setup
Always check radio state before scanning:
class BLEManager: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager!
override init() {
super.init()
centralManager = CBCentralManager(delegate: self,
queue: .global(qos: .userInitiated))
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
guard central.state == .poweredOn else { return }
let uuid = CBUUID(string: "6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
central.scanForPeripherals(withServices: [uuid], options: nil)
}
}
CBPeripheral and Service Discovery
Discovery is asynchronous and delegate-driven. Chain calls through delegate methods:
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
peripheral.services?.forEach { peripheral.discoverCharacteristics(nil, for: $0) }
}
func peripheral(_ peripheral: CBPeripheral,
didDiscoverCharacteristicsFor service: CBService, error: Error?) {
service.characteristics?.forEach {
if $0.properties.contains(.notify) { peripheral.setNotifyValue(true, for: $0) }
}
}
Use the GATT Profile Browser to resolve UUID values to standard service names during development.
Characteristic Properties
CBCharacteristicProperties |
Use Case |
|---|---|
.read |
Poll sensor value |
.writeWithoutResponse |
High-rate command stream |
.write |
Reliable command with ACK |
.notify |
Server-initiated push, no ACK |
.indicate |
Reliable server-initiated push |
Background Modes
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>
| Mode | Background Behavior |
|---|---|
bluetooth-central |
Scanning, connections, and callbacks continue |
bluetooth-peripheral |
Advertising continues at reduced rate |
| Neither | All BLE paused on app backgrounding |
State restoration (CBCentralManagerOptionRestoreIdentifierKey) re-launches the app when the system kills it while a connection is active. Bonding is transparent — iOS stores keys in the Keychain automatically. Reconnect known devices with retrievePeripherals(withIdentifiers:) rather than scanning again.
For the Android equivalent, see Android BLE Development.
Frequently Asked Questions
Yes, our guides range from beginner introductions to advanced topics. Each guide indicates its difficulty level and prerequisites so you can find the right starting point.