Skip to content

Commit

Permalink
updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKibish committed Oct 6, 2023
1 parent ad66f34 commit 04ca5e6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 54 deletions.
20 changes: 20 additions & 0 deletions Sources/iOS-BLE-Library-Mock/CentralManager/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ extension CentralManager {
/// If the peripheral was disconnected successfully, the publisher finishes without error.
/// If the connection was unsuccessful or disconnection returns an error (e.g., peripheral disconnected unexpectedly),
/// the publisher finishes with an error.
///
/// Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
/// The returned publisher will emit the connected peripheral or an error if the connection fails.
/// The publisher will not complete until the peripheral is disconnected.
/// If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
///
/// ```swift
/// centralManager.connect(peripheral)
/// .sink { completion in
/// switch completion {
/// case .finished:
/// print("Peripheral disconnected successfully")
/// case .failure(let error):
/// print("Error: \(error)")
/// }
/// } receiveValue: { peripheral in
/// print("Peripheral connected: \(peripheral)")
/// }
/// .store(in: &cancellables)
/// ```
public func connect(_ peripheral: CBPeripheral, options: [String: Any]? = nil)
-> AnyPublisher<CBPeripheral, Error>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,12 @@

### Create a Central Manager

Since it's not recommended to override the `CBCentralManager`'s methods, ``CentralManager`` is merely a wrapper around `CBCentralManager` with an instance of it inside.
``CentralManager`` is merely a wrapper around `CBCentralManager` with an instance of it inside.

The new instance of `CBCentralManager` can be created during initialization using ``init(centralManagerDelegate:queue:)``, or an existing instance can be passed using ``init(centralManager:)``.
The new instance of `CBCentralManager` can be created during initialization using ``init(centralManagerDelegate:queue:options:)``, or an existing instance can be passed using ``init(centralManager:)``.

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.

### Connection

Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
The returned publisher will emit the connected peripheral or an error if the connection fails.
The publisher will not complete until the peripheral is disconnected.
If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.

> The publisher returned by ``CentralManager/connect(_:options:)`` is a `ConnectablePublisher`. Therefore, you need to call `connect()` or `autoconnect()` to initiate the connection process.
```swift
centralManager.connect(peripheral)
.autoconnect()
.sink { completion in
switch completion {
case .finished:
print("Peripheral disconnected successfully")
case .failure(let error):
print("Error: \(error)")
}
} receiveValue: { peripheral in
print("Peripheral connected: \(peripheral)")
}
.store(in: &cancellables)
```

### Channels

Channels are used to pass through data from the `CBCentralManagerDelegate` methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ``iOS_BLE_Library/CentralManager/connect(_:options:)``

## See Also

- ``CentralManager/connectedPeripheralChannel``
- ``CentralManager/disconnectedPeripheralsChannel``

20 changes: 20 additions & 0 deletions Sources/iOS-BLE-Library/CentralManager/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ extension CentralManager {
/// If the peripheral was disconnected successfully, the publisher finishes without error.
/// If the connection was unsuccessful or disconnection returns an error (e.g., peripheral disconnected unexpectedly),
/// the publisher finishes with an error.
///
/// Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
/// The returned publisher will emit the connected peripheral or an error if the connection fails.
/// The publisher will not complete until the peripheral is disconnected.
/// If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
///
/// ```swift
/// centralManager.connect(peripheral)
/// .sink { completion in
/// switch completion {
/// case .finished:
/// print("Peripheral disconnected successfully")
/// case .failure(let error):
/// print("Error: \(error)")
/// }
/// } receiveValue: { peripheral in
/// print("Peripheral connected: \(peripheral)")
/// }
/// .store(in: &cancellables)
/// ```
public func connect(_ peripheral: CBPeripheral, options: [String: Any]? = nil)
-> AnyPublisher<CBPeripheral, Error>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,12 @@

### Create a Central Manager

Since it's not recommended to override the `CBCentralManager`'s methods, ``CentralManager`` is merely a wrapper around `CBCentralManager` with an instance of it inside.
``CentralManager`` is merely a wrapper around `CBCentralManager` with an instance of it inside.

The new instance of `CBCentralManager` can be created during initialization using ``init(centralManagerDelegate:queue:)``, or an existing instance can be passed using ``init(centralManager:)``.
The new instance of `CBCentralManager` can be created during initialization using ``init(centralManagerDelegate:queue:options:)``, or an existing instance can be passed using ``init(centralManager:)``.

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.

### Connection

Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
The returned publisher will emit the connected peripheral or an error if the connection fails.
The publisher will not complete until the peripheral is disconnected.
If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.

> The publisher returned by ``CentralManager/connect(_:options:)`` is a `ConnectablePublisher`. Therefore, you need to call `connect()` or `autoconnect()` to initiate the connection process.
```swift
centralManager.connect(peripheral)
.autoconnect()
.sink { completion in
switch completion {
case .finished:
print("Peripheral disconnected successfully")
case .failure(let error):
print("Error: \(error)")
}
} receiveValue: { peripheral in
print("Peripheral connected: \(peripheral)")
}
.store(in: &cancellables)
```

### Channels

Channels are used to pass through data from the `CBCentralManagerDelegate` methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ``iOS_BLE_Library/CentralManager/connect(_:options:)``

## See Also

- ``CentralManager/connectedPeripheralChannel``
- ``CentralManager/disconnectedPeripheralsChannel``

0 comments on commit 04ca5e6

Please sign in to comment.