Skip to content

Instantly share code, notes, and snippets.

View AminPlusPlus's full-sized avatar
🌍
Working from home

Amin Abdullo AminPlusPlus

🌍
Working from home
View GitHub Profile
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)
@AminPlusPlus
AminPlusPlus / ViewController
Created July 9, 2020 02:26
DiscoverCharacteristics for service
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
}
}
}
@AminPlusPlus
AminPlusPlus / ViewController
Created July 9, 2020 02:24
PeripheralDiscoverCharac
//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)
@AminPlusPlus
AminPlusPlus / ViewController
Created July 9, 2020 02:22
Discover Services
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
//discover all service
peripheral.discoverServices(nil)
peripheral.delegate = self
}
@AminPlusPlus
AminPlusPlus / ViewController
Created July 9, 2020 02:20
Connect Peripheral
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
@AminPlusPlus
AminPlusPlus / ViewController
Last active July 9, 2020 02:19
Setup CBCentralManager
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...")