Overview

Looking to learn how Bluetooth Low Energy works in iOS & Swift 3? Well look no further, we've got what you've been waiting for! This guide covers the basics on making your own Bluetooth Low Energy (BLE) app using the Core Bluetooth Framework

You'll need a basic understanding of Swift and iOS development, but no prior experience with Bluetooth Low Energy is required.  The example code can be applied to both iPhone and iPad users running the current version of IOS.

In this tutorial, I'll be using the Adafruit Feather 32u4 Bluefruit LE  to send and receive data.

Before you begin, know that the Simulator in Xcode isn't capable of using Bluetooth LE services. You'll need to follow along using your iPhone or iPad running the most current version of iOS (Version 10+).

Since you'll need to test your app using an iOS device, you'll also need an Apple Developer membership (please note that free membership enrollment is fine for following along with this guide)If you need help getting started as an iOS developer, check out our guide covering how to enroll to the Apple Developer program and gain membership:

Before we start...

  • Make sure your Xcode IDE is up-to-date to version 8.2.1 or newer.
  • While in Xcode make sure the development target is 10.2. or higher.
  • You can download the Arduino IDE from the main website here if you haven't already.

Parts Required

You don't need much to start this project, but you'll need these:

This is the Adafruit Feather 32u4 Bluefruit - our take on an 'all-in-one' Arduino-compatible + Bluetooth Low Energy with built in USB and battery charging. Its an Adafruit Feather 32u4 with a BTLE module, ready to rock!

Or

This is the Adafruit Feather M0 with a Bluefruit module. I didn't use this board for this project, but you can follow along with this as well.

&

This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. You'll need this to connect your Adafruit Feather to your laptop.

You'll also need a Mac running the Xcode IDE and an Apple Developer account (check out the iOS Development Guide for more info on how to do this)

Download project from Github

To best visualize how things work, you'll want to follow along with the app provided in this guide. The Basic Chat app sends and receives data using the Feather Bluefruit's UART connection.  You can learn more about UARTs here.

FIrst, go to the download page for the project.  Once on the page, click on the "Clone or download" button.

A small window will pop up beneath the "Clone or Download" button. Click the Download Zip button in the pop-up.

Once the file is downloaded, click on the Basic Chat.xcodeproj app and it will open up the project in Xcode.

Once we have the project open, select your iOS device's scheme in Xcode.

Now just press the Run button to test the app on your iOS device.

If all goes well, the Basic Chat app will run on your iOS device and you'll be able to use the app as a reference while you explore the guide.

Make sure your Bluetooth is enabled while using Basic Chat

These are the steps we'll make during the development process:

      • Create an instance of a CBCentralManager.

      • Once Bluetooth LE is powered on, we'll start scanning for CBPeripherals that hold services we are interested in.

     • Once we've found matching peripherals, we'll display them in a table view and allow the user to choose a peripheral to connect to. 

     • Once connected, we'll look for the CBServices we're interested on the CBPeripheral.

     • Once we find and connect to the desired CBService, we'll start looking for it's CBCharacteristics then subscribe to the one we are interested in. 

     • FInally, we'll send and receive data with the device using the CBCharacteristics found.

Before we begin, let's get a basic understanding of how some the app works.

Understanding CBCentralManager and Peripheral

The CBCentralManager plays the most important role in this application, it represents the local device. Think of it as the "key player" in this operation. The CBCentralManager is responsible for scanning and connecting to peripherals.

If the CBCentralManager is looked at as the key player, then the CBPeripheral can be looked at as the "supporting player". 

A CBPeripheral holds services (each defined as a CBService), and each CBService holds CBCharacteristics.

The following diagram shows the hierarchy of the CBPeripheral:

OK - Let's start looking at how the app works!

Getting Started

In Xcode, take a look at the project's file hierarchy in the left hand side of the window.  Select the file named "BLECentralViewController.swift" and check out its contents in the main window.  This file handles all of the BLE functions we'll be focussing on in this guide.  Let's take a look at some of the code …

At the top of the file, you'll notice the following line of code:

import CoreBluetooth

Importing the Core Bluetooth framework gives you the ability to communicate with BLE devices. With this you'll be able to make your own digital thermostat, game controller, heart rate monitor, and much more.

Next, notice that this object conforms to the CBCentralManagerDelegate protocol in order to discover, scan, and connect to peripheral devices in your area. In addition, it also conforms to the CBPeripheralDelegate protocol to analyze and explore peripheral properties.

class BLECentralViewController : UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate, UITableViewDelegate, UITableViewDataSource{

Likely the most important variable of the BLECentralManager is the CBCentralManager object.  The CBCentralManager object works behind the scenes and performs all the direct communication with CBPeripherals.  The code below creates the CBCentralManager and assigns its delegate variable to be the BLECentralManager.

Next we'll look at scanning for peripherals …

var centralManager : CBCentralManager!

//You'll want to add the line below into the your viewDidLoad
centralManager = CBCentralManager(delegate: self, queue: nil)
Last updated on 2017-07-28 at 05.14.23 PM Published on 2017-07-27 at 08.19.43 AM