Handling iOS 13 Bluetooth Permissions

Need to access CoreBluetooth? Ask the user first.

Anupam Chugh
Oct 25 · 3 min read

Core Bluetooth framework is an abstraction layer that provides developers access to BLE hardware. Apple introduced quite a few changes for the better during WWDC 2019. Besides fast transfer and power-efficient connections, much emphasis has been given to user privacy.

Until iOS 12, applications could access Bluetooth without the user’s knowledge. This could be done for good reasons, such as connecting to Chromecast or wireless headsets.

But this had it’s own pitfalls and created a hole in the user’s privacy. Developers could take advantage of this and track things like location data.

Starting iOS 13, if your application uses any of the Core Bluetooth APIs it requires the user’s permission. And off course, they can change it from settings!

New Bluetooth Permissions. Required if you use Core Bluetooth starting iOS 13.

If you’ve upgraded your device to iOS 13, I’m sure you’d have seen the above prompt plenty of times!

It brings to light the number of applications that had been using bluetooth so far.

Privacy Permissions And Usage Descriptions

Starting iOS 13, it’s mandatory for developers to specify the Privacy Usage Description for Bluetooth by including in their file. Accessing Core Bluetooth without the usage descriptions would lead to a runtime crash.

For backward support for older iOS versions, needs to be defined as well.

A screengrab from my latest iOS Application info.plist file.

API changes

is a newly added property is iOS 13. It’s used to determine the authorization status of Bluetooth permission.

The property can have any of the following states:

In the next section, we’ll be discussing the various steps you need to follow in order to integrate CoreBluetooth in your application.


Implementation

in order to use the Core Bluetooth Framework in your codebase.

For using the Core Bluetooth functionalities, we need to implement and protocols.

Initialize the Bluetooth Manager

is responsible for scanning and connecting to devices. Once the connection is done, the takes charge of the proceedings.

Right when the is initialized, the delegate method is triggered to check the state of the Bluetooth connection.

If the bluetooth is turned off, CBCentralManager can’t be instantiated and the system will automatically throw a dialog prompt asking you to enable it.

We can check the user authorization status using the property.

Scanning of devices is only possible when the state changes to

Note: Core Bluetooth scans for BLE devices only.

Connecting To Scanned Devices

Once a BLE device is discovered it shows up in the below method:

We can then access the Bluetooth Device name from the property.

From here the takes control. We can leverage its delegate method to get notified of the characteristics of peripheral devices.

We can also do stuff such as passing data using the function on the peripheral instance. The positive thing is that the user is aware!


Conclusion

Apple strives to provide transparency and improve user experience by bumping up Bluetooth security through the new iOS 13 permissions.
That’s it for this one. I hope you enjoyed.


Originally published at https://www.iowncode.com.

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

Anupam Chugh

Written by

I develop apps and write about them. Blogging weekly at iowncode.com

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade