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
number
The major device class, e.g. `4` for audio devices or `5` for peripherals such as keyboards and mice.
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.
Declaration
connect() -> Promise<HSBluetoothDevice>
Returns
Promise<HSBluetoothDevice>
A Promise resolving to this device once it's connected, or rejecting if the connection fails or takes longer than 20 seconds
Example
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.
Declaration
disconnect() -> Promise<HSBluetoothDevice>
Returns
Promise<HSBluetoothDevice>
A Promise resolving to this device once it's disconnected
Example
await headphones.disconnect()
readRSSI() -> Promise<number>
Reads the current signal strength of a connected device.
Declaration
readRSSI() -> Promise<number>
Returns
Promise<number>
A Promise resolving to the signal strength in dBm, or rejecting if the device isn't connected
Example
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.
Declaration
lowEnergyPeripheral() -> Promise<HSBluetoothPeripheral | null>
Returns
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
Example
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.
Declaration
on(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | string | The event to listen for |
| listener | function | Called when the event occurs |
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
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.
Declaration
off(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | string | The event the listener was registered for |
| listener | function | The function originally passed to `on` |
Returns
None
Example
keyboard.off('disconnected', myListener)
once(event, listener) -> None
Register a listener that fires at most once for one of this device's events.
Declaration
once(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | string | The event to listen for |
| listener | function | Called once, then automatically removed |
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
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.
Declaration
toJSON() -> {[key: string]: any}
Returns
{[key: string]: any}
An object with the device's `name`, `address`, `isPaired`, `isConnected`, `rssi`, `batteryLevel`, `batteryLevels`, `majorClass` and `minorClass`
Example
console.log(JSON.stringify(hs.bluetooth.pairedDevices(), null, 2))