Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// BluetoothManager.swift
//
import CoreBluetooth
protocol BluetoothManagerDelegate: AnyObject {
func didErrorReceived(manager: NSObject)
func didSensorReceived(data: Data, rssi: Int)
}
class BluetoothManager: NSObject {
weak var delegate: BluetoothManagerDelegate?
var centralManager: CBCentralManager!
let kCBAdvDataManufacturerData = "kCBAdvDataManufacturerData"
let kCBAdvDataLocalName = "kCBAdvDataLocalName"
init(delegate: BluetoothManagerDelegate) {
self.delegate = delegate
}
func start() {
self.centralManager = CBCentralManager(delegate: self, queue: DispatchQueue(label: "com.albertopasca.blequeue"))
}
func stop() {
self.centralManager.stopScan()
}
}
extension BluetoothManager: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state != .poweredOn {
delegate?.didErrorReceived(manager: self)
} else {
centralManager.scanForPeripherals(withServices: nil, options: nil)
}
}
func centralManager(_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String: Any], rssi RSSI: NSNumber) {
print( advertisementData )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.