This example project demonstrates how to use the bluetooth-law-energy-swift package to manage Bluetooth Low Energy (BLE) operations within a SwiftUI application. Bluetooth Low Energy Service package
The example code provided for Bluetooth Low Energy (BLE) functionality utilizes CoreBluetooth, which requires actual Bluetooth hardware to operate like iPhone etc. Simulators does not support Bluetooth functionalities as it lacks the necessary hardware capabilities.
De-initialization of a BLE manager, may not be instantaneous and can vary in duration, depending largely on ongoing BLE tasks and the specifics of Swift’s concurrency model. Tasks already in progress must be completed before resources are freed, as ongoing operations cannot be arbitrarily canceled once they have begun. This behavior is influenced not only by the internal mechanics of the BLE manager but also by the concurrency model where certain tasks, even if marked for cancellation, continue until their current actions are resolved.
When your macOS application is not authorized to use Bluetooth, you will see a window directing you to System Preferences, Security & Privacy, and Bluetooth. To grant access, follow these steps:
- Open Xcode.
- Go to the menu bar and select
Xcode
>Preferences...
. - In the Preferences window, select the
Locations
tab. - Note the path to the Derived Data directory.
- Open Finder and navigate to the Derived Data directory:
~/Library/Developer/Xcode/DerivedData/
- Inside the Derived Data directory, locate the folder for your project. It will have a name that includes your project's name and a random identifier (e.g.,
Your_Project_Name-abcdefgh
). - Navigate to
Build/Products/Debug
(orBuild/Products/Release
if you built a release version). - You will find the compiled macOS executable file in this folder.
- Open System Preferences on your Mac.
- Go to
Security & Privacy
. - Select the
Privacy
tab. - Scroll down to find and select
Bluetooth
. - Click the lock icon to make changes and enter your admin password.
- Click the
+
button to add a new application. - Navigate to the location of your compiled macOS executable file (as described above) and select it.
- Ensure that your application is checked in the list to allow Bluetooth access.
To enable Bluetooth access for your macOS application, you need to modify the entitlements file. Here are the steps to add the required entitlement:
- Open your project in Xcode.
- Select your project in the Project Navigator.
- Select your app target.
- Go to the
Signing & Capabilities
tab. - Click the
+ Capability
button. - Add the
App Sandbox
capability if it is not already added. - Under the
App Sandbox
settings, ensure that theHardware
>Bluetooth
option is checked.
-
Open your entitlements file (usually named
YourApp.entitlements
). -
Ensure it includes the
com.apple.security.device.bluetooth
key. It should look like this:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>com.apple.security.app-sandbox</key> <true/> <key>com.apple.security.device.bluetooth</key> <true/> </dict> </plist>
This configuration ensures that your application has the necessary permissions to use Bluetooth on macOS.