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...")
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!)") } }
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!
dylan
source share