Skip to content
advanced Phase 8 · Media & Hardware

Core Bluetooth

Scan, connect, and communicate with BLE peripherals for IoT and accessory integration.

55m
3 problems
Topic Progress 0%

BLE Fundamentals

Core Bluetooth Overview

Core Bluetooth communicates with BLE (Bluetooth Low Energy) devices. Key concepts:

  • Central: Your app (reads/writes)
  • Peripheral: The device (e.g., fitness tracker)
  • Service: A collection of related data (e.g., Heart Rate Service)
  • Characteristic: A single data point (e.g., Heart Rate Measurement)

CBCentralManager

import CoreBluetooth

class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate {
    var centralManager: CBCentralManager!
    @Published var discoveredPeripherals: [CBPeripheral] = []
    
    override init() {
        super.init()
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .poweredOn:
            central.scanForPeripherals(withServices: nil, options: nil)
        case .poweredOff:
            print("Bluetooth is off")
        default:
            print("Bluetooth state: \(central.state.rawValue)")
        }
    }
}

Scanning

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,
                    advertisementData: [String: Any], rssi RSSI: NSNumber) {
    discoveredPeripherals.append(peripheral)
}

Connection & Services

Connect to Peripheral

func connect(to peripheral: CBPeripheral) {
    peripheral.delegate = self
    centralManager.connect(peripheral, options: nil)
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    peripheral.discoverServices(nil)
}

Discover Services

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    guard let services = peripheral.services else { return }
    for service in services {
        peripheral.discoverCharacteristics(nil, for: service)
    }
}

Read/Write Characteristics

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    guard let characteristics = service.characteristics else { return }
    for char in characteristics {
        if char.properties.contains(.read) {
            peripheral.readValue(for: char)
        }
        if char.properties.contains(.notify) {
            peripheral.setNotifyValue(true, for: char)
        }
    }
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    if let data = characteristic.value {
        print("Received: \(data)")
    }
}

Write to Characteristic

let data = "Hello".data(using: .utf8)!
peripheral.writeValue(data, for: characteristic, type: .withResponse)

Quiz

1. What is a BLE Central?

Question 1 options

2. What must you do before communicating with a peripheral?

Question 2 options

Flashcards

Question

What is the difference between Central and Peripheral?

Answer

Central is the reader/writer (your app). Peripheral is the device providing data (fitness tracker, sensor).

Question

What is a characteristic?

Answer

A single data point within a service, like heart rate measurement or battery level.

Revision Notes

Key Takeaways

  • 1. Check Bluetooth state before scanning
  • 2. Always discover services before accessing characteristics
  • 3. Use setNotifyValue for real-time updates
  • 4. Handle errors at every step

Interview Tips

  • Explain the BLE communication model
  • Discuss power optimization for BLE scanning
  • Show characteristic read/write pattern

Cheat Sheet

BLE: Central (app) connects to Peripheral (device). Discover services, then characteristics. Read/Write/Subscribe to characteristics.