dylan dylan - 1 year ago 130
Swift Question

How do I enable Bluetooth on button press on a OS X Application?

Is it possible to enable Bluetooth (with Discoverability on), using a button on macOS? Or enable it when an application opens? I've looked around online for a solution but everything directs towards iPhone and iOS Development rather than anything on the Mac side. Everything that is geared towards iOS also states that it is not possible on the mobile devices, but it is possible to display an alert notifying the user to turn on their Bluetooth to use their accessories. Is any alert type possible?

Edit: The closest I could get to this was opening the Bluetooth pane of the System Preferences programmatically.

Answer Source

It's a private API.

Add the following to your bridging header:

void IOBluetoothPreferenceSetControllerPowerState(int);

And call it with 1 to enable or 0 to disable:

func setBluetooth(on: Bool) {
    IOBluetoothPreferenceSetControllerPowerState(on ? 1 : 0)

setBluetooth(on: true)

Don't forget to import IOBluetooth in your Swift file.

To get the current status use int IOBluetoothPreferenceGetControllerPowerState();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download