Create a gist now

Instantly share code, notes, and snippets.

@erica /BTWhee.swift
Last active Mar 21, 2017

What would you like to do?
import Cocoa
import CoreBluetooth
import PlaygroundSupport
class BTHelper: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager
override init() {
self.centralManager = CBCentralManager(delegate: nil, queue: nil)
super.init()
self.centralManager.delegate = self
}
// Required. Invoked when the central manager’s state is updated.
func centralManagerDidUpdateState(_ manager: CBCentralManager) {
switch manager.state {
case .poweredOff:
print("BLE has powered off")
centralManager.stopScan()
case .poweredOn:
print("BLE is now powered on")
centralManager.scanForPeripherals(withServices: nil, options: nil)
case .resetting: print("BLE is resetting")
case .unauthorized: print("Unauthorized BLE state")
case .unknown: print("Unknown BLE state")
case .unsupported: print("This platform does not support BLE")
}
}
// Invoked when the central manager discovers a peripheral while scanning.
func centralManager(_ manager: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData advertisement: [String : Any], rssi: NSNumber) {
if let name = peripheral.name {
// RSSI is Received Signal Strength Indicator
print("Found \"\(name)\" peripheral (RSSI: \(rssi))")
} else {
print("Found unnamed peripheral (RSSI: \(rssi))")
}
print("Advertisement data:", advertisement)
print("")
}
}
let bt = BTHelper()
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment