Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Demo/ReliaBLE Demo/ReliaBLE Demo/Central/CentralView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,25 @@ struct CentralView: View {
}
.buttonStyle(.bordered)
} else if case BluetoothState.ready = viewModel.currentState {
TextField("Enter service UUIDs (comma-separated)", text: $viewModel.servicesInput)
TextField("Service UUIDs (comma-separated; required for background)", text: $viewModel.servicesInput)
.textFieldStyle(.roundedBorder)
.padding()
.padding(.horizontal)

Text("Background discovery needs a non-empty service filter. Connect with Auto Reconnect on, then background/force-quit — restored connections reappear on relaunch.")
.font(.caption)
.foregroundStyle(.secondary)
.padding(.horizontal)

Button("Start Scanning") {
viewModel.startScanning()
}
.buttonStyle(.bordered)
} else if case BluetoothState.scanning = viewModel.currentState {
Text("Scanning (continues in background when a service filter is set)")
.font(.caption)
.foregroundStyle(.secondary)
.padding(.horizontal)

Button("Stop Scanning") {
viewModel.stopScanning()
}
Expand Down Expand Up @@ -181,7 +191,15 @@ struct CentralView: View {
reliaBLE: reliaBLE
)
} label: {
Text("\(device.name ?? "Unknown")")
HStack {
Text("\(device.name ?? "Unknown")")
Spacer()
if let state = viewModel.connectionStates[device.id] {
Text(state.description)
.font(.caption)
.foregroundStyle(state.color)
}
}
}
}
.onDelete { offsets in
Expand Down Expand Up @@ -260,6 +278,13 @@ private struct DeviceDetailView: View {

Toggle("Auto Reconnect:", isOn: $autoReconnect)
.disabled(connectionState.map { !$0.canEditAutoReconnect } ?? false)

if isActive {
Text("Standing connection: leave Auto Reconnect on, background or force-quit the app, then relaunch — restored state shows here via connectionStateChanges.")
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
}
.padding()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import ReliaBLE

@Observable class CentralViewModel {
var currentState: BluetoothState = .unknown
var servicesInput = ""
/// Defaults to the Demo peripheral service UUID so background scans have a required filter.
var servicesInput = "12345678-90AB-CDEF-1234-567890ABCDEF"
var connectionStates: [String: ConnectionState] = [:]

private var deviceStore: DeviceStoreActor?
Expand Down
6 changes: 4 additions & 2 deletions Demo/ReliaBLE Demo/ReliaBLE Demo/ReliaBLE_DemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ struct ReliaBLE_DemoApp: App {
config.logWriters = [OSLogWriter(subsystem: "com.five3apps.relia-ble-demo", category: "BLE")]
config.logQueue = DispatchQueue(label: "com.five3apps.relia-ble-demo.logging", qos: .utility)
config.loggingEnabled = true

// Stable across launches so CoreBluetooth can restore scans/connections after termination.
config.restoreIdentifier = "com.five3apps.relia-ble-demo.central"

let defaults = UserDefaults.standard
var reconnectPolicy = ReconnectPolicy()
reconnectPolicy.maxAttempts = defaults.object(forKey: "reconnectPolicy.maxAttempts") as? Int ?? 5
reconnectPolicy.initialDelay = defaults.object(forKey: "reconnectPolicy.initialDelay") as? Double ?? 1.0
reconnectPolicy.maxDelay = defaults.object(forKey: "reconnectPolicy.maxDelay") as? Double ?? 30.0
reconnectPolicy.jitter = defaults.object(forKey: "reconnectPolicy.jitter") as? Double ?? 0.2
config.reconnectPolicy = reconnectPolicy

return ReliaBLEManager(config: config)
}()

Expand Down
6 changes: 5 additions & 1 deletion Demo/ReliaBLE Demo/ReliaBLE-Demo-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<plist version="1.0">
<dict>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app uses Bluetooth to advertise as a peripheral.</string>
<string>This app uses Bluetooth to scan for and connect to nearby peripherals, and to advertise as a peripheral.</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
</array>
</dict>
</plist>
Loading
Loading