← Web Bluetooth: Make BluetoothRemoteGATTServer an EventTarget
v157 · enabled by default · inherited method
BluetoothRemoteGATTServer.dispatchEvent()
Synchronously dispatches an Event you created to a BluetoothRemoteGATTServer and runs the server’s matching listeners. The method is inherited unchanged from EventTarget. It became available on the server through Chromium CL 8363084 and Web Bluetooth PR #672. See the overview shipping note for which Chrome release contains it.
syntax
const notCanceled = server.dispatchEvent(event)
Declared in the DOM Standard as:
boolean dispatchEvent(Event event);
Source: DOM §2.7 EventTarget IDL (verbatim); receiver per Web Bluetooth PR #672 IDL.
parameters
dispatchEvent() takes one argument:
| Parameter | Type | Meaning |
|---|---|---|
event | Event | An initialized event object that is not currently being dispatched, for example new Event("app:sync") or new CustomEvent("app:sync", { detail }). bubbles and composed have no effect here because the server has no parent. cancelable controls whether preventDefault() can change the return value. |
return value
A boolean: false if event is cancelable and a listener called preventDefault() (ignored for passive listeners), otherwise true. During dispatch, event.target and event.currentTarget are the server and event.eventPhase is AT_TARGET (2). The DOM sets event.isTrusted to false for every event passed to dispatchEvent().
exceptions
"InvalidStateError"DOMExceptionif the event’s dispatch flag is set (it is being dispatched right now, for example re-dispatching the same object from inside its own listener), or if its initialized flag is not set (for example an object fromdocument.createEvent()withoutinitEvent()).TypeErrorifeventis not anEvent, or if called with athisthat is not anEventTarget.TypeError: server.dispatchEvent is not a functionon browsers without the inheritance change.- Exceptions thrown by listeners are reported (
window.onerror), not thrown out ofdispatchEvent(). The remaining listeners still run.
context
Callable on any BluetoothRemoteGATTServer in a secure-context Window, connected or not. You get the receiver from device.gatt. Dispatching sends nothing to the Bluetooth device: it only runs JavaScript listeners in this page.
lifecycle
- Dispatch is synchronous: every matching listener has run by the time
dispatchEvent()returns. - The event path contains only the server, because the server is not in the Bluetooth tree. Listeners on
deviceornavigator.bluetoothnever see it, even withbubbles: true. - After dispatch the event can be dispatched again, to the server or to any other target.
- UA-fired events such as
maxwritewithoutresponsesizechangedare queued as tasks on the Bluetooth task source. Your owndispatchEvent()calls run inline.
examples
const server = device.gatt;
server.addEventListener("app:handshake", (event) => {
console.log(event.target === server); // true
console.log(event.isTrusted); // false: script-dispatched
if (!event.detail.ok) event.preventDefault();
});
device.addEventListener("app:handshake", () => {
console.log("never runs: the server has no parent");
});
await server.connect();
const accepted = server.dispatchEvent(
new CustomEvent("app:handshake", { cancelable: true, bubbles: true, detail: { ok: true } }),
);
console.log(accepted); // true
Source: written for this page against the DOM dispatchEvent() and Bluetooth tree definitions; not executed against real hardware.
browser compatibility
| Browser | On BluetoothRemoteGATTServer | EventTarget.dispatchEvent (BCD) |
|---|---|---|
| Chrome / Chrome Android | Listed for 157 (interim, ChromeStatus); code merged after the M159 branch point, so the first release is unknown | 4 |
| Edge | Unknown (not separately reported) | 12 |
| Firefox | No (no Web Bluetooth) | 2 |
| Safari | No (no Web Bluetooth) | 3.1 |
BCD has no key for this inherited member on BluetoothRemoteGATTServer. The receiver column is interim data from ChromeStatus, and the method column is from BCD api/EventTarget.json (fetched 2026-11-15).
security and privacy
Script-dispatched events always have isTrusted set to false, so code can tell them apart from events fired by the browser. Third-party script running in the same page can dispatch fake events on a server it can reach, so don’t treat event data as authoritative: read authoritative state from the server’s attributes. Dispatching an event sends no Bluetooth traffic.