Bluetooth | Bluetooth low energy | Swift

Classic Bluetooth, or the original Bluetooth involves streaming of data and especially suited for voice streaming. So, products such as Headphones and streaming sensor applications used Classic Bluetooth.

Bluetooth Low Energy (BLE) is an emerging wireless technology developed by the Bluetooth Special Interest Group (SIG) for short-range communication. Bluetooth 4.0 specification came along and enabled Bluetooth Low Energy connectivity. Bluetooth Low Energy essentially offers low power consumption of a magnitude of 1-50% of that of Classic Bluetooth. It is not intended to use for stream data. Low power consumption is the most important difference, most imp diff REMEMBER.

Common between Bluetooth Classic and BLE:

  • Both Bluetooth Classic and Bluetooth LE operate in the same 2.4 GHz ISM band.
  • Both Bluetooth Classic and Bluetooth LE operate with the same pairing technology, authentication and even encryption

Both Bluetooth and Bluetooth Low Energy involve devices that operate in a classic master-slave model. First the Bluetooth devices need to be paired and then the data transmission can happen.

BLE VS Classic bluetooth : 

Why low power:

In order to save energy, Peripheral and central devices use sleep mode model periodically.

Connection process:

Basic Bluetooth 4.0 Terminology

  • Bluetooth Central: This is the node that is trying to connect to a data source. Think of this as the client.
  • Bluetooth Peripheral: This is the node that is providing the primary data source. Think of this as the server.
  • Characteristic: A characteristic can be considered a variable. Characteristics are defined attribute types that contain a single logical value.
  • Service: A group of characteristics live under a “Service”. Services are collections of characteristics and relationships to other services that encapsulate the behavior of part of a device. Service are collection of data and associated behaviors describing a specific function or feature of a peripheral. For example, a heart rate sensor has a Heart Rate service. A peripheral can have more than one service.
  • Descriptors are defined attributes that describe a characteristic value.
  • Each service and characteristic is represented by a UUID which can be either a 16-bit or a 128-bit value.
  • Central’ Job
    • Scanning for peripherals
    • Connecting to a peripheral
    • Disconnecting from a peripheral
    • Discovering services
    • Discovering characteristics of a service
    • Subscribing to a characteristic
    • Receiving data from a characteristic
  • Peripheral’s Job
    • Advertising a service and characteristic
    • Adding service and characteristic to the PeripheralManager
    • Detecting of new subscribers to a characteristics
    • Detecting of unsubscribing
    • Handling of unready state of the device.

Note : Peripheral does not connect to peripheral. and central does not connect to central.

Framework

Core Bluetooth :

Overview

The Core Bluetooth framework provides the classes needed for your apps to communicate with devices that are equipped with Bluetooth low energy wireless technology.

Do not subclass any of the classes of the Core Bluetooth framework. Overriding these classes is unsupported and results in undefined behavior.

class CBCentral

The CBCentral class represents remote central devices that have connected to an app implementing the peripheral role on a local device. That is, when you are implementing the peripheral role using the CBPeripheralManager class, centrals that connect to your local peripheral are represented as CBCentral objects. Remote centrals are identified by universally unique identifiers (UUIDs), represented by NSUUID objects.

scan for other peripherals

Central devices scan for any advertising peripherals and displays it to the user

A central typically uses the information served up by peripherals to accomplish some particular task.

  • A central is kinda like the “boss”. It wants information from a bunch of its workers in order to accomplish a particular task.

It defines logic behind the Core bluetooth communication. It is able to scan, connect and disconnect peripherals and it also consumes information available at each peripheral. The central devices are usually mobile devices or tablets.

class CBPeripheral

The CBPeripheral class represents remote peripheral devices that your app—by means of a central manager (an instance of CBCentralManager)—has discovered advertising or is currently connected to. Peripherals are identified by universally unique identifiers (UUIDs), represented by NSUUID objects. Peripherals may contain one or more services or provide useful information about their connected signal strength.

advertise information as peripheral

  • A peripheral is kinda like the “worker”. It gathers and publishes data to that is consumed by other devices/means central.

The device that has information to share eg. Heart rate or temperature etc. The information can be pushed or advertise over the air.The peripheral devices are things like Heart rate monitor, BLE enables proximity tags, etc.

The following image defines the relationship between central and peripherals:

In the above scenario IOS devices (Central) communicate with the Heart rate Monitoring system (Peripheral) to get heart rate information and shows it on a device.

How Centrals Communicate with Peripherals

Advertising is the primary way that peripherals make their presence known via Bluetooth LE.

