Saturday, March 14, 2015

How to use Classic Bluetooth on iOS

Something like classic Bluetooth sounds like it should be simple enough to use on iOS. I recently falsely stated is was all there and ready to go in the Core Bluetooth APIs. Although Bluetooth LE is fully accessible, classic Bluetooth is not. But there are a few options developers can explore if they need to use Classic Bluetooth within an application.

The first thing to note is, pairing with a Classic Bluetooth device is handled at the OS level, so there isn’t any way we can start the connection or pairing process from within an app.

So assuming a device is already paired, we have a few options:

 

External Accessory Framework

https://developer.apple.com/library/prerelease/ios/documentation/ExternalAccessory/Reference/ExternalAccessoryFrameworkReference/index.html

The External Accessory Framework is used to communicate with devices that are Apple MFi certified. MFi stands for “Made For i(phone/pad/pod)”. These devices can be physically connected, or connected over Bluetooth. If your device is MFi certified, this is the way to go.

As developers, you’re not required to join the MFi Program, that’s only necessary if you intend to develop hardware.

And of course, the External Accessory Framework APIs will not work with non-MFi devices.

 

Bluetooth Manager Framework

The Bluetooth Manager Framework sounds like the perfect solution to communicate with non-MFi Bluetooth Devices. Unfortunately, it’s a private API, meaning if you want to get your app published on the App Store, it can’t be used.

However, it can do things like scan for local devices, manage connections, and communicate with paired devices. Because it’s a private API, you won’t find any official Apple documentation but a quick Google search should get you going. There’s a good Objective-C tutorial here:

https://github.com/michaeldorner/BeeTee

 

GameKit

If you’re looking to connect two iOS devices, GameKit has a straight forward solution. It’s a simple matter of starting up GameKit, searching for local devices, connect, and then send/receive data. However, this is only for iOS device-to-device communication:

https://developer.apple.com/library/ios/documentation/GameKit/Reference/GameKit_Collection/index.html

 

BTstack

If you’re willing to Jailbreak your device and write applications that will never make it onto the App Store, you can make use of BTstack. The packages are available on Cydia. I’ve personally never used it but you can find more information here:

https://code.google.com/p/btstack/wiki/GettingStarted

 

Bluetooth 4.0

And finally, if your hardware is Bluetooth 4.0 compatible, you should probably consider moving away from Bluetooth Classic and using Bluetooth LE. Although BLE supports “casual” connections, it can also pair devices and establish secure connections.

This is also the path that Apple recommends, it’s well documented, stable on devices made in the last 2-3 years (iPhone 5 and up, iPad 3 and up), and fully defined in the CoreBluetooth APIs:

https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CoreBluetooth_Framework/