Core Bluetooth on WatchOS
Ever thought about adding a Watch App to your product? Wondering how to make CoreBluetooth work on your Watch App? You’re in the right place! This tutorial is your go-to guide. In this post, we’ll take you step by step through the process of smoothly bringing in data from Bluetooth gadgets into your Apple Watch apps.
Discover how to harness the potential of Bluetooth devices to enhance your Apple Watch user experience. We’ll also provide insights into overcoming common challenges when dealing with Core Bluetooth on watchOS. Whether you’re a seasoned professional or a beginner, this tutorial simplifies the process for you.
Environments: XCode 15.0.1, iOS 17.0.3, WatchOS 10.1.1, Swift 5.
Set up project
Start by going to your project settings, then select File
> New Target
> Watch OS
> App
, and fill in the required fields. Once done, Xcode will seamlessly integrate a new watch app project into your existing workspace.
Bluetooth config
Essentially, all methods and Bluetooth events on WatchOS closely resemble those on iOS. If you already have a BluetoothManager
class that handles various Bluetooth functions, such as initiating scanning or connecting to a peripheral, and manages Bluetooth delegates, you’re in good shape.
1 | class BluetoothManager : NSObject, CBCentralManagerDelegate { |
To save time and avoid duplicating code, you can easily share the file containing the BluetoothManager
class with both your iOS and watch app targets. With this setup, you can seamlessly use the BluetoothManager
class in your watch app just like you would in your iOS app.
1 | struct ContentView: View { |
Important notes
- To test your project’s Bluetooth functionality, it’s essential to run it on a real Apple Watch since the simulator doesn’t support Bluetooth.
- Keep in mind that the connection time on the Apple Watch can be influenced by the device’s battery status, even if low power mode is not enabled.
- Ensure that you manually add the necessary capability to the Watch App plist file. This step is crucial; otherwise, your app won’t be able to scan, connect, or execute any Bluetooth commands when it’s in the background.
1 | <key>UIBackgroundModes</key> |
- Unlike Bluetooth on iOS, where you can leverage State preservation and restoration to awaken the app if it has been terminated by the system due to Bluetooth events (see Best practice: Best practice: How to deal with Bluetooth Low Energy in background), it’s important to note that there is no equivalent State preservation and restoration mechanism on watchOS.
- The connection time on iOS and WatchOS is quite equal. I measured the Connect API by performing 200 calls (same devices, same testing environment). The average on iOS is approximately 0.69 seconds, while on WatchOS, it is 0.78 seconds.
Conclusion
In a nutshell, by learning how to connect your Apple Watch to Bluetooth devices, you’ve boosted your watch’s features. This tutorial has guided you through using Core Bluetooth on watchOS, handling common problems along the way. Whether you’re a pro or a beginner, we’ve broken it down for you. Now, your Watch App not only works well but also impresses users. As you keep making apps, use these skills to create cool and smooth experiences. Happy coding!
References
[1] WWDC 2021
[2] WWDC 2022
[3] Core Bluetooth in watchOS Tutorial