<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=160269078105920&amp;ev=PageView&amp;noscript=1">
This service has been removed!
Service successfully added!
Sign up for our blog, talk to a specialist, or just send us an email

Using Bluetooth on iOS Devices

Thursday 04 of May, 2017./ Reading Time: 6 minutes./ By Avantica

pexels-photo-249324

Bluetooth technology has become a household name. That's how important it has become. Bluetooth allows us to share data, audio files, videos, and large amounts of information between paired devices. It’s built into most devices, phones, computers, cars, portable devices, and just about everything tech. Apple has taken advantage of Bluetooth to offer features like Handsoff and AirDrop that enhance the user's experience with their Apple products. Apple also leverages Bluetooth with iOS to allow the Apple Watch and AirPods to communicate with your phone and open more opportunities. 

As mobile development trends remain strong, it’s important for software developers to have clear knowledge about what Bluetooth technology is and how it's ingrained in mobile technologies. Before starting from scratch with a Bluetooth app, you should understand what is happening when a Bluetooth application is submitted under foreground and background scenarios. You should understand how to get it working and what its possibilities and limitations are. Continue reading to see our advice for iOS Bluetooth programming with examples.

 

Bluetooth

Bluetooth is a standard wireless technology for exchanging data over short distances from fixed and mobile devices, and building personal area networks. Bluetooth is managed by the Bluetooth Special Interest Group (SIG) and is regularly updated and enhanced by Bluetooth SIG Working Groups.

There are three main implementations of Bluetooth tech:

  • Bluetooth  Basic Rate / Enhanced Data Rate (BR/EDR): Establishes a relatively short-range, continuous wireless connection, which makes it ideal for use in cases, such as streaming audio.

  • Bluetooth with low energy (LE): Allows for short bursts of long-range radio connection, making it ideal for Internet of Things (IoT) applications that don’t require a continuous connection, but depend on long battery life.

  • Dual-Mode: Is the combination of the two previous implementations. It’s available to support single devices such as smartphones or tablets that need to connect to both BR/EDR devices (such as audio headsets), and LE devices (such as wearables or retail beacons).


 

iOS Core Bluetooth

The Core Bluetooth framework, provided for iOS and Mac apps, enables communication with devices that are equipped with Bluetooth low energy (BLE) wireless technology. This means your app can discover, explore, and interact with low energy peripheral devices, such as heart rate monitors and digital thermostats.

BLE wireless technology is based in the Bluetooth 4.0 specification. The Core Bluetooth framework is an abstraction of the BLE protocol stack. It means that many of the low-level details are hidden from the developer, making it much easier to develop apps that interact with BLE devices.

In Core Bluetooth, there are two main players: the central and the peripheral. Each one has a different role. The role of the peripheral is to share data that is consumed by other devices, while the central uses the information provided by a peripheral to accomplish tasks.

Peripherals make their presence known by advertising the data they have to the surrounding devices. Meanwhile, the central scans for nearby peripherals that might have data it's interested in. When a peripheral is discovered by a central, the central requests to connect to the peripheral and begins exploring and interacting with the peripheral’s data. The peripheral is responsible for responding to the central in a proper way.

 

CBDevices1_2x.pngCentral and Peripheral Devices

For iOS apps, it’s very important to know whether the app is running in the foreground or background. An app that's running in background mode behaves differently because the system resources are limited on iOS devices.

iOS applications support both foreground and background modes for working with Bluetooth, though each has different behaviors.


Foreground-Only Apps

In this mode, when the app is in a suspended state, it’s unable to perform Bluetooth-related tasks, nor is it aware of any Bluetooth-related events until it resumes to the foreground. This behavior can affect your app, depending on the tasks it’s performing (e.g. if your app moves to the suspended state—maybe the user switches to another app). If the connection to the peripheral is lost while your app is suspended, you won’t be aware that any disconnection occurred until your app resumes to the foreground.


Background Execution Modes

Enable Background Modes in your project's capabilities to allow your application to run in background mode and avoid the aforementioned problems.

Screen Shot 2017-04-21 at 3.47.39 PM.png

