hammerspoon2-docs
    Preparing search index...

    Namespace bluetooth

    Inspect, 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 by scan(), which Hammerspoon can connect to itself to read and write its GATT characteristics. macOS gives these a UUID identifier instead 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")))

    hs.bluetooth.scan({ seconds: 5 })
    .then(nearby => 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.

    isInquiring
    isScanning
    powered
    connectedDevices
    connectedPeripherals
    device
    inquiry
    off
    on
    once
    pairedDevices
    peripheral
    scan
    stopInquiry
    stopScan