API Docs

Control and query Wi-Fi interfaces, scan for networks, and watch for Wi-Fi events.

Built on CoreWLAN. Fields that reveal network identity — ssid, bssid, countryCode, and BSSIDs inside scan results — are only populated once Location Services is enabled and the user has authorized this app; call hs.permissions.requestLocation() first (the same gate hs.location uses). Without authorization these fields are null/omitted, not errors.

Watching for Wi-Fi events

hs.wifi.on('ssidChange', info => console.log("SSID changed on " + info.interface))

Object Types

Plain JavaScript objects that this module's methods and properties take or return:

HSWifiChannel

A Wi-Fi channel

Field Type Description
band "2GHz" | "5GHz" | "6GHz" | "unknown" The channel's band
number number The channel number
width "20MHz" | "40MHz" | "80MHz" | "160MHz" | "unknown" The channel's width

HSWifiNetwork

A Wi-Fi network found by a scan

Field Type Description
ssid optional string The network's name. Absent for a hidden network
bssid optional string The access point's MAC address. Absent without Location Services authorization
rssi number Signal strength in dBm
noise number Noise in dBm
ibss boolean Whether it's an ad-hoc (IBSS) network
countryCode optional string The network's country code. Absent without Location Services authorization
beaconInterval number The beacon interval, in milliseconds
security string[] The kinds of security the network supports
phyModes string[] The PHY modes the network supports, e.g. "AX" for 802.11ax
wlanChannel HSWifiChannel The network's channel
informationElementData number[] The raw beacon/probe-response data, as byte values (0-255) rather than a string, since it can contain sequences that are unsafe to render as text

Event Types

The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:

HSWifiEvent

An event name for hs.wifi.on(), hs.wifi.off() and hs.wifi.once()

Event Description
"powerChange" The interface was turned on or off
"ssidChange" The interface joined or left a network
"bssidChange" The interface moved to a different access point
"countryCodeChange" The interface's country code changed
"linkChange" The interface's link went up or down
"linkQualityChange" The link's signal strength or transmit rate changed
"modeChange" The interface's operating mode changed, e.g. to or from an ad-hoc (IBSS) network
"scanCacheUpdated" The interface's cached scan results were updated

Properties

This module has no properties.

Methods

hs.wifi.interfaces() -> string[]

Returns the names of all Wi-Fi interfaces attached to the system (e.g. ["en0"]).

hs.wifi.interfaces() -> string[]
string[]
an array of interface name strings
console.log(hs.wifi.interfaces())

hs.wifi.interfaceDetails(interface) -> object | null

Returns detailed information about a Wi-Fi interface.

This is safe to call frequently. The interface's cached scan results and its configuration are available from cachedScanResults() and configuration(), which are kept separate because fetching them can leak memory inside macOS.

active reports whether the interface is in use, and does not need Location Services authorization, so it's a way to check that Wi-Fi is connected without that permission.

hs.wifi.interfaceDetails(interface) -> object | null
Name Type Description
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
object | null
The interface's details, or null if the interface doesn't exist. ssid/bssid/countryCode/wlanChannel may be absent without Location Services authorization
Field Type Description
interface optional string The interface's name
active boolean Whether the interface is in use
power boolean Whether the interface is powered on
ssid optional string The name of the network it's on
bssid optional string The MAC address of the access point it's using
security string The security of the network it's on
interfaceMode string e.g. "Station"
activePHYMode string e.g. "AX" for 802.11ax
rssi number Signal strength in dBm
noise number Noise in dBm
transmitRate number The transmit rate, in Mbps
transmitPower number The transmit power, in mW
countryCode optional string The country code the interface is using
hardwareAddress optional string The interface's MAC address
wlanChannel optional HSWifiChannel The channel it's on
supportedChannels HSWifiChannel[] The channels the interface supports
const info = hs.wifi.interfaceDetails()
if (info) console.log(info.ssid + " on channel " + info.wlanChannel.number)

hs.wifi.cachedScanResults(interface) -> HSWifiNetwork[] | null

Returns the networks an interface found in its most recent scan, without starting a new scan.

Avoid calling this frequently: it has been seen to leak memory each time macOS refreshes the scan cache, so polling it in a long-running configuration can grow Hammerspoon's memory use. Use scanNetworks() for a fresh scan, or on('scanCacheUpdated', ...) to learn when the cache changes.

hs.wifi.cachedScanResults(interface) -> HSWifiNetwork[] | null
Name Type Description
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
HSWifiNetwork[] | null
The networks, or null if the interface doesn't exist
const networks = hs.wifi.cachedScanResults() ?? []
networks.forEach(n => console.log(n.ssid + " (" + n.rssi + " dBm)"))

hs.wifi.configuration(interface) -> object | null

Returns an interface's configuration: its remembered network profiles and administrator settings.

Avoid calling this frequently: macOS leaks memory on every call, so polling this function in a long-running configuration grows Hammerspoon's memory use.

hs.wifi.configuration(interface) -> object | null
Name Type Description
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
object | null
The interface's configuration, or null if the interface doesn't exist
Field Type Description
rememberJoinedNetworks boolean Whether networks the interface joins are remembered
requireAdministratorForAssociation boolean Whether changing networks needs an administrator
requireAdministratorForIBSSMode boolean Whether creating an ad-hoc network needs an administrator
requireAdministratorForPower boolean Whether turning Wi-Fi on or off needs an administrator
networkProfiles object[] The remembered networks. Their ssid may be absent without Location Services authorization
ssid optional string
security string
const config = hs.wifi.configuration()
if (config) console.log(config.networkProfiles.length + " remembered networks")