In order for an app to continue to run in background mode and perform Bluetooth-related tasks, it must turned on for Core Bluetooth background execution mode in the Information property list (Info.plist) file.

Let's work together

There are two Core Bluetooth background execution modes that an app may declare: 

  • bluetooth-peripheral

For apps sharing data using CoreBluetooth and implementing the peripheral role. The app shares data using the Core Bluetooth framework.
  • bluetooth-central


For apps communicating using CoreBluetooth and implementing the central role. It communicates with Bluetooth low energy (BLE) peripherals using the Core Bluetooth framework.

Important: When you edit the property list, consider that the editor in Xcode, by default, displays human-readable strings for many keys instead of the actual key names. In order to display the actual key name, as it appears in the Info.plist file, Control-click any of the keys in the editor window and enable the Show Raw Keys/Values item in the contextual window.


The bluetooth-peripheral Background Execution Mode

This mode allows the app to perform certain peripheral role tasks while in the background. Therefore, the system wakes up your app to process read, write, and subscription events.

You should be aware that advertising data while your app is in background mode is different than when your app is in foreground mode. In particular, when your app is advertising while in the background:

  • The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of the peripheral is not advertised.

  • All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area. They can be discovered only by an iOS device that is explicitly scanning for them.

  • If all advertising apps are in the background, the frequency at which your peripheral device sends advertising packets may decrease.


 

The bluetooth-central Background Execution Mode
With this mode activated, your app can discover and connect to peripherals, as well as explore and interact with peripheral data while it’s in the background mode.

The system wakes up your app when any of the CBCentralManagerDelegate or CBPeripheralDelegate delegate methods are invoked, allowing your app to handle important central role events, such as when a connection is established or turn down, when a peripheral sends updated characteristic values, and when a central manager’s state changes.

Keep in mind that scanning for peripherals while your app is in background mode operates differently than when your app is in foreground mode:
  • The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored in background mode, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event. This value specifies whether the scan should run without duplicate filtering. Therefore, when your app is running in the background, it ignores this option and scans only peripherals that haven't been discovered before.

  • If all peripheral scanning apps are in the background, the interval at which your central device scans for advertising packets increases. As a result, it may take longer to discover an advertising peripheral.


 

Retrieving a list of known Peripherals

The first time you discover a peripheral, the system generates an identifier (a UUID, represented by an NSUUID object) to identify the peripheral. You can then store this identifier, and later use it to try to reconnect to the peripheral using the retrievePeripheralsWithIdentifiers: method of the CBCentralManager class.

You can also start scanning for peripherals, filtered by a list of services, using the scanForPeripheralsWithServices:options: method. In order to scan for peripherals while in background mode, you should indicate this list of services, so that the app will look for the peripherals with those services.

Important: Consider that if you don't add any service UUID, the application won’t scan any peripherals while in background mode.

 

Scanning Rate

When the App enteres in background mode, the scanning parameters are changed in order to reduce power consumption. It should be a balance between performance and power consumption while the phone is in standby mode.

Since the phone is scanning with a low duty cycle, the peripheral will have to go the extra mile and advertise more often. The approximate average discovery time when the phone is in standby is about 60 times the advertising interval. The 95-percentile discovery time when the phone is in standby mode is about 300 times the advertising interval. This means that, if the peripheral advertises every 20 ms, an iPhone in standby mode would discover it within 6 seconds with a 95 percent probability.

 

Conclusion

When you go deep into the technology and the options the platform gives, you notice that working with Bluetooth is very straightforward. You do have to deal with the limitations while working on background modes and craft a solution that is based on how iOS works and behaves in different scenarios. Make sure your iOS apps programming includes the best options for performance and efficiency. 

 

About Avantica

If you are looking for a software partner who will work towards your own business goals and success, then Avantica is your solution. We offer dedicated teams, team augmentation, and individual projects to our clients, and are constantly looking for the best methodologies in order to give you the best results.


Let’s start a project together

Join our newsletter

PREVIOUS
Who needs innovation more? Clients or Employees?
NEXT
Accessibility Testing: What It Is and Why You Should Care About It