Created
March 31, 2017 01:53
-
-
Save jamesmartin/9847466aba513de9a77507b56e712296 to your computer and use it in GitHub Desktop.
Swift example of iterating over all known Bluetooth devices on macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import IOBluetooth | |
// See https://developer.apple.com/reference/iobluetooth/iobluetoothdevice | |
// for API details. | |
class BluetoothDevices { | |
func pairedDevices() { | |
print("Bluetooth devices:") | |
guard let devices = IOBluetoothDevice.pairedDevices() else { | |
print("No devices") | |
return | |
} | |
for item in devices { | |
if let device = item as? IOBluetoothDevice { | |
print("Name: \(device.name)") | |
print("Paired?: \(device.isPaired())") | |
print("Connected?: \(device.isConnected())") | |
} | |
} | |
} | |
} | |
var bt = BluetoothDevices() | |
bt.pairedDevices() |
I believe IOBluetooth should be able to handle both normal and LE on the Mac, I wondered if there was an easy way to get this to work?
Good question. Unfortunately I haven't needed to detect any bluetooth devices using this code snippet lately. I expect, given it's been six years, that the state of the art has moved on. I suggest hitting the latest Apple developer reference docs. Good luck! 🤞
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi James, it seems to detect my 'normal' Bluetooth devices perfectly. Any Bluetooth LE devices are not detected, for example my Apple mouse which is normal bluetooth is picked up, but my Microsoft Modern Wireless Mobile Mouse is not.
I believe IOBluetooth should be able to handle both normal and LE on the Mac, I wondered if there was an easy way to get this to work?