← Web Bluetooth: Make BluetoothRemoteGATTServer an EventTarget
v157 · enabled by default · inherited method
BluetoothRemoteGATTServer.addEventListener()
Registers a listener for events of a given type on a BluetoothRemoteGATTServer. The method is inherited unchanged from EventTarget. It became available on the server when Chromium CL 8363084 made the interface inherit from EventTarget, as specified by Web Bluetooth PR #672. See the overview shipping note for which Chrome release contains it.
syntax
server.addEventListener(type, callback)
server.addEventListener(type, callback, options)
server.addEventListener(type, callback, useCapture)
Declared in the DOM Standard as:
undefined addEventListener(DOMString type, EventListener? callback, optional (AddEventListenerOptions or boolean) options = {});
Source: DOM §2.7 EventTarget IDL (verbatim); receiver per Web Bluetooth PR #672 IDL.
parameters
addEventListener() takes three arguments. The third can be an options dictionary or a boolean:
| Parameter | Type | Meaning |
|---|---|---|
type | DOMString | The event type to listen for. The spec defines one type for this target: "maxwritewithoutresponsesizechanged", which ships as a separate feature. Any other string is accepted and only matches events you dispatch yourself. |
callback | EventListener? | A function, or an object with a handleEvent(event) method. null is accepted and makes the call do nothing. |
options.capture | boolean, default false | Part of the listener’s identity. The server has no parent, so there is no capture or bubble path and capture listeners run in the at-target phase. Passing a boolean instead of a dictionary sets only capture. |
options.once | boolean, default false | Remove the listener automatically after its first invocation. |
options.passive | boolean, no default | If true, a call to preventDefault() in the listener is ignored. |
options.signal | AbortSignal | When the signal aborts, the listener is removed. If it is already aborted, the listener is not added. |
return value
addEventListener() returns undefined. The effect is that an entry is added to the server’s event listener list. If an entry with the same type, callback and capture is already registered, the call does nothing, so attaching the same function twice does not make it run twice.
exceptions
- The method itself does not throw for any
typestring or anullcallback. TypeErrorifcallbackis not callable and not an object, or ifoptions.signalis not anAbortSignal(Web IDL conversion).TypeErrorif called with athisthat is not anEventTarget, for exampleEventTarget.prototype.addEventListener.call({}, …).- On a browser without the inheritance change,
server.addEventListenerisundefinedand calling it throwsTypeError: server.addEventListener is not a function. CheckEventTarget.prototype.isPrototypeOf(BluetoothRemoteGATTServer.prototype)first. - An exception thrown by your listener is reported to the global error handler (
window.onerror). It does not propagate to the code that caused the event.
context
Available on any BluetoothRemoteGATTServer in a secure-context Window, whether or not the server is connected. You get the receiver from device.gatt, after requestDevice() (user gesture and chooser) or getDevices(). The gatt getter returns null in the spec unless the origin may use GATT on that device. The document must be allowed the "bluetooth" Permissions Policy feature. No extra permission is needed to add a listener.
[Exposed=Window, SecureContext]); Web Bluetooth gatt getter.
lifecycle
device.gattis the same object for the life of theBluetoothDevice, so a listener survivesdisconnect()andconnect(). Add it once, not on every reconnect.- The server is not in the Bluetooth tree. A listener on the server receives only events targeted at the server: it never sees
gattserverdisconnected(fired at the device) orcharacteristicvaluechanged(fired at characteristics). - A listener stops running when it is removed with
removeEventListener(), when itssignalaborts, after one run ifonceis set, or when the document is torn down. - Listeners added during a dispatch do not run for that same dispatch.
examples
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: ["heart_rate"] }],
});
const server = device.gatt;
if (EventTarget.prototype.isPrototypeOf(BluetoothRemoteGATTServer.prototype)) {
const controller = new AbortController();
// The connection-level event designed for this target (separate feature).
server.addEventListener("maxwritewithoutresponsesizechanged", () => {
console.log("max write size:", server.maxWriteWithoutResponseSize);
}, { signal: controller.signal });
// Disconnects are still fired at the device: stop listening there.
device.addEventListener("gattserverdisconnected", () => controller.abort(), { once: true });
}
await server.connect();
Source: written for this page against the DOM addEventListener() and PR #672 IDL; not executed against real hardware.
browser compatibility
| Browser | On BluetoothRemoteGATTServer | EventTarget.addEventListener (BCD) |
|---|---|---|
| Chrome / Chrome Android | Listed for 157 (interim, ChromeStatus); code merged after the M159 branch point, so the first release is unknown | 1 (options object 49) |
| Edge | Unknown (not separately reported) | 12 |
| Firefox | No (no Web Bluetooth) | 1 |
| Safari | No (no Web Bluetooth) | 1 |
BCD has no key for this inherited member on BluetoothRemoteGATTServer. The receiver column is interim data from ChromeStatus. The method column is from BCD api/EventTarget.json, and interface support is in BCD api/BluetoothRemoteGATTServer.json (both fetched 2026-11-15).
security and privacy
Adding a listener gives no access to device data and does not connect to the device or keep it connected. Any UA-fired event the listener receives is one the page could already observe through the server’s attributes. Web Bluetooth’s chooser, secure-context requirement, service allowlist, blocklist and Permissions Policy are unchanged.
Source: ChromeStatus API record (security risks).