Last active
June 12, 2020 17:25
-
-
Save ChrisMarshallNY/d287be6dbcc88627178058bdee348d32 to your computer and use it in GitHub Desktop.
Companion Gists to the Introduction to Core Bluetooth Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ITCB_SDK_Central: CBCentralManagerDelegate { | |
public func centralManagerDidUpdateState(_ centralManager: CBCentralManager) { } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if centralManager.state == .poweredOn { | |
print("Scanning for Peripherals") | |
centralManager.scanForPeripherals(withServices: [_static_ITCB_SDK_8BallServiceUUID], options: nil) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal let _static_ITCB_SDK_RSSI_Min = -60 | |
internal let _static_ITCB_SDK_RSSI_Max = -20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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