API Docs

A Bluetooth device macOS knows: one paired with this Mac, or one found by hs.bluetooth.inquiry().

Obtain devices from the hs.bluetooth module; do not instantiate them directly. A device is identified by its address, and the same device is returned as the same object for as long as you hold on to it.

Bluetooth Low Energy (BLE) peripherals found by hs.bluetooth.scan() are HSBluetoothPeripheral objects instead. A paired device that also speaks BLE can give you its peripheral with lowEnergyPeripheral(), e.g. to read its GATT characteristics.

Battery levels

hs.bluetooth.connectedDevices().forEach(d => {
    if (d.batteryLevel !== null) console.log(d.name + ": " + d.batteryLevel + "%")
})

Properties

typeName

string
The type name for JavaScript introspection. Always `"HSBluetoothDevice"`.

name

string
The device's name, or `null` if it hasn't reported one.

address

string
The device's Bluetooth address, in the form `xx-xx-xx-xx-xx-xx`.

isPaired

boolean
Whether the device is paired with this Mac.

isConnected

boolean
Whether macOS currently has the device connected.

isFavorite

boolean
Whether the device is marked as a favorite in macOS.

classOfDevice

number
The device's raw Bluetooth Class of Device value.

majorClass

number
The major device class, e.g. `4` for audio devices or `5` for peripherals such as keyboards and mice.

minorClass

number
The minor device class, whose meaning depends on `majorClass`.

serviceClasses

number
The major service classes the device advertises, as a bitmask.

serviceNames

string[]
The names of the device's classic Bluetooth (SDP) services, e.g. `["Audio Sink", "AVRCP Controller"]`.

rssi

NSNumber
The signal strength in dBm that macOS last measured for the device, or `null` if there is none. Call `readRSSI()` for a fresh reading from a connected device.

lastSeen

NSNumber
When the device was last used or found by an inquiry, in Unix seconds, or `null` if unknown.

batteryLevel

NSNumber
The device's battery level as a percentage, or `null` if macOS doesn't know it. This is what macOS itself reports: for Apple's Magic accessories from the system, and for AirPods and Beats from what macOS last heard from them. For earbuds it is the lower of the two buds; see `batteryLevels` for each one. For a device that reports its battery over BLE instead, read it with `lowEnergyPeripheral()` and `readBatteryLevel()`.

batteryLevels

[String: Int]
The individual battery levels of a device with several batteries, such as earbuds and their case, or `null` if it has only one (or none is known). The object has any of `left`, `right` and `case`, each a percentage.

Methods

connect() -> Promise<HSBluetoothDevice>

Connects the device to the Mac, the same way macOS would, e.g. so headphones become available as an audio device.
connect() -> Promise<HSBluetoothDevice>
Promise<HSBluetoothDevice>
A Promise resolving to this device once it's connected, or rejecting if the connection fails or takes longer than 20 seconds
const headphones = hs.bluetooth.pairedDevices().find(d => d.name === "My Headphones")
await headphones.connect()

disconnect() -> Promise<HSBluetoothDevice>

Disconnects the device from the Mac. This ends macOS's own connection, not just Hammerspoon's, as if you had disconnected it in System Settings.
disconnect() -> Promise<HSBluetoothDevice>
Promise<HSBluetoothDevice>
A Promise resolving to this device once it's disconnected
await headphones.disconnect()

readRSSI() -> Promise<number>

Reads the current signal strength of a connected device.
readRSSI() -> Promise<number>
Promise<number>
A Promise resolving to the signal strength in dBm, or rejecting if the device isn't connected
console.log(await device.readRSSI() + " dBm")

lowEnergyPeripheral() -> Promise<HSBluetoothPeripheral | null>

Finds the BLE peripheral of a connected device that also speaks Bluetooth Low Energy, such as a Magic Keyboard or a heart-rate strap, so you can use its GATT services. macOS doesn't link a paired device to its BLE side, so this looks for the BLE peripheral connected to the Mac with the same name. It rejects rather than guess if several have that name, and resolves to `null` if none does (e.g. the device doesn't use BLE, or it was renamed after pairing). The peripheral still needs `connect()` before its GATT methods can be used.
lowEnergyPeripheral() -> Promise<HSBluetoothPeripheral | null>
Promise<HSBluetoothPeripheral | null>
A Promise resolving to the device's peripheral, or `null` if it has none. Rejects if the device isn't connected, several peripherals match, or Bluetooth is unavailable
const keyboard = hs.bluetooth.connectedDevices().find(d => d.name === "Magic Keyboard")
const peripheral = await keyboard.lowEnergyPeripheral()
if (peripheral) {
    await peripheral.connect()
    console.log(await peripheral.discoverServices())
}

on(event, listener) -> None

Register a listener for this device's events. Listeners for `"connected"` and `"disconnected"` are called with no arguments whenever macOS connects or disconnects the device. A device with listeners keeps working after you drop your reference to it.
on(event, listener) -> None
Name Type Description
event string The event to listen for
listener function Called when the event occurs
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    keyboard.on('disconnected', () => console.log(keyboard.name + " went away"))
} catch (err) {
    console.error(err.message)
}

off(event, listener) -> None

Remove a previously registered listener for this device's events.
off(event, listener) -> None
Name Type Description
event string The event the listener was registered for
listener function The function originally passed to `on`
None
keyboard.off('disconnected', myListener)

once(event, listener) -> None

Register a listener that fires at most once for one of this device's events.
once(event, listener) -> None
Name Type Description
event string The event to listen for
listener function Called once, then automatically removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    headphones.once('connected', () => console.log("Headphones are back"))
} catch (err) {
    console.error(err.message)
}

toJSON() -> {[key: string]: any}

Returns a plain object describing this device, for JSON serialisation `JSON.stringify()` calls this automatically, so you rarely need to call it yourself.
toJSON() -> {[key: string]: any}
{[key: string]: any}
An object with the device's `name`, `address`, `isPaired`, `isConnected`, `rssi`, `batteryLevel`, `batteryLevels`, `majorClass` and `minorClass`
console.log(JSON.stringify(hs.bluetooth.pairedDevices(), null, 2))