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
Copy file name to clipboardExpand all lines: Sources/iOS-BLE-Library-Mock/CentralManager/CentralManager.swift
+18-8
Original file line number
Diff line number
Diff line change
@@ -18,9 +18,10 @@ extension CentralManager {
18
18
publicvarlocalizedDescription:String{
19
19
switchself{
20
20
case.wrongManager:
21
-
return"Incorrect manager instance provided."
21
+
return
22
+
"Incorrect manager instance provided. Delegate must be of type ReactiveCentralManagerDelegate."
22
23
case.badState(let state):
23
-
return"Bad state: \(state)"
24
+
return"Bad state: \(state)."
24
25
case.unknownError:
25
26
return"An unknown error occurred."
26
27
}
@@ -53,14 +54,18 @@ private class Observer: NSObject {
53
54
}
54
55
}
55
56
56
-
/// A custom Central Manager class that extends the functionality of the standard CBCentralManager.
57
-
/// This class brings a reactive approach and is based on the Swift Combine framework.
57
+
/// A Custom Central Manager class.
58
+
///
59
+
/// It wraps the standard CBCentralManager and has similar API. However, instead of using delegate, it uses publishers, thus bringing the reactive programming paradigm to the CoreBluetooth framework.
Since it's not recommended to override the `CBCentralManager`'s methods, ``CentralManager`` is merely a wrapper around `CBCentralManager` with an instance of it inside.
6
+
7
+
The new instance of `CBCentralManager` can be created during initialization using ``init(centralManagerDelegate:queue:)``, or an existing instance can be passed using ``init(centralManager:)``.
8
+
9
+
If you pass a central manager inside ``init(centralManager:)``, it should already have a delegate set. The delegate should be an instance of ``ReactiveCentralManagerDelegate``; otherwise, an error will be thrown.
10
+
11
+
### Connection
12
+
13
+
Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
14
+
The returned publisher will emit the connected peripheral or an error if the connection fails.
15
+
The publisher will not complete until the peripheral is disconnected.
16
+
If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
17
+
18
+
> The publisher returned by ``CentralManager/connect(_:options:)`` is a `ConnectablePublisher`. Therefore, you need to call `connect()` or `autoconnect()` to initiate the connection process.
19
+
20
+
```swift
21
+
centralManager.connect(peripheral)
22
+
.autoconnect()
23
+
.sink { completion in
24
+
switch completion {
25
+
case .finished:
26
+
print("Peripheral disconnected successfully")
27
+
case .failure(let error):
28
+
print("Error: \(error)")
29
+
}
30
+
} receiveValue: { peripheral in
31
+
print("Peripheral connected: \(peripheral)")
32
+
}
33
+
.store(in: &cancellables)
34
+
```
35
+
36
+
### Channels
37
+
38
+
Channels are used to pass through data from the `CBCentralManagerDelegate` methods.
39
+
You can consider them as a reactive version of the `CBCentralManagerDelegate` methods.
40
+
41
+
In most cases, you will not need to use them directly, as `centralManager`'s methods return proper publishers. However, if you need to handle the data differently (e.g., log all the events), you can subscribe to the channels directly.
42
+
43
+
All channels have `Never` as their failure type because they never fail. Some channels, like `CentralManager/connectedPeripheralChannel` or `CentralManager/disconnectedPeripheralsChannel`, send tuples with the peripheral and the error, allowing you to handle errors if needed. Despite this, the failure type remains `Never`, so it will not complete even if an error occurs during the connection or disconnection of the peripheral.
`ReactiveCentralManagerDelegate` is a class that implements the `CBCentralManagerDelegate` and is an essential part of the ``CentralManager`` class.
6
+
7
+
It brings a reactive programming approach, utilizing Combine publishers to seamlessly handle Bluetooth events and data.
8
+
This class allows to monitor and respond to events such as peripheral connection, disconnection, and scanning for peripherals.
9
+
10
+
It has all needed publishers that are used for handling Bluetooth events and data.
11
+
12
+
## Override
13
+
14
+
It's possible to override the default implementation of the `ReactiveCentralManagerDelegate` by creating a new class that inherits from `ReactiveCentralManagerDelegate` and overriding the needed methods.
15
+
16
+
However, it's important to call the `super` implementation of the method, otherwise it will break the `CentralManager` functionality.
Copy file name to clipboardExpand all lines: Sources/iOS-BLE-Library-Mock/Utilities/Publishers/Publishers+Bluetooth.swift
+20
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,26 @@ extension Publisher {
17
17
}
18
18
19
19
extensionPublishers{
20
+
21
+
/**
22
+
A publisher that is used for most of the Bluetooth operations.
23
+
24
+
# Overview
25
+
This publisher conforms to the `ConnectablePublisher` protocol because most of the Bluetooth operations have to be set up before they can be used.
26
+
27
+
It means that the publisher will not emit any values until it is connected. The connection is established by calling the `connect()` or `autoconnect()` methods.
28
+
To learn more about the `ConnectablePublisher` protocol, see [Apple's documentation](https://developer.apple.com/documentation/combine/connectablepublisher).
29
+
30
+
```swift
31
+
let publisher = centralManager.scanForPeripherals(withServices: nil)
0 commit comments