Skip to content

Commit

Permalink
WIP: 接続に成功したかどうかを受け取れるようにした(い)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Apr 2, 2022
1 parent b41f789 commit 243ab74
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Komusou/Settings/SensorSettingView.swift
Expand Up @@ -146,6 +146,11 @@ struct SensorSettingView_Previews: PreviewProvider {
}

final class BluetoothManager: NSObject {
enum ConnectingWithPeripheralError: Error {
case peripheralNotFound
}
typealias ConnectingWithPeripheralFuture = Future<Void, ConnectingWithPeripheralError>

static let shared = BluetoothManager()
static private let kSpeedSensorKey = "speed_sensor_key"

Expand Down Expand Up @@ -177,6 +182,7 @@ final class BluetoothManager: NSObject {
@Published
private var isBluetoothEnabled = false
private var isScanningPeripherals = false
private var speedSensorPromise: ConnectingWithPeripheralFuture.Promise?
private var cancellables = Set<AnyCancellable>()

private let centralManager = CBCentralManager()
Expand Down Expand Up @@ -208,11 +214,17 @@ final class BluetoothManager: NSObject {
centralManager.stopScan()
}

func connectToSpeedSensor(uuid: UUID) {
guard let peripheral = discoveredPeripherals[uuid] else { return }
func connectToSpeedSensor(uuid: UUID) -> ConnectingWithPeripheralFuture {
guard let peripheral = discoveredPeripherals[uuid] else {
return .init { $0(.failure(.peripheralNotFound)) }
}

speedSensorUUID = uuid
centralManager.connect(peripheral)

return .init { [unowned self] promise in
self.speedSensorPromise = promise
}
}
}

Expand All @@ -229,6 +241,7 @@ extension BluetoothManager: CBCentralManagerDelegate {
switch peripheral.identifier {
case speedSensorUUID:
speedSensor = peripheral
speedSensorPromise?(.success(()))
default:
break
}
Expand Down

0 comments on commit 243ab74

Please sign in to comment.