How to work with BLE Devices on iOS
BLE (Bluetooth Low Energy Devices) are supported with iPhones, so why not we (developers) explore the possibilities of its implementation. Lets dive deep into BLE device integration with iPhones.
Lets first get familiar with the terminologies of the BLE and learn about Tools of integrating them:
Introduction of the tools
Lets first understand the Central & peripheral devices in bluetooth. The whole BLE communication depends on these two concepts.
This device listens to the advertising devices, It sends them command and get their information. You can imagine theses devices as the Reporting Managers which constantly ask for the information and sends command on which reports they need.
This device advertise themselves so they can be found by other devices. These devices has predefined set of commands which they accepts from other devices and they act as data center for providing some information or performing some tasks.
Lets imagine the Beacon examples. There are malls which has the beacon devices on each sections. Here beacon act as the Peripheral devices which constantly advertise themselves while the iPhone act as the Central device which constantly ask for the informations from the beacons. Here beacons will send them their information using which iPhone can show the latest offer or new collection on that section of the mall.
There are immense possibilities for the application of BLE devices. So let’s not wait and dive into the actual implementation of BLE integration with iPhone:
Setting up the environment
So for the BLE implementation we will need to follow few steps as mentioned below :
1. Create the New xCode Project
Here we will need to create the new xcode project, Make sure you include the unique bundle identifier of your project.
2. Enable Background Modes to make use of bluetooth even in background.
Well you might be wondering why we need to enable the background modes. The reason is we will need to keep track of bluetooth availability & range even to work in background modes. This is common with most of beacon apps.
So you can enable the background modes via checking the option for “Use Bluetooth LE accessories” tick mark, as described in the above image.
3. Include CoreBluetooth libraries
Now to communicate with iPhone’s bluetooth device we will need to access its Bluetooth Framework, iOS SDK provides CoreBluetooth.framework with set of predefined function & delegate methods for us to communicate with the bluetooth hardware of iOS.
We can get the events like change of bluetooth status, new device detection and so on by the delegate methods provided by CoreBluetooth.framework.
Lets dive into the coding part. We will need to follow few steps for the implementation. They are as below:
Step 1: Create the UI
Create UITableView & Label in the Storyboard as shown below :
Step 2 : Setup Central Manager & Initialise the needed variables
We will include the CoreBluetooth File via import statement as described below :
Here we have the setupBluetooth function to initialise the CBCentralManager. We also have the array of CBPeripherals, where we will store all the detected Peripherals in the array.
In the initialisation we tell the CBCentralManager to send the events to our ViewController file by telling delegate : self
Step 3: Setup Delegate Methods for CBCentralManager
It’s always handy to use the extensions so that Delegation code & our business logic remains separate. It’s good practice to have this extensions in our code.
Here centralManagerDidUpdateState method gets called when the bluetooth is switched on or switched off or its state has changed. This method is good for reloading our TableView and ask user to perform some actions.
In Our case we will reload the tableview so that user will have the latest information on their screen and we will prompt user to switch on the bluetooth if they have not switched on the bluetooth. (The reason is we don’t have the access to switch on bluetooth by ourselves so we must prompt user to switch on their bluetooth devices)
Similarly didDiscover method will get called whenever we discover any new peripheral devices which can be beacons, bluetooth speakers and so on. Here we add each peripheral device into the array we have. If you have notices we also check if the peripheral is present in our array list, if it is we don’t add the duplicate. It is always good practice implement these checks.
Now let’s update UI i.e. UITableView & UILabel
Step 4: Implement The TableView Delegates
Here we update the UITableview based on the number of peripheral device we detect. In the cell we either show its name or its identifier.
Step 5: Connect with the Peripheral Device
Here on tapping the listed devices you should be able to establish the connection with the device. There are chances that it might fail, so we have both the methods implemented. If you have the specific UUID of the service of the device you can pass it so it would be discovering the specific services.
Step 6: Discover Services & Characteristics
We have the CBPeripheralDelegate delegate available, so we can implement it by extension. It has method “didDiscoverServices” which we will use it for getting all the services for the device.
Once we discover the device we can get its characteristics by implementing “didDiscoverCharacteristicsFor” method. We will use print() to print all the characteristics of the services.
Step 7: Run the App
Press Command + R. Yes you read it correctly. And we are ready with the app which will detect the peripheral devices, will list out its services & print their characteristics.
To implement the BLE device we just need to understand the concept of CentralManager & Peripheral Devices, Explore the CoreBluetooth.framework and play through its delegate methods and have fun.
Downloaded source code
We have the source code ready for you which you can download and simply run to view the demo.
- You would not be able to run this demo in simulator unless and until you have the external bluetooth device and you know how to configure it on your mac.
- You will need the BLE device or the device which can act as the Peripheral. If you would try turning on the bluetooth of your phone it will not work as it’s not broadcasting the BLE signals.
- Debugging: If you need to debug, have the android device with you, install the app : https://play.google.com/store/apps/details?id=net.alea.beaconsimulator. Enable the bluetooth and create iBeacon and advertise the beacon you will see the demo in action.
Why Swift Code Application Programming Language is the Best?
Swift may be considered a young platform in the development world. But, it has surely paved its way to becoming the most sought after language...
Why iPhones and iPads are Currently Ruling the Smartphone Industry
iPhone is a pioneer in the smartphone industry as it was developed much before the Android devices. First iPhone was launched on June 29, 2007...