6f7c5f9d24e40aed43c7b3fefac89073a7cd6d1cf5a8101acacb3808d6bea4567f62af6851bccbf32c5a1abc8438c787bd2af7639d8b7f35e296587677bad0 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. type BluetoothServiceUUID = number | string;
  2. type BluetoothCharacteristicUUID = number | string;
  3. type BluetoothDescriptorUUID = number | string;
  4. type BluetoothManufacturerData = Map<number, DataView>;
  5. type BluetoothServiceData = Map<BluetoothServiceUUID, DataView>;
  6. interface BluetoothDataFilter {
  7. readonly dataPrefix?: BufferSource | undefined;
  8. readonly mask?: BufferSource | undefined;
  9. }
  10. interface BluetoothManufacturerDataFilter extends BluetoothDataFilter {
  11. companyIdentifier: number;
  12. }
  13. interface BluetoothServiceDataFilter extends BluetoothDataFilter {
  14. service: BluetoothServiceUUID;
  15. }
  16. interface BluetoothLEScanFilter {
  17. readonly name?: string | undefined;
  18. readonly namePrefix?: string | undefined;
  19. readonly services?: BluetoothServiceUUID[] | undefined;
  20. readonly manufacturerData?: BluetoothManufacturerDataFilter[] | undefined;
  21. readonly serviceData?: BluetoothServiceDataFilter[] | undefined;
  22. }
  23. interface BluetoothLEScanOptions {
  24. readonly filters?: BluetoothLEScanFilter[] | undefined;
  25. readonly keepRepeatedDevices?: boolean | undefined;
  26. readonly acceptAllAdvertisements?: boolean | undefined;
  27. }
  28. interface BluetoothLEScan extends BluetoothLEScanOptions {
  29. active: boolean;
  30. stop: () => void;
  31. }
  32. type RequestDeviceOptions = {
  33. filters: BluetoothLEScanFilter[];
  34. optionalServices?: BluetoothServiceUUID[] | undefined;
  35. optionalManufacturerData?: number[] | undefined;
  36. } | {
  37. acceptAllDevices: boolean;
  38. optionalServices?: BluetoothServiceUUID[] | undefined;
  39. optionalManufacturerData?: number[] | undefined;
  40. };
  41. interface BluetoothAdvertisingEvent extends Event {
  42. readonly device: BluetoothDevice;
  43. readonly uuids: BluetoothServiceUUID[];
  44. readonly manufacturerData: BluetoothManufacturerData;
  45. readonly serviceData: BluetoothServiceData;
  46. readonly name?: string | undefined;
  47. readonly appearance?: number | undefined;
  48. readonly rssi?: number | undefined;
  49. readonly txPower?: number | undefined;
  50. }
  51. interface BluetoothRemoteGATTDescriptor {
  52. readonly characteristic: BluetoothRemoteGATTCharacteristic;
  53. readonly uuid: string;
  54. readonly value?: DataView | undefined;
  55. readValue(): Promise<DataView>;
  56. writeValue(value: BufferSource): Promise<void>;
  57. }
  58. interface BluetoothCharacteristicProperties {
  59. readonly broadcast: boolean;
  60. readonly read: boolean;
  61. readonly writeWithoutResponse: boolean;
  62. readonly write: boolean;
  63. readonly notify: boolean;
  64. readonly indicate: boolean;
  65. readonly authenticatedSignedWrites: boolean;
  66. readonly reliableWrite: boolean;
  67. readonly writableAuxiliaries: boolean;
  68. }
  69. interface CharacteristicEventHandlers {
  70. oncharacteristicvaluechanged: (this: this, ev: Event) => any;
  71. }
  72. interface BluetoothRemoteGATTCharacteristic extends EventTarget, CharacteristicEventHandlers {
  73. readonly service: BluetoothRemoteGATTService;
  74. readonly uuid: string;
  75. readonly properties: BluetoothCharacteristicProperties;
  76. readonly value?: DataView | undefined;
  77. getDescriptor(descriptor: BluetoothDescriptorUUID): Promise<BluetoothRemoteGATTDescriptor>;
  78. getDescriptors(descriptor?: BluetoothDescriptorUUID): Promise<BluetoothRemoteGATTDescriptor[]>;
  79. readValue(): Promise<DataView>;
  80. writeValue(value: BufferSource): Promise<void>;
  81. writeValueWithResponse(value: BufferSource): Promise<void>;
  82. writeValueWithoutResponse(value: BufferSource): Promise<void>;
  83. startNotifications(): Promise<BluetoothRemoteGATTCharacteristic>;
  84. stopNotifications(): Promise<BluetoothRemoteGATTCharacteristic>;
  85. addEventListener(
  86. type: "characteristicvaluechanged",
  87. listener: (this: this, ev: Event) => any,
  88. useCapture?: boolean,
  89. ): void;
  90. addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
  91. }
  92. interface ServiceEventHandlers {
  93. onserviceadded: (this: this, ev: Event) => any;
  94. onservicechanged: (this: this, ev: Event) => any;
  95. onserviceremoved: (this: this, ev: Event) => any;
  96. }
  97. interface BluetoothRemoteGATTService extends EventTarget, CharacteristicEventHandlers, ServiceEventHandlers {
  98. readonly device: BluetoothDevice;
  99. readonly uuid: string;
  100. readonly isPrimary: boolean;
  101. getCharacteristic(characteristic: BluetoothCharacteristicUUID): Promise<BluetoothRemoteGATTCharacteristic>;
  102. getCharacteristics(characteristic?: BluetoothCharacteristicUUID): Promise<BluetoothRemoteGATTCharacteristic[]>;
  103. getIncludedService(service: BluetoothServiceUUID): Promise<BluetoothRemoteGATTService>;
  104. getIncludedServices(service?: BluetoothServiceUUID): Promise<BluetoothRemoteGATTService[]>;
  105. addEventListener(type: "serviceadded", listener: (this: this, ev: Event) => any, useCapture?: boolean): void;
  106. addEventListener(type: "servicechanged", listener: (this: this, ev: Event) => any, useCapture?: boolean): void;
  107. addEventListener(type: "serviceremoved", listener: (this: this, ev: Event) => any, useCapture?: boolean): void;
  108. addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
  109. }
  110. interface BluetoothRemoteGATTServer {
  111. readonly device: BluetoothDevice;
  112. readonly connected: boolean;
  113. connect(): Promise<BluetoothRemoteGATTServer>;
  114. disconnect(): void;
  115. getPrimaryService(service: BluetoothServiceUUID): Promise<BluetoothRemoteGATTService>;
  116. getPrimaryServices(service?: BluetoothServiceUUID): Promise<BluetoothRemoteGATTService[]>;
  117. }
  118. interface BluetoothDeviceEventHandlers {
  119. onadvertisementreceived: (this: this, ev: BluetoothAdvertisingEvent) => any;
  120. ongattserverdisconnected: (this: this, ev: Event) => any;
  121. }
  122. interface WatchAdvertisementsOptions {
  123. signal?: AbortSignal;
  124. }
  125. interface BluetoothDevice
  126. extends EventTarget, BluetoothDeviceEventHandlers, CharacteristicEventHandlers, ServiceEventHandlers
  127. {
  128. readonly id: string;
  129. readonly name?: string | undefined;
  130. readonly gatt?: BluetoothRemoteGATTServer | undefined;
  131. forget(): Promise<void>;
  132. watchAdvertisements(options?: WatchAdvertisementsOptions): Promise<void>;
  133. readonly watchingAdvertisements: boolean;
  134. addEventListener(
  135. type: "gattserverdisconnected",
  136. listener: (this: this, ev: Event) => any,
  137. useCapture?: boolean,
  138. ): void;
  139. addEventListener(
  140. type: "advertisementreceived",
  141. listener: (this: this, ev: BluetoothAdvertisingEvent) => any,
  142. useCapture?: boolean,
  143. ): void;
  144. addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
  145. }
  146. interface Bluetooth
  147. extends EventTarget, BluetoothDeviceEventHandlers, CharacteristicEventHandlers, ServiceEventHandlers
  148. {
  149. getDevices(): Promise<BluetoothDevice[]>;
  150. getAvailability(): Promise<boolean>;
  151. onavailabilitychanged: (this: this, ev: Event) => any;
  152. readonly referringDevice?: BluetoothDevice | undefined;
  153. requestDevice(options?: RequestDeviceOptions): Promise<BluetoothDevice>;
  154. requestLEScan(options?: BluetoothLEScanOptions): Promise<BluetoothLEScan>;
  155. addEventListener(type: "availabilitychanged", listener: (this: this, ev: Event) => any, useCapture?: boolean): void;
  156. addEventListener(
  157. type: "advertisementreceived",
  158. listener: (this: this, ev: BluetoothAdvertisingEvent) => any,
  159. useCapture?: boolean,
  160. ): void;
  161. addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
  162. }
  163. declare namespace BluetoothUUID {
  164. function getService(name: string | number): string;
  165. function getCharacteristic(name: string | number): string;
  166. function getDescriptor(name: string | number): string;
  167. function canonicalUUID(alias: string | number): string;
  168. }
  169. interface Navigator {
  170. bluetooth: Bluetooth;
  171. }