Interacting with Bluetooth peripherals during background app refresh
At a glance
| Item | Summary |
|---|---|
| Purpose | Keep your complications up-to-date by reading values from a Bluetooth peripheral while your app is running in the background. |
| App architecture | A Swift sample bundle with entry-bearing project variants BARBluetooth WatchKit Extension, BARBluetooth, each leading to CoreBluetooth / SwiftUI APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks, SwiftUI environment injection, Publisher-backed observable state |
| Project style | 11 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── BARBluetooth WatchKit Extension/
│ ├── BARBluetoothApp.swift
│ ├── ComplicationController.swift
│ ├── ExtensionDelegate.swift
│ ├── NotificationHandler.swift
│ ├── RootView.swift
│ ├── BluetoothReceiver.swift
│ └── Info.plist
├── BARBluetooth/
│ ├── SamplePeripheralApp.swift
│ ├── ApplicationDelegate.swift
│ ├── RootView.swift
│ └── BluetoothSender.swift
└── shared/
└── BluetoothConstants.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 5 project/configuration file(s) and 14 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["BARBluetooth WatchKit Extension"]
V2["BARBluetooth"]
Boundary["CoreBluetooth / SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
BARBluetooth WatchKit Extension/BARBluetoothApp.swift:10 — architecture anchor
@main
struct BARBluetoothApp: App {
// ...
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
BARBluetoothApp *-- String : name
BARBluetoothApp o-- ExtensionDelegate : delegate
SamplePeripheralApp *-- String : name
SamplePeripheralApp o-- ApplicationDelegate : delegate
Ownership evidence
BARBluetooth WatchKit Extension/BARBluetoothApp.swift:13 — stored dependency or nearest verified ownership anchor
@main
struct BARBluetoothApp: App {
// ...
static let name: String = "BARBluetooth"
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
BARBluetoothApp |
String (name) |
owns value state | Initialized by the owner; the binding is immutable |
BARBluetoothApp |
ExtensionDelegate (delegate) |
stores or receives | App/module collaborators |
SamplePeripheralApp |
String (name) |
owns value state | Initialized by the owner; the binding is immutable |
SamplePeripheralApp |
ApplicationDelegate (delegate) |
stores or receives | App/module collaborators |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Class and protocol design
BARBluetooth WatchKit Extension/BluetoothReceiver.swift:11 — representative type boundary
protocol BluetoothReceiverDelegate: AnyObject {
func didReceiveData(_ message: Data) -> Int
func didCompleteDisconnection(from peripheral: CBPeripheral, mustDisconnect: Bool)
func didFailWithError(_ error: BluetoothReceiverError)
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
BARBluetoothApp |
Application entry and top-level composition | App |
SamplePeripheralApp |
Application entry and top-level composition | App |
BluetoothReceiverDelegate |
Defines a capability or collaboration contract | AnyObject |
BluetoothSenderDelegate |
Defines a capability or collaboration contract | AnyObject |
ApplicationDelegate |
Receives callback-driven events | NSObject, UIApplicationDelegate, BluetoothSenderDelegate |
RootView |
User-interface presentation and input forwarding | View |
ComplicationController |
View lifecycle, callbacks, and feature coordination | NSObject, CLKComplicationDataSource |
ExtensionDelegate |
Receives callback-driven events | NSObject, WKExtensionDelegate, BluetoothReceiverDelegate |
NotificationHandler |
Handles callbacks or feature events | NSObject, UNUserNotificationCenterDelegate |
RootView |
User-interface presentation and input forwarding | View |
The source explicitly defines local protocol relationships: ExtensionDelegate → BluetoothReceiverDelegate, ApplicationDelegate → BluetoothSenderDelegate.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
logger (BARBluetooth WatchKit Extension/BluetoothReceiver.swift:28) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
serviceUUID (BARBluetooth WatchKit Extension/BluetoothReceiver.swift:39) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
characteristicUUID (BARBluetooth WatchKit Extension/BluetoothReceiver.swift:41) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
connectedPeripheral (BARBluetooth WatchKit Extension/BluetoothReceiver.swift:43) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
Reference code
BARBluetooth WatchKit Extension/BluetoothReceiver.swift:28 — representative boundary
private var logger = Logger(
subsystem: BARBluetoothApp.name,
category: String(describing: BluetoothReceiver.self)
)
var notificationHandler = ExtensionDelegate.instance.notificationHandlerSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | BARBluetoothApp, SamplePeripheralApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | ComplicationController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | ApplicationDelegate, BluetoothReceiverDelegate, BluetoothSenderDelegate, ExtensionDelegate |
The source’s Delegate suffix makes this role explicit. |
| Handles callbacks or feature events | NotificationHandler |
The source’s Handler suffix makes this role explicit. |
| User-interface presentation and input forwarding | RootView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | BARBluetooth WatchKit Extension/ComplicationController.swift:11 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | BARBluetooth WatchKit Extension/ExtensionDelegate.swift:12 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | BARBluetooth WatchKit Extension/BluetoothReceiver.swift:26 |
Callback protocols invert event delivery back into the sample’s owner. |
| SwiftUI environment injection | BARBluetooth WatchKit Extension/RootView.swift:14 |
The environment supplies state or a capability without threading it through every initializer. |
| Publisher-backed observable state | BARBluetooth WatchKit Extension/BluetoothReceiver.swift:43 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: BARBluetoothApp, SamplePeripheralApp; Controller: ComplicationController; Delegate: ApplicationDelegate, BluetoothReceiverDelegate, BluetoothSenderDelegate, ExtensionDelegate; Handler: NotificationHandler; View: RootView.
- Protocols:
BluetoothReceiverDelegate,BluetoothSenderDelegate. - Methods:
getDataFor,updateAllActiveComplications,getComplicationDescriptors,handleSharedComplicationDescriptors,getTimelineEndDate,getPrivacyBehavior,getCurrentTimelineEntry,getTimelineEntries. - Files:
BARBluetooth WatchKit Extension/BARBluetoothApp.swift,BARBluetooth/SamplePeripheralApp.swift,BARBluetooth/ApplicationDelegate.swift,BARBluetooth/RootView.swift,BARBluetooth WatchKit Extension/ComplicationController.swift,BARBluetooth WatchKit Extension/ExtensionDelegate.swift.
Architecture takeaways
BARBluetoothAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches CoreBluetooth, SwiftUI, UserNotifications, ClockKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
BARBluetooth WatchKit Extension/BARBluetoothApp.swift |
BARBluetoothApp |
BARBluetooth/SamplePeripheralApp.swift |
SamplePeripheralApp |
BARBluetooth/ApplicationDelegate.swift |
ApplicationDelegate |
BARBluetooth/RootView.swift |
RootView, RootView_Previews |
BARBluetooth WatchKit Extension/ComplicationController.swift |
ComplicationController |
BARBluetooth WatchKit Extension/ExtensionDelegate.swift |
ExtensionDelegate |
BARBluetooth WatchKit Extension/NotificationHandler.swift |
NotificationHandler |
BARBluetooth WatchKit Extension/RootView.swift |
RootView |
BARBluetooth WatchKit Extension/BluetoothReceiver.swift |
BluetoothReceiverDelegate, BluetoothReceiverError, BluetoothReceiver |
BARBluetooth/BluetoothSender.swift |
BluetoothSenderDelegate, BluetoothSender |