hammerspoon2-docs
    Preparing search index...

    Class HSBluetoothPeripheral

    A Bluetooth Low Energy (BLE) peripheral, found by hs.bluetooth.scan() or through a paired device's lowEnergyPeripheral().

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

    Call connect() before using its GATT services. A connected peripheral stays connected, and keeps delivering its events, until you call disconnect(), even if you drop your reference to it.

    async function watchSensor() {
    const [sensor] = await hs.bluetooth.scan({ seconds: 5, services: ["181A"] })
    await sensor.connect()
    const bytes = await sensor.readValue("181A", "2A6E")
    sensor.on("value", (service, characteristic, value) => console.log(characteristic + ": " + value))
    await sensor.subscribe("181A", "2A6E")
    }
    watchSensor()
    Index
    advertisement: Record<string, any> | null

    The peripheral's last advertisement, if a scan has seen one, otherwise null.

    The object has localName (string or null), isConnectable (boolean or null), txPower (number or null), serviceUUIDs (string[]), manufacturerData (Uint8Array or null) and serviceData (an object mapping service UUIDs to Uint8Array).

    batteryLevel: number | null

    The battery level last read by readBatteryLevel(), as a percentage, or null if it hasn't been read.

    identifier: string

    The peripheral's identifier, a UUID that macOS assigns in place of its Bluetooth address, which it doesn't reveal. It is stable on this Mac, but differs between Macs.

    isConnected: boolean

    Whether Hammerspoon is connected to the peripheral.

    lastSeen: number | null

    When the peripheral was last seen, in Unix seconds: in a scan, or while connected. null if unknown.

    name: string | null

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

    rssi: number | null

    The last known signal strength in dBm, or null if there is none. It is updated by hs.bluetooth.scan() and readRSSI().

    typeName: string

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

    • Connects Hammerspoon to the peripheral, so its GATT services can be used. It stays connected until disconnect() is called, even if you drop your reference to it.

      Returns Promise<HSBluetoothPeripheral>

      A Promise resolving to this peripheral once it's connected, or rejecting if the connection fails or takes longer than 20 seconds

    • Disconnects Hammerspoon from the peripheral. If macOS has its own connection to it (e.g. a paired keyboard), that connection stays.

      Returns Promise<HSBluetoothPeripheral>

      A Promise resolving to this peripheral once it's disconnected

    • Discovers the characteristics of one of a connected peripheral's GATT services.

      Parameters

      • service: string

        The service UUID: 16-bit ("180F"), 32-bit, or 128-bit

      Returns Promise<{ isNotifying: boolean; properties: string[]; uuid: string }[]>

      A Promise resolving to the service's characteristics. properties lists what each supports: "read", "write", "writeWithoutResponse", "notify", "indicate", "broadcast", "authenticatedSignedWrites" and "extendedProperties"

    • Discovers the GATT services of a connected peripheral.

      Returns Promise<string[]>

      A Promise resolving to the UUIDs of the peripheral's services, e.g. ["180A", "180F"]

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

      Parameters

      • event: "value" | "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 peripheral's events.

      Listeners receive:

      • "connected", "disconnected": no arguments, when Hammerspoon's connection to the peripheral opens or closes
      • "value": (service, characteristic, value), the UUIDs and the new value (a Uint8Array) of a characteristic you subscribed to. While subscribed, values fetched by readValue() arrive here too, since the peripheral sends both the same way

      A peripheral with listeners keeps working after you drop your reference to it.

      Parameters

      • event: "value" | "connected" | "disconnected"

        The event to listen for

      • listener: (
            service?: string,
            characteristic?: string,
            value?: Uint8Array<ArrayBufferLike>,
        ) => 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 peripheral's events.

      Parameters

      • event: "value" | "connected" | "disconnected"

        The event to listen for

      • listener: (
            service?: string,
            characteristic?: string,
            value?: Uint8Array<ArrayBufferLike>,
        ) => void

        Called once, then automatically removed

      Returns void

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

    • Reads the peripheral's battery level from its standard GATT Battery service. The peripheral must be connected.

      Returns Promise<number | null>

      A Promise resolving to the battery percentage, or null if the peripheral has no Battery service

    • Reads the current signal strength of a connected peripheral.

      Returns Promise<number>

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

    • Reads the value of a GATT characteristic of a connected peripheral. The service and characteristic are discovered first if needed.

      Parameters

      • service: string

        The service UUID, e.g. "180F"

      • characteristic: string

        The characteristic UUID, e.g. "2A19"

      Returns Promise<Uint8Array<ArrayBufferLike>>

      A Promise resolving to the characteristic's value

    • Subscribes to a GATT characteristic of a connected peripheral, so each new value is delivered as a "value" event (see on()).

      Parameters

      • service: string

        The service UUID

      • characteristic: string

        The characteristic UUID, which must support "notify" or "indicate"

      Returns Promise<void>

      A Promise resolving once the peripheral has started sending values

    • Returns a plain object describing this peripheral, for JSON serialisation

      JSON.stringify() calls this automatically, so you rarely need to call it yourself.

      Returns Record<string, any>

      An object with the peripheral's identifier, name, isConnected, rssi, lastSeen and batteryLevel

    • Stops a subscription started by subscribe().

      Parameters

      • service: string

        The service UUID

      • characteristic: string

        The characteristic UUID

      Returns Promise<void>

      A Promise resolving once the peripheral has stopped sending values

    • Writes the value of a GATT characteristic of a connected peripheral. Writes to one characteristic are sent in the order they're made.

      Parameters

      • service: string

        The service UUID

      • characteristic: string

        The characteristic UUID

      • value: string | number[] | ArrayBuffer | Uint8Array<ArrayBufferLike>

        The bytes to write. A string is written as UTF-8

      • Optionaloptions: { withoutResponse?: boolean }

        With withoutResponse: true, write without asking the peripheral to confirm it, which is faster but can't tell whether it arrived. Defaults to false

      Returns Promise<void>

      A Promise resolving once the peripheral has accepted the write (or, without a response, once it has been sent)