Creating a Simple BLE Scanner on iPhone


Here are steps to create a simple BLE (Bluetooth Low Energy) scanner app for iPhone (for my own learning purpose). The goal is to enable below features.

  • Scan for all advertising devices around.
  • Print the device name, RSSI (signal strength), and advertising data [1] for each discovered device in the debug console output [2].

* It doesn’t filter any specific UUID, doesn’t connect to any peripheral device, and doesn’t show anything on app screen.

 

 

Prerequisites (parentheses indicate my environment)

  • Xcode (10.0) ruining on Mac (10.14)
  • iPhone (iPhone 8 with iOS 12.0.1)
    * An actual iOS device is required since Bluetooth is not supported in Xcode simulator.

 

Steps
1. Launch Xcode and create a Single View App.

2. Open ViewController.swift file and import CoreBluetooth framework.

3. In order to scan, iPhone need to be BLE central. So, add CBCentralManagerDelegate protocol to ViewController class.

4. Then, Xcode will prompt an error. Click on “Fix” button and it will create  centralManagerDidUpdateState method which will be called when the central manager’s state is changed. At startup, this method is called after instantiating CBCentralManager.

5. Declare a variable for CBCentralManager in ViewController class.

6. Instantiate CBCentralManager in viewDidLoad method.

7. In centralManagerDidUpdateState method, start scanning when the state is poweredOn.

8. Define centralManager didDiscover method in ViewController class. It will be called when any advertising device is discovered. Inside the method, it prints the device name, RSSI, and advertising data.

 

 

Test
1. Connect iPhone to Mac.

2. Make sure Bluetooth setting is ON on the connected iPhone.

3. Build and run the program, and check the console output on Xcode. Below is an example of the output.

In the example above, it detected my Fitbit Flex (Line 3-14) and some other devices. All possible advertising data type on CoreBluetooth are listed here.

 

References
[1] Advertising Data Type – Bluetooth SIG
[2] Console output in Xcode – Apple Developer

 

 

Sponsor Link

Leave a Reply

Your email address will not be published. Required fields are marked *