Follow

Play with advanced BLE

Introduction

Bluetooth Low Energy or BLE has become very popular now a days as it is widely used in personal fitness gadgets, home automation products etc. Although BLE is similar to classic Bluetooth, its power consumption is less. Almost all major operating systems natively support BLE such as iOS 5 and later, Windows Phone 8.1, Android 4.3 and later, BlackBerry 10.

This article is limited to BLE in IOS and how it is implemented in it. While the sections to follow provide the necessary information with code fragments, it is suggested to download the sample project to get a complete picture. Please note that sample project works on IOS 7 or later and has basic functionality such as scanning and connecting to the BLE devices.

The Core Bluetooth framework is an abstraction of the Bluetooth low energy protocol stack and provides the necessary means for the developers to create apps that interact with the Bluetooth low energy devices. Using this framework, an app discover, explore, and interact with low energy peripheral devices.

Internals of Core Bluetooth two major components play a vital role in the framework:

  • Central:   Consumes the data provided by a peripheral, which in turn can be used to perform an action.
  • Peripheral: Holds the data that is needed by other devices. They make their presence known by advertising the data they have over the air.

Scanning

Central has the capability to scan for peripherals and can request for a connection when a peripheral is discovered. Upon connecting Central can do further interaction and use the peripheral’s data. Scanning for peripherals can be performed on the basis of the app requirements.

BLE01

  • Below code initiates the peripheral scanning and can find any BLE signal broadcasting peripheral.

Below code fragment can be used to scan for peripherals with services, where scanning can be done for a single or set of services.

  • There could be a requirement where scanning for peripherals may have to be done continuously. For this purpose a timer object can be used, where the desired interval in seconds can be specified.

  • A delegate method is invoked whenever a peripheral is located. Further filtering of the peripheral can be done here such as check for the Peripheral property etc. and communicate with it only if such property is found on the peripheral.

There could be a scenario where large number of BLE devices exist and a faster way of identifying all the scanned peripherals. In such case a DICTIONARY or ARRAY can be used to store all the scanned peripherals with peripheral identifier as Key. These can be used later to quickly connect to a BLE peripheral.

  • It is a good practice to stop the scanning when the desired peripherals are found, which can be done with the below line of code:

Note: iOS 7 used to support the scanning even if the device central manager power is OFF. In iOS 8 this behavior is changed and the device central manager will not scan until the power is ON.

The delegate method for this is as follows:

Start searching when the central manager is power ON.

Peripheral Communication App can connect to a peripheral as soon as it is located, which can be done with:

If a list of peripherals are available then the connection can be made with the help of peripheral identifier. This is very useful when there are many peripherals.

Below delegates will be fired upon successful connection.

If the connection results in failure, below delegate is fired:

It is a good practice to dis-connect the peripheral when it is no longer needed, which can be accomplished with the below line:

Upon disconnection below delegate is fired

We will explore more about Next peripherals, services and characteristics in the coming articles

You can download the sample project from here

Are you interested? follow us and get notified of new posts

Leave A Reply

two + nineteen =