Skip to content

Instantly share code, notes, and snippets.

@ChrisMarshallNY
Last active June 12, 2020 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMarshallNY/d287be6dbcc88627178058bdee348d32 to your computer and use it in GitHub Desktop.
Save ChrisMarshallNY/d287be6dbcc88627178058bdee348d32 to your computer and use it in GitHub Desktop.
Companion Gists to the Introduction to Core Bluetooth Class
extension ITCB_SDK_Central {
override var _managerInstance: Any! {
get {
if super._managerInstance == nil {
print("Creating A new instance of CBCentralManager.")
super._managerInstance = CBCentralManager(delegate: self, queue: nil)
}
return super._managerInstance
}
set {
super._managerInstance = newValue
}
}
}
extension ITCB_SDK_Central: CBCentralManagerDelegate {
public func centralManagerDidUpdateState(_ centralManager: CBCentralManager) { }
}
if centralManager.state == .poweredOn {
print("Scanning for Peripherals")
centralManager.scanForPeripherals(withServices: [_static_ITCB_SDK_8BallServiceUUID], options: nil)
}
internal let _static_ITCB_SDK_RSSI_Min = -60
internal let _static_ITCB_SDK_RSSI_Max = -20
public func centralManager(_ centralManager: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi: NSNumber) {
if !devices.contains(where: { $0.uuid == peripheral.identifier.uuidString }),
let peripheralName = peripheral.name,
!peripheralName.isEmpty,
(_static_ITCB_SDK_RSSI_Min..._static_ITCB_SDK_RSSI_Max).contains(rssi.intValue) {
print("Peripheral Discovered: \(peripheralName), RSSI: \(rssi)")
devices.append(ITCB_SDK_Device_Peripheral(peripheral, owner: self))
print("Connecting to \(peripheralName).")
centralManager.connect(peripheral, options: nil)
}
}
public func centralManager(_ centralManager: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Successfully Connected to \(peripheral.name ?? "ERROR").")
print("Discovering Services for \(peripheral.name ?? "ERROR").")
peripheral.discoverServices([_static_ITCB_SDK_8BallServiceUUID])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment