How to scan & connect & read peripherals?
Use protocol “CBCentralManagerDelegate“, “CBPeripheralDelegate“
How to scan BLE?
...
import CoreBluetooth
// Service UUID
let serviceUUID = CBUUID.init(string: "b4250400-fb4b-4746-b2b0-93f0e61122c6")
class ViewController: UIViewController, CBCentralManagerDelegate {
// Property
private var centralManager: CBCentralManager!
// Start scanning
func centralManagerDidUpdateState(_ central: CBCentralManager) {
// Update when the Bluetooth Peripheral is switched on or off. It will fire when an app first starts so you know the state of Bluetooth
print("Central state update")
if central.state != .poweredOn {
print("Central is not powered on")
} else {
print("Central scanning for", serviceUUID);
centralManager.scanForPeripherals(
withServices: [serviceUUID],
options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
}
}
...
}
How to connect a peripheral?
...
class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {
// Properties
private var centralManager: CBCentralManager!
private var peripheral: CBPeripheral!
// Start scanning
func centralManagerDidUpdateState(_ central: CBCentralManager) {
...
}
// Connect
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// We've found it so stop scan
self.centralManager.stopScan()
// Copy the peripheral instance
self.peripheral = peripheral
self.peripheral.delegate = self
// Connect!
self.centralManager.connect(self.peripheral, options: nil)
}
// Handler if we do connect succesfully
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
if peripheral == self.peripheral {
print("Connected!")
peripheral.discoverServices([serviceUUID])
}
}
...
}
How to read characteristics?
...
import CoreBluetooth
// Service UUID
let serviceUUID = CBUUID.init(string: "b4250400-fb4b-4746-b2b0-93f0e61122c6")
// Characteristic UUID
let redLEDCharacteristicUUID = CBUUID.init(string: "b4250401-fb4b-4746-b2b0-93f0e61122c6")
let greenLEDCharacteristicUUID = CBUUID.init(string: "b4250402-fb4b-4746-b2b0-93f0e61122c6")
let blueLEDCharacteristicUUID = CBUUID.init(string: "b4250403-fb4b-4746-b2b0-93f0e61122c6")
class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {
...
// Handles discovery event
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let services = peripheral.services {
for service in services {
if service.uuid == serviceUUID {
print("LED service found")
//Now kick off discovery of characteristics
peripheral.discoverCharacteristics([redLEDCharacteristicUUID,
greenLEDCharacteristicUUID,
blueLEDCharacteristicUUID], for: service)
return
}
}
}
}
// Handling discovery of characteristics
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.uuid == redLEDCharacteristicUUID {
print("Red LED characteristic found")
} else if characteristic.uuid == greenLEDCharacteristicUUID {
print("Green LED characteristic found")
} else if characteristic.uuid == blueLEDCharacteristicUUID {
print("Blue LED characteristic found");
}
}
}
}
...
}
Reference: ultimate-how-to-bluetooth-swift
Leave a Reply