// Type definitions for Web Bluetooth // Project: https://webbluetoothcg.github.io/web-bluetooth/ // Definitions by: Uri Shaked // Xavier Lozinguez // Rob Moran // David Bjerremose // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped type BluetoothServiceUUID = number | string; type BluetoothCharacteristicUUID = number | string; type BluetoothDescriptorUUID = number | string; type BluetoothManufacturerData = Map; type BluetoothServiceData = Map; interface BluetoothDataFilter { readonly dataPrefix?: BufferSource | undefined; readonly mask?: BufferSource | undefined; } interface BluetoothManufacturerDataFilter extends BluetoothDataFilter { companyIdentifier: number; } interface BluetoothServiceDataFilter extends BluetoothDataFilter { service: BluetoothServiceUUID; } interface BluetoothLEScanFilter { readonly name?: string | undefined; readonly namePrefix?: string | undefined; readonly services?: BluetoothServiceUUID[] | undefined; readonly manufacturerData?: BluetoothManufacturerDataFilter[] | undefined; readonly serviceData?: BluetoothServiceDataFilter[] | undefined; } interface BluetoothLEScanOptions { readonly filters?: BluetoothLEScanFilter[] | undefined; readonly keepRepeatedDevices?: boolean | undefined; readonly acceptAllAdvertisements?: boolean | undefined; } interface BluetoothLEScan extends BluetoothLEScanOptions { active: boolean; stop: () => void; } type RequestDeviceOptions = { filters: BluetoothLEScanFilter[]; optionalServices?: BluetoothServiceUUID[] | undefined; optionalManufacturerData?: number[] | undefined; } | { acceptAllDevices: boolean; optionalServices?: BluetoothServiceUUID[] | undefined; optionalManufacturerData?: number[] | undefined; }; interface BluetoothAdvertisingEvent extends Event { readonly device: BluetoothDevice; readonly uuids: BluetoothServiceUUID[]; readonly manufacturerData: BluetoothManufacturerData; readonly serviceData: BluetoothServiceData; readonly name?: string | undefined; readonly appearance?: number | undefined; readonly rssi?: number | undefined; readonly txPower?: number | undefined; } interface BluetoothRemoteGATTDescriptor { readonly characteristic: BluetoothRemoteGATTCharacteristic; readonly uuid: string; readonly value?: DataView | undefined; readValue(): Promise; writeValue(value: BufferSource): Promise; } interface BluetoothCharacteristicProperties { readonly broadcast: boolean; readonly read: boolean; readonly writeWithoutResponse: boolean; readonly write: boolean; readonly notify: boolean; readonly indicate: boolean; readonly authenticatedSignedWrites: boolean; readonly reliableWrite: boolean; readonly writableAuxiliaries: boolean; } interface CharacteristicEventHandlers { oncharacteristicvaluechanged: (this: this, ev: Event) => any; } interface BluetoothRemoteGATTCharacteristic extends EventTarget, CharacteristicEventHandlers { readonly service: BluetoothRemoteGATTService; readonly uuid: string; readonly properties: BluetoothCharacteristicProperties; readonly value?: DataView | undefined; getDescriptor(descriptor: BluetoothDescriptorUUID): Promise; getDescriptors(descriptor?: BluetoothDescriptorUUID): Promise; readValue(): Promise; writeValue(value: BufferSource): Promise; writeValueWithResponse(value: BufferSource): Promise; writeValueWithoutResponse(value: BufferSource): Promise; startNotifications(): Promise; stopNotifications(): Promise; addEventListener(type: "characteristicvaluechanged", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } interface ServiceEventHandlers { onserviceadded: (this: this, ev: Event) => any; onservicechanged: (this: this, ev: Event) => any; onserviceremoved: (this: this, ev: Event) => any; } interface BluetoothRemoteGATTService extends EventTarget, CharacteristicEventHandlers, ServiceEventHandlers { readonly device: BluetoothDevice; readonly uuid: string; readonly isPrimary: boolean; getCharacteristic(characteristic: BluetoothCharacteristicUUID): Promise; getCharacteristics(characteristic?: BluetoothCharacteristicUUID): Promise; getIncludedService(service: BluetoothServiceUUID): Promise; getIncludedServices(service?: BluetoothServiceUUID): Promise; addEventListener(type: "serviceadded", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "servicechanged", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "serviceremoved", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } interface BluetoothRemoteGATTServer { readonly device: BluetoothDevice; readonly connected: boolean; connect(): Promise; disconnect(): void; getPrimaryService(service: BluetoothServiceUUID): Promise; getPrimaryServices(service?: BluetoothServiceUUID): Promise; } interface BluetoothDeviceEventHandlers { onadvertisementreceived: (this: this, ev: BluetoothAdvertisingEvent) => any; ongattserverdisconnected: (this: this, ev: Event) => any; } interface WatchAdvertisementsOptions { signal?: AbortSignal; } interface BluetoothDevice extends EventTarget, BluetoothDeviceEventHandlers, CharacteristicEventHandlers, ServiceEventHandlers { readonly id: string; readonly name?: string | undefined; readonly gatt?: BluetoothRemoteGATTServer | undefined; forget(): Promise; watchAdvertisements(options?: WatchAdvertisementsOptions): Promise; readonly watchingAdvertisements: boolean; addEventListener(type: "gattserverdisconnected", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "advertisementreceived", listener: (this: this, ev: BluetoothAdvertisingEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } interface Bluetooth extends EventTarget, BluetoothDeviceEventHandlers, CharacteristicEventHandlers, ServiceEventHandlers { getDevices(): Promise; getAvailability(): Promise; onavailabilitychanged: (this: this, ev: Event) => any; readonly referringDevice?: BluetoothDevice | undefined; requestDevice(options?: RequestDeviceOptions): Promise; requestLEScan(options?: BluetoothLEScanOptions): Promise; addEventListener(type: "availabilitychanged", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "advertisementreceived", listener: (this: this, ev: BluetoothAdvertisingEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } interface Navigator { bluetooth: Bluetooth; }