BlueCapKit
BlueCapKit is a powerful and flexible framework for working with Bluetooth Low Energy (BLE) devices on iOS. It provides a simple and intuitive interface to discover, connect, configure, and communicate with BLE peripherals.
Features
- Discover nearby BLE peripherals
- Connect and disconnect from peripherals
- Read and write characteristic values
- Subscribe to characteristic value notifications
- Configure and manage BLE services
- Retrieve peripheral information like name, RSSI, UUIDs, etc.
- Perform scans with custom filters
- Powerful error handling and connection management
Requirements
To use BlueCapKit in your iOS project, you need:
- iOS 10.0 or later
- Xcode 12 or later
- Swift 5
Installation
To integrate BlueCapKit into your project, you can use Swift Package Manager or CocoaPods.
Swift Package Manager
Follow these steps to add BlueCapKit as a dependency in your project:
- In Xcode, go to File > Swift Packages > Add Package Dependency.
- Enter the repository URL: https://github.com/bluecapio/bluecapkit
- Select the version or branch you want to use.
- Click Next and follow the prompts to complete the installation.
CocoaPods
To install BlueCapKit with CocoaPods, add the following line to your Podfile:
pod 'BlueCapKit'
Then, run the command pod install
.
Usage
Discovering Peripherals
BlueCapKit provides a simple way to discover nearby BLE peripherals using the PeripheralScanner
class. Here’s how you can get started:
- Create an instance of
PeripheralScanner
. - Implement the necessary delegate methods to handle scan results.
- Start the scan by calling
startScan
method. - Process the discovered peripherals in the delegate methods.
- Stop the scan when done by calling
stopScan
method.
Example:
import BlueCapKit
class MyPeripheralScannerDelegate: PeripheralScannerDelegate {
func didDiscoverPeripheral(_ peripheral: Peripheral, advertisementData: AdvertisementData, rssi: Int) {
// Handle discovered peripheral
}
func didUpdateState(_ state: PeripheralScannerState) {
// Handle state updates
}
// Implement other necessary delegate methods
}
let scanner = PeripheralScanner()
let delegate = MyPeripheralScannerDelegate()
scanner.delegate = delegate
scanner.startScan()
// Handle other tasks or wait for scan to complete
scanner.stopScan()
Connecting to Peripherals
Once you have discovered a peripheral, you can establish a connection using the Peripheral
class. Here’s an example of how to connect to a peripheral:
let peripheral: Peripheral = // Retrieved peripheral from scan results
peripheral.connect { result in
switch result {
case .success:
// Connection successful
// Perform necessary operations with peripheral
case .failure(let error):
// Connection failed
// Handle error
}
}
// Handle other tasks or wait for connection to complete
Communicating with Peripherals
Once connected, you can read and write characteristic values, subscribe to notifications, and perform other operations with the peripheral. Here’s an example of reading a characteristic value:
let characteristic: Characteristic = // Retrieved characteristic from connected peripheral
characteristic.readValue { result in
switch result {
case .success(let data):
// Value read successfully
// Process the data
case .failure(let error):
// Read failed
// Handle error
}
}
// Handle other tasks or wait for read operation to complete
Advanced Configuration
BlueCapKit offers advanced configuration options to fine-tune your BLE interactions. These options include enabling background execution, adjusting connection timeouts, managing maximum read/write lengths, and more. Refer to the official documentation for detailed information on advanced configuration.
Resources
For more information and detailed documentation, visit the official BlueCapKit repository on GitHub.
You can also find example projects, guides, and API references on the CocoaDocs page.
Get started with BlueCapKit and unlock the power of Bluetooth Low Energy on iOS!