hs.bluetooth
ModuleInspect, connect to and scan for Bluetooth devices, and watch them connect and disconnect.
The module offers two kinds of object, because macOS identifies them in two different ways:
HSBluetoothDevice: a device macOS knows by its Bluetooth address, such as headphones, a keyboard or a mouse paired with this Mac. These have battery levels and system-wide connect/disconnect events.HSBluetoothPeripheral: a Bluetooth Low Energy (BLE) peripheral, found byscan(), which Hammerspoon can connect to itself to read and write its GATT characteristics. macOS gives these a UUIDidentifierinstead of revealing their address.
hs.bluetooth.pairedDevices().forEach(d => console.log(d.name + " " + d.address))
hs.bluetooth.connectedDevices()
.filter(d => d.batteryLevel !== null)
.forEach(d => console.log(d.name + ": " + d.batteryLevel + "%"))
hs.bluetooth.on('connected', d => console.log(d.name + " connected"))
hs.bluetooth.on('powerChanged', on => console.log("Bluetooth is " + (on ? "on" : "off")))
const nearby = await hs.bluetooth.scan({ seconds: 5 })
nearby.forEach(p => console.log((p.name ?? p.identifier) + " " + p.rssi + " dBm"))
macOS asks the user for Bluetooth permission the first time this module talks to Bluetooth.
Use hs.permissions.checkBluetooth() to see whether it has been granted.
Types
This module provides the following types:
Properties
hs.bluetooth.isInquiring
boolean
Whether a search for classic devices started by `inquiry()` is running.
Methods
hs.bluetooth.pairedDevices() -> HSBluetoothDevice[]
All devices paired with this Mac, connected or not.
Declaration
hs.bluetooth.pairedDevices() -> HSBluetoothDevice[]
Returns
HSBluetoothDevice[]
An array of `HSBluetoothDevice` objects
Example
hs.bluetooth.pairedDevices().forEach(d => console.log(d.name + " " + d.address))
hs.bluetooth.connectedDevices() -> HSBluetoothDevice[]
The paired devices macOS currently has connected.
Declaration
hs.bluetooth.connectedDevices() -> HSBluetoothDevice[]
Returns
HSBluetoothDevice[]
An array of `HSBluetoothDevice` objects
Example
hs.bluetooth.connectedDevices().forEach(d => console.log(d.name + " is connected"))
hs.bluetooth.device(address) -> HSBluetoothDevice
Looks up a device macOS knows by its Bluetooth address.
Declaration
hs.bluetooth.device(address) -> HSBluetoothDevice
Parameters
| Name | Type | Description |
|---|---|---|
| address | string | The device's address, as `"a4-83-e7-12-34-56"` or `"A4:83:E7:12:34:56"` |
Returns
HSBluetoothDevice
The `HSBluetoothDevice`, or `null` if macOS doesn't know a device with that address
Example
const keyboard = hs.bluetooth.device("a4-83-e7-12-34-56")
if (keyboard) console.log(keyboard.name + " battery: " + keyboard.batteryLevel + "%")
hs.bluetooth.connectedPeripherals() -> HSBluetoothPeripheral[]
The BLE peripherals Hammerspoon is connected to, with `HSBluetoothPeripheral.connect()`.
Declaration
hs.bluetooth.connectedPeripherals() -> HSBluetoothPeripheral[]
Returns
HSBluetoothPeripheral[]
An array of `HSBluetoothPeripheral` objects
Example
hs.bluetooth.connectedPeripherals().forEach(p => console.log(p.name))
hs.bluetooth.peripheral(identifier) -> HSBluetoothPeripheral
Looks up a BLE peripheral by its identifier.
Hammerspoon must have seen the peripheral in a scan, or Bluetooth must already be in use, for it to be found. This lookup doesn't start Bluetooth itself, so it never shows the permission prompt.
Declaration
hs.bluetooth.peripheral(identifier) -> HSBluetoothPeripheral
Parameters
| Name | Type | Description |
|---|---|---|
| identifier | string | The peripheral's `identifier`, a UUID |
Returns
HSBluetoothPeripheral
The `HSBluetoothPeripheral`, or `null` if it isn't known
Example
const sensor = hs.bluetooth.peripheral("5D7E2C4A-1B3F-4E8A-9C6D-0F1E2D3C4B5A")
if (sensor) await sensor.connect()
hs.bluetooth.scan(options) -> Promise<HSBluetoothPeripheral[]>
Scans for BLE peripherals nearby. Only one scan can run at a time.
Each peripheral found is also delivered to `'discovered'` listeners (see `on()`) as soon as it's seen.
Declaration
hs.bluetooth.scan(options) -> Promise<HSBluetoothPeripheral[]>
Parameters
| Name | Type | Description |
|---|---|---|
| options | [String: Any] | An object with any of: |
Returns
Promise<HSBluetoothPeripheral[]>
A Promise resolving to the peripherals found when the scan ends, or when `stopScan()` is called. Rejects if a scan is already running, the options are invalid, or Bluetooth is unavailable
Example
const nearby = await hs.bluetooth.scan({ seconds: 5 })
nearby.forEach(p => console.log((p.name ?? p.identifier) + " " + p.rssi + " dBm"))
const heartStraps = await hs.bluetooth.scan({ services: ["180D"] })
hs.bluetooth.stopScan() -> None
Ends a scan started by `scan()` early. Its Promise resolves with the peripherals found so far.
Declaration
hs.bluetooth.stopScan() -> None
Returns
None
Example
hs.bluetooth.once('discovered', p => hs.bluetooth.stopScan())
const [first] = await hs.bluetooth.scan({ seconds: 30 })
hs.bluetooth.inquiry(options) -> Promise<HSBluetoothDevice[]>
Searches for discoverable classic Bluetooth devices nearby, such as headphones in pairing mode. Only one search can run at a time.
This interrupts the audio of connected Bluetooth headphones while it runs.
Declaration
hs.bluetooth.inquiry(options) -> Promise<HSBluetoothDevice[]>
Parameters
| Name | Type | Description |
|---|---|---|
| options | [String: Any] | An object with `seconds` (number, default `10`, at most `255`): how long to search for |
Returns
Promise<HSBluetoothDevice[]>
A Promise resolving to the devices found when the search ends, or when `stopInquiry()` is called. Rejects if a search is already running, the options are invalid, or the search fails
Example
const found = await hs.bluetooth.inquiry({ seconds: 10 })
found.forEach(d => console.log(d.name + " " + d.address))
hs.bluetooth.stopInquiry() -> None
Ends a search started by `inquiry()` early. Its Promise resolves with the devices found so far.
Declaration
hs.bluetooth.stopInquiry() -> None
Returns
None
Example
hs.bluetooth.stopInquiry()
hs.bluetooth.on(event, listener) -> None
Register a listener for Bluetooth events.
Declaration
hs.bluetooth.on(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called with the event's device or peripheral, or for `"powerChanged"` the new power state |
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
try {
hs.bluetooth.on('connected', device => console.log(device.name + " connected"))
hs.bluetooth.on('disconnected', device => console.log(device.name + " disconnected"))
hs.bluetooth.on('powerChanged', on => console.log("Bluetooth is " + (on ? "on" : "off")))
} catch (err) {
console.error(err.message)
}
hs.bluetooth.off(event, listener) -> None
Remove a previously registered Bluetooth event listener.
Declaration
hs.bluetooth.off(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event the listener was registered for |
| listener | any | The function originally passed to `on` |
Returns
None
Example
const onConnected = device => console.log(device.name)
hs.bluetooth.on('connected', onConnected)
// later…
hs.bluetooth.off('connected', onConnected)
hs.bluetooth.once(event, listener) -> None
Register a listener that fires at most once for a Bluetooth event.
Declaration
hs.bluetooth.once(event, listener) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| event | any | The event to listen for |
| listener | any | Called the next time the event occurs, then automatically removed |
Returns
None
Throws
Throws an Error on failure; wrap calls in try/catch to handle it.
Example
try {
hs.bluetooth.once('connected', device => console.log("First connection: " + device.name))
} catch (err) {
console.error(err.message)
}