Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/13-retrieve-peripherals - Add functionality to retrieve peripherals by UUID #48

Merged
merged 1 commit into from
May 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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