@@ -4,7 +4,9 @@ import Combine
44import os
55
66@MainActor
7+ /// Represents blemanager.
78final class BLEManager : NSObject , ObservableObject {
9+ /// Represents status.
810 enum Status : String {
911 case idle
1012 case bluetoothOff
@@ -30,6 +32,7 @@ final class BLEManager: NSObject, ObservableObject {
3032 /// Pełny hex + diff bajtów w konsoli Xcode i w devRawLog (szukaj przycisku).
3133 @Published public var debugRXBytes = false
3234
35+ /// Stores `byteProbe` used by this scope.
3336 public let byteProbe = BLEByteProbe ( )
3437
3538 /// Domyślnie wyłączone — flow jak w Żappce: user wybiera urządzenie z listy.
@@ -42,6 +45,7 @@ final class BLEManager: NSObject, ObservableObject {
4245 private var didAutoConnectThisScan = false
4346
4447 private let rxBytesSubject = PassthroughSubject < [ UInt8 ] , Never > ( )
48+ /// Stores `rxBytes` used by this scope.
4549 var rxBytes : AnyPublisher < [ UInt8 ] , Never > { rxBytesSubject. eraseToAnyPublisher ( ) }
4650
4751 private let log = Logger ( subsystem: " com.koderteam.gametriki " , category: " BLE " )
@@ -51,12 +55,18 @@ final class BLEManager: NSObject, ObservableObject {
5155 central = CBCentralManager ( delegate: self , queue: nil )
5256 }
5357
58+ /// Stores `isScanning` used by this scope.
5459 var isScanning : Bool { status == . scanning }
5560
61+ /// Stores `centralStateLabel` used by this scope.
5662 var centralStateLabel : String {
5763 " \( centralState. rawValue) ( \( stateLabel ( centralState) ) ) "
5864 }
5965
66+ /// Handles `startScan`.
67+ ///
68+ /// - Parameters:
69+ /// - clearList: Input used by this operation.
6070 func startScan( clearList: Bool = true ) {
6171 guard isBluetoothReady else {
6272 status = . bluetoothOff
@@ -77,12 +87,17 @@ final class BLEManager: NSObject, ObservableObject {
7787 log. info ( " BLE scan started (no service filter, allow duplicates) " )
7888 }
7989
90+ /// Handles `stopScan`.
8091 func stopScan( ) {
8192 central. stopScan ( )
8293 if status == . scanning { status = . idle }
8394 addDevLog ( " SCAN ■ stop " )
8495 }
8596
97+ /// Handles `connect`.
98+ ///
99+ /// - Parameters:
100+ /// - item: Input used by this operation.
86101 func connect( _ item: DiscoveredPeripheral ) {
87102 stopScan ( )
88103 status = . connecting
@@ -92,18 +107,25 @@ final class BLEManager: NSObject, ObservableObject {
92107 addDevLog ( " CONN ▶ \( item. name) " )
93108 }
94109
110+ /// Handles `disconnect`.
95111 func disconnect( ) {
96112 guard let p = activePeripheral else { return }
97113 central. cancelPeripheralConnection ( p)
98114 }
99115
116+ /// Handles `sendInitAndStartIfReady`.
100117 func sendInitAndStartIfReady( ) {
101118 guard activePeripheral != nil , let rx = rxChar else { return }
102119 write ( [ 0x01 , 0x00 ] , to: rx, type: . withResponse)
103120 write ( [ 0x20 , 0x10 , 0x00 , 0xD0 , 0x07 , 0x34 , 0x00 , 0x03 ] , to: rx, type: . withoutResponse)
104121 addDevLog ( " TX ▶ INIT + START " )
105122 }
106123
124+ /// Handles `writeHex`.
125+ ///
126+ /// - Parameters:
127+ /// - hex: Input used by this operation.
128+ /// - withResponse: Input used by this operation.
107129 func writeHex( _ hex: String , withResponse: Bool ) {
108130 guard let rx = rxChar else { return }
109131 let bytes = Hex . parse ( hex)
@@ -187,14 +209,21 @@ final class BLEManager: NSObject, ObservableObject {
187209 }
188210}
189211
212+ /// Represents discovered peripheral.
190213struct DiscoveredPeripheral : Identifiable , Equatable {
214+ /// Stores `id` used by this scope.
191215 let id : UUID
216+ /// Stores `peripheral` used by this scope.
192217 let peripheral : CBPeripheral
218+ /// Stores `name` used by this scope.
193219 var name : String
220+ /// Stores `rssi` used by this scope.
194221 var rssi : Int
222+ /// Stores `isLikelyController` used by this scope.
195223 var isLikelyController : Bool
196224}
197225
226+ /// Adds focused blemanager helpers.
198227extension BLEManager : CBCentralManagerDelegate {
199228 nonisolated func centralManagerDidUpdateState( _ central: CBCentralManager ) {
200229 Task { @MainActor in
@@ -255,6 +284,7 @@ extension BLEManager: CBCentralManagerDelegate {
255284 }
256285}
257286
287+ /// Adds focused blemanager helpers.
258288extension BLEManager : CBPeripheralDelegate {
259289 nonisolated func peripheral( _ peripheral: CBPeripheral , didDiscoverServices error: Error ? ) {
260290 Task { @MainActor in
@@ -353,6 +383,7 @@ extension BLEManager: CBPeripheralDelegate {
353383 }
354384}
355385
386+ /// Represents hex.
356387enum Hex {
357388 static func format( _ bytes: [ UInt8 ] ) -> String {
358389 bytes. map { String ( format: " %02X " , $0) } . joined ( separator: " " )
0 commit comments