You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let value =tryawait peripheral.readValue(for: .someCharacteristic)
84
45
85
-
> **Note**
86
-
Force-unwrapping is only used for brevity and is not recommended.
46
+
central.cancelPeripheralConnection(peripheral)
47
+
```
87
48
88
49
#### Callbacks
89
50
90
-
```swift
91
-
// Most of the stock CoreBluetooth methods have an additional new signature that takes a completionHandler
51
+
Stock CoreBluetooth methods now also have an additional overload that takes a completionHandler for projects not using Swift Concurrency.
92
52
53
+
```swift
93
54
central.connect(peripheral) { result in
94
55
if result == .failure(let error) {
95
56
// Issue connecting
@@ -98,14 +59,38 @@ central.connect(peripheral) { result in
98
59
99
60
// Connected!
100
61
}
62
+
```
63
+
> Methods often now have 3 overloads. One marked `async`, one with a `completionHandler`, and the original CoreBluetooth verision. Meaning you can choose whichever is most convienient at the time.
64
+
65
+
#### Stream discovered peripherals
101
66
67
+
Some operations (like scanning) conform to `AsyncStream`, meaning you can use for-await-in loops to iterate over new items.
Characteristics can be staticly defined on the stock `Characteristic` type, which removes the burden of keeping track of `CBCharacteristic` instances around your app.
105
78
106
79
```swift
107
-
// Peristent tasks return a `CancellableTask` that needs to be cancelled when you're done
Peristent tasks return a `CancellableTask` that needs to be cancelled when you're done.
92
+
93
+
```swift
109
94
let task = central.scanForPeripherals { peripheral in
110
95
print("Discovered:", peripheral.name??"Unknown")
111
96
}
@@ -116,6 +101,24 @@ task.cancel()
116
101
> **Note**
117
102
Calling `central.stopScan()` will also cancel any peripheral scanning tasks
118
103
104
+
#### Migrate existing projects
105
+
106
+
Existing projects that already use `CoreBluetooth` can immediately get started by typealiasing the stock types. Afterwards, you can adopt async API's at your own pace.
107
+
108
+
```swift
109
+
importCoreBluetooth
110
+
importSwiftBluetooth// Add this
111
+
112
+
// Override existing CoreBluetooth classes to use SwiftBluetooth
0 commit comments