-
Connect Bluetooth devices to Apple Watch
Discover how you can integrate data from Bluetooth accessories into Apple Watch apps and complications. Bluetooth devices can provide medical data, sports stats, and more to Apple Watch, and help people get more out of your software in the process. We'll show you how to connect to these devices during Background App Refresh to display the most up-to-date information in your Apple Watch complications, provide an overview of Core Bluetooth on watchOS, and explore best practices for Bluetooth accessory design.
Recursos
- Background execution
- Interacting with Bluetooth peripherals during background app refresh
- Core Bluetooth
Vídeos relacionados
WWDC22
WWDC21
-
Buscar neste vídeo...
-
-
7:35 - didDiscoverPeripheral
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber ) { // Add to an array of discovered peripherals, // then connect to the peripheral. central.connect(peripheral, options: nil) } -
7:55 - handleBackgroundTasks
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) { for task in backgroundTasks { if let refreshTask = task as? WKApplicationRefreshBackgroundTask { // Insert your code to start background work here. central.connect(peripheral, options: nil) refreshTask.expirationHandler = { // Insert your code to cancel existing work here. if let peripheral = self.bluetoothReceiver.connectedPeripheral { self.central.cancelPeripheralConnection(peripheral) } refreshTask.setTaskCompletedWithSnapshot(false) } } } } -
8:51 - didDisconnectPeripheral
// If the app gets woken up to handle a background refresh task, this method will be called // if a peripheral was disconnected when the app had previously transitioned to the // background. func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { connectedPeripheral = nil delegate?.didCompleteDisconnection(from: peripheral) } -
9:08 - didCompleteDisconnection
// In your WatchKit Extension delegate: func didCompleteDisconnection(from peripheral: CBPeripheral) { if let refreshTask = currentRefreshTask { task.setTaskCompletedWithSnapshot(false) currentRefreshTask = nil } }
-