hammerspoon2-docs
    Preparing search index...

    Class HSBluetoothDevice

    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.

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

    Constructors

    Properties

    address: string

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

    batteryLevel: number | null

    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: Record<string, number> | null

    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.

    classOfDevice: number

    The device's raw Bluetooth Class of Device value.

    isConnected: boolean

    Whether macOS currently has the device connected.

    isFavorite: boolean

    Whether the device is marked as a favorite in macOS.

    isPaired: boolean

    Whether the device is paired with this Mac.

    lastSeen: number | null

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

    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.

    name: string | null

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

    rssi: number | null

    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.

    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"].

    typeName: string

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

    Methods

    • Connects the device to the Mac, the same way macOS would, e.g. so headphones become available as an audio device.

      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

    • 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.

      Returns Promise<HSBluetoothDevice>

      A Promise resolving to this device once it's disconnected

    • 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.

      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

    • Remove a previously registered listener for this device's events.

      Parameters

      • event: "connected" | "disconnected"

        The event the listener was registered for

      • listener: (...args: any[]) => any

        The function originally passed to on

      Returns void

    • 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.

      Parameters

      • event: "connected" | "disconnected"

        The event to listen for

      • listener: () => void

        Called when the event occurs

      Returns void

      Throws an Error on failure; wrap calls in try/catch to handle it.

    • Register a listener that fires at most once for one of this device's events.

      Parameters

      • event: "connected" | "disconnected"

        The event to listen for

      • listener: () => void

        Called once, then automatically removed

      Returns void

      Throws an Error on failure; wrap calls in try/catch to handle it.

    • Reads the current signal strength of a connected device.

      Returns Promise<number>

      A Promise resolving to the signal strength in dBm, or rejecting if the device isn't connected

    • Returns a plain object describing this device, for JSON serialisation JSON.stringify() calls this automatically, so you rarely need to call it yourself.

      Returns Record<string, any>

      An object with the device's name, address, isPaired, isConnected, rssi, batteryLevel, batteryLevels, majorClass and minorClass