Skip to content

Commit

Permalink
Resolved issue #13: Added API to retrieve peripherals
Browse files Browse the repository at this point in the history
- Automatically call centralManager.retrievePeripherals on powered on, to get CoreBluetooth running
  • Loading branch information
sureshjoshi committed May 19, 2020
1 parent 8f1a489 commit 4a02340
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions Sources/SwiftyTeeth/SwiftyTeeth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ open class SwiftyTeeth: NSObject {
stateChangedHandler?(state)
}
}

fileprivate var scanChangesHandler: ((Device) -> Void)?
fileprivate var scanCompleteHandler: (([Device]) -> Void)?

open lazy var centralManager: CBCentralManager = {
public lazy var centralManager: CBCentralManager = {
let instance = CBCentralManager(
delegate: self,
queue: DispatchQueue(label: "com.robotpajamas.SwiftyTeeth"))
// Throwaway command to init CoreBluetooth (helps prevent timing problems)
instance.retrievePeripherals(withIdentifiers: [])
return instance
}()

Expand All @@ -38,20 +33,16 @@ open class SwiftyTeeth: NSObject {
}

// TODO: Hold a private set, and expose a list?
open var scannedDevices = Set<Device>()

// TODO: Should be a list? Can connect to > 1 device
fileprivate var connectedDevices = [String:Device]()
public var scannedDevices = Set<Device>()


// TODO: Need iOS 9 support
// open var state: CBManagerState {
// return centralManager.state
// }

open var isScanning: Bool {
public var isScanning: Bool {
return centralManager.isScanning
}

// TODO: Should be a list? Can connect to > 1 device
private var connectedDevices = [String:Device]()
private var scanChangesHandler: ((Device) -> Void)?
private var scanCompleteHandler: (([Device]) -> Void)?

public override init() {
}
Expand All @@ -68,6 +59,14 @@ public extension SwiftyTeeth {
}
}

// MARK: - Manager Utility functions
public extension SwiftyTeeth {
func retrievePeripherals(withIdentifiers uuids: [UUID]) -> [Device] {
let cbPeripherals = centralManager.retrievePeripherals(withIdentifiers: uuids)
return cbPeripherals.map { Device(manager: self, peripheral: $0) }
}
}

// MARK: - Manager Scan functions
public extension SwiftyTeeth {

Expand Down Expand Up @@ -145,6 +144,8 @@ extension SwiftyTeeth: CBCentralManagerDelegate {
Log(v: "Bluetooth state is powered off.")
case .poweredOn:
Log(v: "Bluetooth state is powered on")
// Throwaway command to init CoreBluetooth (helps prevent timing problems)
centralManager.retrievePeripherals(withIdentifiers: [])
default:
Log(v: "Bluetooth state is not in supported switches")
}
Expand Down

0 comments on commit 4a02340

Please sign in to comment.