Commit fa759dd6 authored by Thomas Mellenthin's avatar Thomas Mellenthin

Announce connection when all characteristics are discovered

parent 3fb7eb48
......@@ -321,10 +321,9 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("did connect: \(peripheral.name ?? "<nil>") \(peripheral.identifier))")
self.connectedPeripheral = peripheral
peripheral.delegate = self
peripheral.discoverServices([card10ServiceUUID, card10RxTxServiceUUID])
self.connectedPeripheral = peripheral
subscribers.forEach { $0.value?.didConnectToPeripheal(FoundPeripheral(peripheral: peripheral, advertisementName: nil, rssi: nil)) }
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
......@@ -360,8 +359,35 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print("did didDiscoverCharacteristicsFor: \(service.uuid) \(service.characteristics!))")
// FIXME: pairing is broken ATM
// pairWithCard10Badge(peripheral: peripheral)
// make sure that all characteristic are discovered, if yes announce to subscribers
let card10ServiceIsComplete = connectedPeripheral?
.services?
.filter { $0.uuid.uuidString == card10ServiceUUID.uuidString }
.first?
.characteristics?
.allSatisfy { [card10TimeCharacteristic,
card10VibrateCharacteristic,
card10RocketsCharacteristic,
card10BackgroundLedBottomLeftCharacteristic,
card10BackgroundLedBottomRightCharacteristic,
card10BackgroundLedTopRightCharacteristic,
card10BackgroundLedTopLeftCharacteristic,
card10LedsDimBottomCharacteristic,
card10LedsDimTopCharacteristic,
card10LedsAboveCharacteristic,
card10SingleRgbLedCharacteristic,
card10LightSensorCharacteristic].contains($0.uuid) } ?? false
let card10RxTxServiceIsComplete = connectedPeripheral?
.services?
.filter { $0.uuid.uuidString == card10RxTxServiceUUID.uuidString }
.first?
.characteristics?
.allSatisfy { [card10TXWriteCharacteristicUUID,
card10RXReadCharacteristicUUID].contains($0.uuid) } ?? false
if card10ServiceIsComplete && card10RxTxServiceIsComplete {
subscribers.forEach { $0.value?.didConnectToPeripheal(FoundPeripheral(peripheral: peripheral, advertisementName: nil, rssi: nil)) }
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment