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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was having issues with
IOBluetoothDevice.pairedDevices()
always returning an empty array but I found out that it is because we need the Bluetooth entitlement:This on macOS 13.4 (Ventura)