HSBluetoothDevice
TypeA 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
majorClass
The major device class, e.g. 4 for audio devices or 5 for peripherals such as keyboards and mice.
serviceNames
The names of the device's classic Bluetooth (SDP) services, e.g. ["Audio Sink", "AVRCP Controller"].
rssi
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
When the device was last used or found by an inquiry, in Unix seconds, or null if unknown.
batteryLevel
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
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>
const headphones = hs.bluetooth.pairedDevices().find(d => d.name === "My Headphones")
headphones.connect().then(() => console.log("Headphones connected"))
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>
headphones.disconnect().then(() => console.log("Headphones disconnected"))
readRSSI() -> Promise<number>
Reads the current signal strength of a connected device.
readRSSI() -> Promise<number>
device.readRSSI().then(rssi => console.log(rssi + " 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>
null if it has none. Rejects if the device isn't connected, several peripherals match, or Bluetooth is unavailableasync function showKeyboardServices() {
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())
}
}
showKeyboardServices()
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 |
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 |
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 |
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}
name, address, isPaired, isConnected, rssi, batteryLevel, batteryLevels, majorClass and minorClassconsole.log(JSON.stringify(hs.bluetooth.pairedDevices(), null, 2))