Get List Of BLE Devices Using CoreBluetooth On iOS – Swift — January 21, 2016

Get List Of BLE Devices Using CoreBluetooth On iOS – Swift

This post will show you just the minimum effort needed to get a list of BLE (Bluetooth 4.0) devices that are advertising on your iOS device. Make sure you have an iOS device with BLE and another device that is capable of advertising its BLE services ( I am using an RFDuino, with it’s device name set to Display).

Let’s get started.

  • Create a new single view Swift project.
  • Add a UITableView to the ViewController in the pre-made Storyboard.
  • Attach the datasource and delegate from the UITableView to the ViewController in the Storyboard.
  • Add an IBOutlet for the UITableView in your ViewController class.
  • Make sure the class for the ViewController is set to the class ViewController.

Add the CoreBluetooth framework to your project.

Screen Shot 2016-01-21 at 16.51.32


Now make your ViewController Swift file look like this:

import CoreBluetooth
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate
    var centralManager: CBCentralManager?
    var peripherals: Array<CBPeripheral> = Array<CBPeripheral>()
	@IBOutlet weak var tableView: UITableView!
	override func viewDidLoad()
	//Initialise CoreBluetooth Central Manager
        centralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())

    //CoreBluetooth methods
    func centralManagerDidUpdateState(central: CBCentralManager)
        if (central.state == CBCentralManagerState.PoweredOn)
            self.centralManager?.scanForPeripheralsWithServices(nil, options: nil)
            // do something like alert the user that ble is not on

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)

	//UITableView methods
	func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
		let peripheral = peripherals[indexPath.row]
        cell.textLabel?.text =

        return cell

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        return peripherals.count

centralManagerDidUpdateState will be called whenever the CoreBluetooth BLE service state changes. For this example I am only checking if the state == CBCentralManagerState.PoweredOn but there are other states you can check for such as off, unknown and resetting. When the state is on we initiate a device scan using scanForPeripheralsWithServices.

If the service finds a device the delegate method didDiscoverPeripheral is called. In this method we just get the newly discovered peripheral and add it to our array of peripherals. We then call reloadData on the tableView so that the new object in the array is used in our tableView. I have only used the name property from the peripheral but there is loads more stuff to play with.

When you turn on your BLE device you should see something like the following:

Screen Shot 2016-01-21 at 23.14.49