This file contains 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
fileprivate var ledMask: UInt8 = 0 | |
fileprivate let digitalBits = 2 | |
func setDigitalOutput(_ index: Int, on: Bool, characteristic :CBCharacteristic) { | |
let shift = UInt(index) * UInt(digitalBits) | |
var mask = ledMask | |
if on { | |
mask = mask | UInt8(1 << shift) |
This file contains 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
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { | |
if let charac = service.characteristics { | |
for characteristic in charac { | |
//MARK:- Light Value | |
if characteristic.uuid == Digital { | |
self.lighCharacteristics = characteristic | |
} | |
} | |
} |
This file contains 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
//MARK:- CBPeripheralDelegate | |
extension ViewController : CBPeripheralDelegate { | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { | |
if let services = peripheral.services { | |
//discover characteristics of services | |
for service in services { | |
peripheral.discoverCharacteristics(nil, for: service) |
This file contains 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
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { | |
//discover all service | |
peripheral.discoverServices(nil) | |
peripheral.delegate = self | |
} |
This file contains 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
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
guard peripheral.name != nil else {return} | |
if peripheral.name! == "Thunder Sense #33549" { | |
print("Sensor Found!") | |
//stopScan | |
cbCentralManager.stopScan() | |
//connect |
This file contains 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
override func viewDidLoad() { | |
super.viewDidLoad() | |
//Start manager | |
cbCentralManager = CBCentralManager(delegate: self, queue: nil) | |
} | |
extension ViewController : CBCentralManagerDelegate { | |
func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
if central.state == .poweredOn { | |
central.scanForPeripherals(withServices: nil, options: nil) | |
print("Scanning...") |