Swift 3: Bluetooth list is empty

If (central.state == .poweredOn) does not check. Bluetooth list does not come what can i do?



import CoreBluetooth

import UIKit

class ViewController: UIViewController {

var centralManager: CBCentralManager?

var peripherals = Array<CBPeripheral>()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {

super.viewDidLoad()

centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)

}

}

extension ViewController: CBCentralManagerDelegate {

func centralManagerDidUpdateState(_ central: CBCentralManager) {

print(central.state)

if (central.state == .poweredOn){

self.centralManager?.scanForPeripherals(withServices: nil, options: nil)

}

else {

print("nil")

}

}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

peripherals.append(peripheral)

print(peripheral)

}


}

Replies

What list ?

your code is incomplete but when I fill the missing bits it works (and I am by no means an expert)

Remember that you'll have to use an actual device if you want to use CoreBluetooth, the simulator won't work.


hope this helps