core-bluetooth

Build direct Bluetooth Low Energy workflows with Core Bluetooth. Use when implementing BLE central or peripheral GATT communication, scanning or connecting with CBCentralManager, discovering services and characteristics, reading/writing/subscribing with CBPeripheral, publishing local services with C

By dpearson2699 · 3,256 installs

npx skills add dpearson2699/swift-ios-skills --skill core-bluetooth

Source repository · Upstream listing

Core Bluetooth Scan for, connect to, and exchange data with Bluetooth Low Energy (BLE) devices. Covers the central role (scanning and connecting to peripherals), the peripheral role (advertising services), background modes, and state restoration. Use accessorysetupkit for privacy preserving accessory discovery and setup; use this skill for direct Core Bluetooth GATT communication. Contents [Setup]( setup) [Central Role: Scanning]( central role scanning) [Central Role: Connecting]( central role connecting) [Discovering Services and Characteristics]( discovering services and characteristics) [Reading, Writing, and Notifications]( reading writing and notifications) [Peripheral Role: Advertising]( peripheral role advertising) [Background BLE]( background ble) [State Restoration]( state restoration) [Common Mistakes]( common mistakes) [Review Checklist]( review checklist) [References]( references) Setup Info.plist Keys Key Purpose NSBluetoothAlwaysUsageDescription Required. Explains why the app uses Bluetooth UIBackgroundModes with bluetooth central Background scanning and connecting UIBackgroundModes with bluetooth peripheral Background advertising Bluetooth Authorization Core Bluetooth has no explicit permission request API. Add NSBluetoothAlwaysUsageDescription , create the manager when the app is ready for Bluetooth access, then check manager.authorization and manager.state . Treat .denied and .restricted as terminal until the user changes Settings; wait for .poweredOn before scanning, connecting, advertising, or publishing services. Central Role: Scanning Creating the Central Manager Always wait for the poweredOn state before scanning. Scanning for Peripherals Scan for specific service UUIDs to save power. Pass nil to discover all peripherals (not recommended in production). Central Role: Connecting Discovering Services and Characteristics Implement CBPeripheralDelegate to walk the service/characteristic tree. Reading, Writing, and Notifications Reading a Value Writing a Value Subscribing to Notifications Peripheral Role: Advertising Publish services from the local device using CBPeripheralManager . Background BLE Background Central Mode Add bluetooth central to UIBackgroundModes . In the background: Scanning must specify one or more service UUIDs; nil scans are foreground only Scan options, including CBCentralManagerScanOptionAllowDuplicatesKey , have no effect Background Peripheral Mode Add bluetooth peripheral to UIBackgroundModes . In the background: Without this mode, published service contents are disabled while suspended The local name is not advertised Service UUIDs move to the overflow area and require explicit service scans State Restoration State restoration allows the system to re create your central or peripheral manager after your app is terminated and relaunched for a BLE event. Central Manager State Restoration Peripheral Manager State Restoration Common Mistakes Mistake Fix Scan/connect before .poweredOn Start BLE work from centralManagerDidUpdateState . Discovered peripheral is not retained Hold a strong reference through connection and discovery. Production scan passes nil services Filter by the service UUIDs the feature needs. Service discovery begins before didConnect Advance only from delegate callbacks and handle failure/disconnect paths. Writes ignore characteristic properties or payload limits Select the supported write type, respect maximumWriteValueLength , and gate .withoutResponse on canSendWriteWithoutResponse . Review Checklist [ ] NSBluetoothAlwaysUsageDescription added to Info.plist [ ] All BLE operations gated on centralManagerDidUpdateState returning .poweredOn [ ] Discovered peripherals retained with a strong reference [ ] Scanning uses specific service UUIDs (not nil ) in production [ ] CBPeripheralDelegate set before calling discoverServices [ ] Characteristic properties checked before read/write/notify [ ] Write payloads stay within maximumWriteValueLength(for:) [ ] .withoutResponse writes honor canSendWriteWithoutResponse [ ] Background mode ( bluetooth central or bluetooth peripheral ) added if needed [ ] State restoration identifier set if app needs relaunch on BLE event support [ ] willRestoreState delegate method implemented when using state restoration [ ] Scanning stopped after discovering the target peripheral [ ] Disconnection handled with optional automatic reconnect logic [ ] Write type matches characteristic properties ( .withResponse vs .withoutResponse ) References Extended patterns (reconnection strategies, data parsing, SwiftUI integration): [references/ble patterns.md](references/ble patterns.md) [Core Bluetooth framework](https://sosumi.ai/documentation/corebluetooth) [CBCentralManager](https://sosumi.ai/documentation/corebluetooth/cbcentralmanager) [CBPeripheral](https://sosumi.ai/documentation/corebluetooth/cbperipheral) [CBPeripheralManager](https://sosumi.ai/documentation/corebluetooth/cbperipheralmanager) [CBService](https://sosumi.ai/documentation/corebluetooth/cbservice) [CBCharacteristic](https://sosumi.ai/documentation/corebluetooth/cbcharacteristic) [CBUUID](https://sosumi.ai/documentation/corebluetooth/cbuuid) [CBCentralManagerDelegate](https://sosumi.ai/documentation/corebluetooth/cbcentralmanagerdelegate) [CBPeripheralDelegate](https://sosumi.ai/documentation/corebluetooth/cbperipheraldelegate) [NSBluetoothAlwaysUsageDescription](https://sosumi.ai/documentation/bundleresources/information property list/nsbluetoothalwaysusagedescription) [CBManagerAuthorization](https://sosumi.ai/documentation/corebluetooth/cbmanagerauthorization) [scanForPeripherals(withServices:options:)](https://sosumi.ai/documentation/corebluetooth/cbcentralmanager/scanforperipherals(withservices:options:)) [startAdvertising( :)](https://sosumi.ai/documentation/corebluetooth/cbperipheralmanager/startadvertising( :)) [writeValue( :for:type:)](https://sosumi.ai/documentation/corebluetooth/cbperipheral/writevalue( :for:type:)) [maximumWriteValueLength(for:)](https://sosumi.ai/documentation/corebluetooth/cbperipheral/maximumwritevaluelength(for:)) [canSendWriteWithoutResponse](https://sosumi.ai/documentation/corebluetooth/cbperipheral/cansendwritewithoutresponse) [Configuring background execution modes](https://sosumi.ai/documentation/xcode/configuring background execution modes)