Near Bluetooth devices using Swift 3.0

I am looking for a way to programmatically list any nearby Bluetooth devices (discoverable) that my device will detect. I could not find any information or tutorials regarding making this call in Swift 3.0. This QA article discusses finding these devices using Swift 1.0 and building in Xcode 6 rather than the latest version 8.

I did my best to try to make my code in syntax 3.0 of 1.0, but when I run the following code on the playground, nothing returns:

import Cocoa import IOBluetooth import PlaygroundSupport class BlueDelegate : IOBluetoothDeviceInquiryDelegate { func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) { aborted print("called") let devices = sender.foundDevices() for device : Any? in devices! { if let thingy = device as? IOBluetoothDevice { thingy.getAddress() } } } } var delegate = BlueDelegate() var inquiry = IOBluetoothDeviceInquiry(delegate: delegate) inquiry?.start() PlaygroundPage.current.needsIndefiniteExecution = true 
+6
ios swift bluetooth macos
source share
1 answer

Using IOBluetooth The Right Way

The following code works flawlessly in Xcode version 8.2.1 (8C1002), Swift 3.0. There are several lines that are not required, for example, the entire deviceInquiryStarted method.

Update: These usages still work with Xcode 9.2 (9B55) and Swift 4.

area

 import Cocoa import IOBluetooth import PlaygroundSupport class BlueDelegate : IOBluetoothDeviceInquiryDelegate { func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) { print("Inquiry Started...") //optional, but can notify you when the inquiry has started. } func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) { print("\(device.addressString!)") } func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) { //optional, but can notify you once the inquiry is completed. } } var delegate = BlueDelegate() var ibdi = IOBluetoothDeviceInquiry(delegate: delegate) ibdi?.updateNewDeviceNames = true ibdi?.start() PlaygroundPage.current.needsIndefiniteExecution = true 

Using Application Project

 import Cocoa import IOBluetooth import ... class BlueDelegate : IOBluetoothDeviceInquiryDelegate { func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) { print("Inquiry Started...") } func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) { print("\(device.addressString!)") } } //other classes here: //reference the following outside of any class: var delegate = BlueDelegate() var ibdi = IOBluetoothDeviceInquiry(delegate: delegate) //refer to these specifically inside of any class: ibdi?.updateNewDeviceNames = true ibdi?.start() //recommended under after an action-button press. 

Description

The problem I ran into was trying to access information, as the request was still in process.

When I accessed it, in many different cases, my playground hung up, and I was forced to exit Xcode.app and com.apple.CoreSimulator.CoreSimulatorService from Activity Monitor. I force myself to believe that this is just a mistake on the playground, only to find out that my application will work after the request is completed.

How the Apple API Reference :

Important Note. DO NOT run remote name queries on devices from delegate methods or while this object is in use. If you want to make your own requests for remote names on devices, execute them after you stop this object. If you do not consider this warning, you can block your process.

Which fully explained my problem. Instead of directly requesting IOBluetoothDevice information from the sender.foundDevices() method (which I suppose might not have been updated ..?) I just used the parameters built into the function to mention that it really was an IOBluetoothDevice object and just ask print this information.

Final note

I hope this Q / A I created helps others who need to use IOBluetooth in Swift. The lack of any tutorials and the large amount of obsolete Objective-C code made finding this information a very difficult task. I would like to thank @RobNapier for their support in trying to find the answer to this riddle at the beginning. I would also like to thank NotMyName for responding to my post on the Apple Developer Forums.

I will learn to use this on an iOS device sooner rather than later!

+5
source share

All Articles