We will learn about how central discovers peripheral devices and show them in a UITableView. Make sure to have read Bluetooth & Bluetooth fundamentals.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
//Make sure to import CoreBluetooth framework import CoreBluetooth import UIKit class ViewController: UIViewController { //Create a variable centralManager of type CBCentralManager var centralManager: CBCentralManager? //Initialize a array to hold the discovered peripherals var discoveredPeripherals = Array<CBPeripheral>() //Create a table view from storyboard @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() //Initialise CoreBluetooth Central Manager, it takes two parameters delegate and queue. //If you want to implement all the delegate methods of CBCentralManagerDelegate in this class you can set delegate to self. //We want to perform central manager tasks on main thread. You can pass a background queue or nil also. centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main) //as soon as central manager is initialized it will call it's delegate method centralManagerDidUpdateState. } } //lets create a extension on view controller, and confirm to CBCentralManagerDelegate extension ViewController: CBCentralManagerDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .poweredOn: print("powered on") //The right time to scan for other peripheral devices. // Here we pass nil for the services and options, for the simplicity. //We want to scan for all the peripherals nearby without specifically looking for a specific peripheral. centralManager?.scanForPeripherals(withServices: nil, options: nil) //scanForPeripherals will give automatic call back when it will discover a peripheral. //scanForPeripheral will call didDiscover peripheral delegate. case .poweredOff: print("powered off") case .resetting: print("resetting") case .unauthorized: print("unauthorized") case .unknown: print("unknown") case .unsupported: print("unsupported") } } /*we get the call to this delegate method because of scanForPeripheral method call previously. This delegate call back contains important info about peripheral like peripheral object discovered, advertisement data and RSSI. */ func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { discoveredPeripherals.append(peripheral)//append peripherals in a array so that to show it in UITableView tableView.reloadData()//reload tableview to reflect discoveredPeripherals changes. } } extension ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell //access discoveredPeripherals for specific row let peripheral = discoveredPeripherals[indexPath.row] //use peripheral property name to show on UILabel cell.textLabel?.text = peripheral.name return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return discoveredPeripherals.count } } |
Wrap up : So to scan for peripherals the least steps are: Init CBCentralManager Implement CBCentralManager delegate method Read More …