forked from Polidea/RxBluetoothKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCentralManagerRestoredState.swift
66 lines (55 loc) · 2.87 KB
/
CentralManagerRestoredState.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Foundation
import CoreBluetooth
/// It should be deleted when `RestoredState` will be deleted
protocol CentralManagerRestoredStateType {
var restoredStateData: [String: Any] { get }
var centralManager: CentralManager { get }
var peripherals: [Peripheral] { get }
var scanOptions: [String: AnyObject]? { get }
var services: [Service] { get }
}
/// Convenience class which helps reading state of restored CentralManager.
public struct CentralManagerRestoredState: CentralManagerRestoredStateType {
/// Restored state dictionary.
public let restoredStateData: [String: Any]
public unowned let centralManager: CentralManager
/// Creates restored state information based on CoreBluetooth's dictionary
/// - parameter restoredStateDictionary: Core Bluetooth's restored state data
/// - parameter centralManager: `CentralManager` instance of which state has been restored.
init(restoredStateDictionary: [String: Any], centralManager: CentralManager) {
restoredStateData = restoredStateDictionary
self.centralManager = centralManager
}
/// Array of `Peripheral` objects which have been restored.
/// These are peripherals that were connected to the central manager (or had a connection pending)
/// at the time the app was terminated by the system.
public var peripherals: [Peripheral] {
let objects = restoredStateData[CBCentralManagerRestoredStatePeripheralsKey] as? [AnyObject]
guard let arrayOfAnyObjects = objects else { return [] }
#if swift(>=4.1)
let cbPeripherals = arrayOfAnyObjects.compactMap { $0 as? CBPeripheral }
#else
let cbPeripherals = arrayOfAnyObjects.flatMap { $0 as? CBPeripheral }
#endif
return cbPeripherals.map { centralManager.retrievePeripheral(for: $0) }
}
/// Dictionary that contains all of the peripheral scan options that were being used
/// by the central manager at the time the app was terminated by the system.
public var scanOptions: [String: AnyObject]? {
return restoredStateData[CBCentralManagerRestoredStatePeripheralsKey] as? [String: AnyObject]
}
/// Array of `Service` objects which have been restored.
/// These are all the services the central manager was scanning for at the time the app
/// was terminated by the system.
public var services: [Service] {
let objects = restoredStateData[CBCentralManagerRestoredStateScanServicesKey] as? [AnyObject]
guard let arrayOfAnyObjects = objects else { return [] }
#if swift(>=4.1)
let cbServices = arrayOfAnyObjects.compactMap { $0 as? CBService }
#else
let cbServices = arrayOfAnyObjects.flatMap { $0 as? CBService }
#endif
return cbServices.map { Service(peripheral: centralManager.retrievePeripheral(for: $0.peripheral),
service: $0) }
}
}