Skip to content

Commit dc57b83

Browse files
committed
add the public getPeripheral function to peripheral
1 parent 27b2e1d commit dc57b83

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ for await rssi in peripheral.rssi.stream {
377377
// note that the rssi is only updated when you call readRSSI, so you would need to call this for the loop to itterate
378378
```
379379

380+
381+
## Accessing Underlying CBMPeripheral
382+
383+
Sometimes you need to access CoreBluetooth functionality that hasn't been wrapped by this library yet. The `withCBPeripheral` method provides safe access to the underlying `CBMPeripheral` instance:
384+
385+
```swift
386+
// Get maximum write length for a peripheral
387+
let maxLength = await peripheral.withCBPeripheral { cbPeripheral in
388+
cbPeripheral.maximumWriteValueLength(for: .withoutResponse)
389+
}
390+
391+
// Or using shorthand closure syntax
392+
let maxLength = await peripheral.withCBPeripheral {
393+
$0.maximumWriteValueLength(for: .withoutResponse)
394+
}
395+
396+
// You can also assign to external variables
397+
var writeLength: Int!
398+
await peripheral.withCBPeripheral { cbPeripheral in
399+
writeLength = cbPeripheral.maximumWriteValueLength(for: .withoutResponse)
400+
}
401+
```
402+
403+
This is useful when you need CoreBluetooth features that aren't yet implemented in this library.
404+
380405
## Running Tests
381406

382407
```
@@ -385,4 +410,4 @@ swift test --no-parallel
385410

386411
## Building Documentation
387412

388-
check build.sh
413+
check build.sh

Sources/AsyncCoreBluetooth/Peripheral.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,43 @@ public actor Peripheral {
137137
/// Avoid using this if you can. It's been left public in case this library missed some functionality
138138
/// that is only available in the underlying `CBMPeripheral`.
139139
public private(set) var cbPeripheral: CBMPeripheral
140+
141+
/// Provides safe access to the underlying CBMPeripheral instance through a closure.
142+
///
143+
/// Avoid using this if you can.
144+
///
145+
/// This method allows you to access functionality from the underlying `CBMPeripheral`
146+
/// that may not be directly exposed by this Peripheral wrapper. The closure receives
147+
/// the `CBMPeripheral` instance and can return any value.
148+
///
149+
/// Use this method when you need to access Core Bluetooth functionality that hasn't
150+
/// been wrapped by this library, such as getting write value lengths or other
151+
/// peripheral-specific properties.
152+
///
153+
/// Example Usage:
154+
/// ```swift
155+
/// // Get maximum write length and assign to variable
156+
/// var maxLength: Int!
157+
/// await peripheral.getPeripheral { cbPeripheral in
158+
/// maxLength = cbPeripheral.maximumWriteValueLength(for: .withoutResponse)
159+
/// }
160+
///
161+
/// // Get maximum write length with return value
162+
/// let maxLength = await peripheral.getPeripheral { cbPeripheral in
163+
/// cbPeripheral.maximumWriteValueLength(for: .withoutResponse)
164+
/// }
165+
///
166+
/// // Shorthand closure syntax
167+
/// let maxLength = await peripheral.getPeripheral {
168+
/// $0.maximumWriteValueLength(for: .withoutResponse)
169+
/// }
170+
/// ```
171+
///
172+
/// - Parameter fn: A closure that takes the underlying `CBMPeripheral` and returns a value of type `T`.
173+
/// - Returns: The value returned by the closure.
174+
public func withCBPeripheral<T>(fn: (CBMPeripheral) -> T) -> T {
175+
return fn(cbPeripheral)
176+
}
140177

141178

142179
/// The unique identifier associated with the peripheral.

0 commit comments

Comments
 (0)