hs.wifi.currentNetwork(interface) -> string

Returns the SSID of the network currently joined on an interface.

hs.wifi.currentNetwork(interface) -> string
Name Type Description
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
string
the SSID string, or null if not joined to a network (or Location Services is not authorized)
console.log(hs.wifi.currentNetwork())

hs.wifi.setPower(state, interface) -> boolean

Turns a Wi-Fi interface on or off.

hs.wifi.setPower(state, interface) -> boolean
Name Type Description
state boolean true to power the interface on, false to power it off
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
boolean
true if the power state was changed successfully, false if the interface doesn't exist or the change failed (see the Console for the reason)
hs.wifi.setPower(false) // turn Wi-Fi off

hs.wifi.disassociate(interface) -> None

Disconnects an interface from its current network.

hs.wifi.disassociate(interface) -> None
Name Type Description
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
None
hs.wifi.disassociate()

hs.wifi.associate(ssid, passphrase, interface) -> Promise<boolean>

Scans for a network by SSID and joins it. Enterprise networks are not supported.

This can take several seconds; it runs off the main thread so it does not block the app.

hs.wifi.associate(ssid, passphrase, interface) -> Promise<boolean>
Name Type Description
ssid string the SSID of the network to join
passphrase optional string the network passphrase; required for WEP/WPA/WPA2/WPA3 Personal networks
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
Promise<boolean>
A Promise that resolves true if joined successfully, false if no network with that SSID was found, or rejects if the interface doesn't exist or the association attempt fails
hs.wifi.associate("MyNetwork", "hunter2")
    .then(joined => console.log(joined ? "Joined" : "Network not found"))
    .catch(err => console.log("Error: " + err))

hs.wifi.scanNetworks(interface) -> Promise<HSWifiNetwork[]>

Scans for visible Wi-Fi networks.

This can take a few seconds; it runs off the main thread so it does not block the app.

hs.wifi.scanNetworks(interface) -> Promise<HSWifiNetwork[]>
Name Type Description
interface optional string the interface name as returned by interfaces(); omit for the system's default Wi-Fi interface
Promise<HSWifiNetwork[]>
A Promise that resolves to the networks found, or rejects if the interface doesn't exist or the scan fails
hs.wifi.scanNetworks().then(networks => {
    networks.forEach(n => console.log(n.ssid + " (" + n.rssi + " dBm)"))
})

hs.wifi.on(event, listener) -> None

Register a listener for a Wi-Fi interface event. Info keys by event:

  • "ssidChange", "bssidChange", "countryCodeChange", "linkChange", "modeChange", "powerChange", "scanCacheUpdated": interface: string
  • "linkQualityChange": interface: string, rssi: number, transmitRate: number
hs.wifi.on(event, listener) -> None
Name Type Description
event HSWifiEvent The event to listen for
listener (info: {interface: string, rssi?: number, transmitRate?: number}) => void Called with an info object describing the event; see the keys listed above
Event Description
"powerChange" The interface was turned on or off
"ssidChange" The interface joined or left a network
"bssidChange" The interface moved to a different access point
"countryCodeChange" The interface's country code changed
"linkChange" The interface's link went up or down
"linkQualityChange" The link's signal strength or transmit rate changed
"modeChange" The interface's operating mode changed, e.g. to or from an ad-hoc (IBSS) network
"scanCacheUpdated" The interface's cached scan results were updated
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.wifi.on('ssidChange', info => console.log("SSID changed on " + info.interface))
    hs.wifi.on('linkQualityChange', info => console.log(info.interface + ": " + info.rssi + " dBm"))
} catch (err) {
    console.error(err.message)
}

hs.wifi.off(event, listener) -> None

Remove a previously registered Wi-Fi event listener.

hs.wifi.off(event, listener) -> None
Name Type Description
event HSWifiEvent The event the listener was registered for
listener (info: {interface: string, rssi?: number, transmitRate?: number}) => void The function originally passed to on
Event Description
"powerChange" The interface was turned on or off
"ssidChange" The interface joined or left a network
"bssidChange" The interface moved to a different access point
"countryCodeChange" The interface's country code changed
"linkChange" The interface's link went up or down
"linkQualityChange" The link's signal strength or transmit rate changed
"modeChange" The interface's operating mode changed, e.g. to or from an ad-hoc (IBSS) network
"scanCacheUpdated" The interface's cached scan results were updated
None
const onSsidChange = info => console.log(info)
hs.wifi.on('ssidChange', onSsidChange)
// later…
hs.wifi.off('ssidChange', onSsidChange)

hs.wifi.once(event, listener) -> None

Register a listener that fires at most once for a Wi-Fi interface event.

hs.wifi.once(event, listener) -> None
Name Type Description
event HSWifiEvent The event to listen for
listener (info: {interface: string, rssi?: number, transmitRate?: number}) => void Called once, then automatically removed
Event Description
"powerChange" The interface was turned on or off
"ssidChange" The interface joined or left a network
"bssidChange" The interface moved to a different access point
"countryCodeChange" The interface's country code changed
"linkChange" The interface's link went up or down
"linkQualityChange" The link's signal strength or transmit rate changed
"modeChange" The interface's operating mode changed, e.g. to or from an ad-hoc (IBSS) network
"scanCacheUpdated" The interface's cached scan results were updated
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.wifi.once('ssidChange', info => console.log("First SSID change: " + info.interface))
} catch (err) {
    console.error(err.message)
}