In addition to advertising their existence, advertising packets can contain some data, such as the peripheral’s name. It can also include some extra data related to what the peripheral collects. For example, in the case of a heart rate monitor, the packet also provides heartbeats per minute (BPM) data.

The job of a central is to scan for these advertising packets, identify any peripherals it finds relevant, and connect to individual devices for more information.

class CBService

CBService and its subclass CBMutableService represent a peripheral’s service—a collection of data and associated behaviors for accomplishing a function or feature of a device (or portions of that device). CBService objects in particular represent services of a remote peripheral device (represented by a CBPeripheral object). Services are either primary or secondary and may contain a number of characteristics or included services (references to other services).

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

The following diagram shows the hierarchy of the CBPeripheral:

What are CBCentralManagerStates:

  • CBCentralManagerStateUnknown
  • CBCentralManagerStateResetting
  • CBCentralManagerStateUnsupported
  • CBCentralManagerStateUnauthorized
  • CBCentralManagerStatePoweredOff
  • CBCentralManagerStatePoweredOn

BLE VS classic bluetooth?

    • Classic Bluetooth, or the original Bluetooth was an extremely popular connectivity option for products which involves streaming of data and was especially suited for voice streaming. So, products such as Headphones and streaming sensor applications used Classic Bluetooth.
    • The Bluetooth 4.0 specification came along and enabled Bluetooth Low Energy connectivity. Bluetooth Low Energy essentially offers low power consumption of a magnitude of 1-50% of that of Classic Bluetooth.
    • Bluetooth Low Energy (Bluetooth LE) is ideally suited to connectivity in products that require only periodic transfer of data and not continuous streaming of data. This makes Bluetooth Low Energy suitable to IOT applications such as building automation and lighting. Heart rate monitoring.
    • Classic Bluetooth is fantastic for products that require continuous streaming of voice and data. Headphones are the classic application of Classic Bluetooth.

 

  • Bluetooth Low Energy (BLE) is not intended to stream data !

What are the central’s states and peripheral state

What is GATT?

  • GATT is an acronym for the Generic Attribute Profile, and it defines the way that two Bluetooth Low Energy devices transfer data back and forth using concepts called Services and Characteristics.

GATT database is comprised of Services and Characteristics.

  • All functions are divided up into services and characteristics. Each service and characteristic has its own UUID.

What is GAP?

GAP

GAP is an acronym for the Generic Access Profile, and it controls connections and advertising in Bluetooth. GAP is what makes your device visible to the outside world, and determines how two devices can (or can’t) interact with each other.

Device Roles

GAP defines various roles for devices, but the two key concepts to keep in mind are Central devices and Peripheral devices.

  • Peripheral devices are small, low power, resource contrained devices that can connect to a much more powerful central device. Peripheral devices are things like a heart rate monitor, a BLE enabled proximity tag, etc.
  • Central devices are usually the mobile phone or tablet that you connect to with far more processing power and memory.
  • GAP (Generic Access Profile) Central and GAP Peripheral roles. The GAP Observer and GAP Broadcaster roles are typically used in applications.
  • GAP is the layer of the BLE stack which determines the network topology of the BLE system. The GAP Central is typically the device which initiates the connection with the GAP Peripheral. Once the two devices are connected, they will perform a “pairing” process where they will exchange the information necessary to establish an encrypted connection.

What are BLE MODES?

Advertising and connected mode

The two modes BLE uses are:

Advertising mode

the peripheral sends out a bit of information that any device in the area can pick up. This is how central devices know that there are peripherals around.

Connected mode

the peripheral and a central device establish a one-to-one conversation. This is how they can exchange complex information.

Next Bluetooth | Bluetooth low energy | Discovering Peripherals | Swift

 

 

 

Thankful to these web sites:

https://learn.adafruit.com/crack-the-code/overview

https://learn.adafruit.com/introduction-to-bluetooth-low-energy/introduction

http://www.kevinhoyt.com/2016/05/20/the-12-steps-of-bluetooth-swift/

https://www.cloudcity.io/blog/2016/09/09/zero-to-ble-on-ios–part-two—swift-edition/

https://wingoodharry.wordpress.com/2016/01/21/get-list-of-ble-devices-using-corebluetooth-on-ios-swift/

Very advanced using futures:

https://medium.com/@yostane/getting-started-with-bluetooth-low-energy-on-ios-ada3090fc9cc

Bluetooth Special Interest Group (SIG) : https://www.bluetooth.com/bluetooth-technology

One thought on “Bluetooth | Bluetooth low energy | Swift”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.