webbluetooth.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. Unit webbluetooth;
  2. {$MODE ObjFPC}
  3. {$H+}
  4. {$modeswitch externalclass}
  5. interface
  6. uses JS,web;
  7. Type
  8. // Forward class definitions
  9. TJSBluetooth = Class;
  10. TJSBluetoothPermissionResult = Class;
  11. TJSValueEvent = Class;
  12. TJSBluetoothDevice = Class;
  13. TJSBluetoothManufacturerDataMap = Class;
  14. TJSBluetoothServiceDataMap = Class;
  15. TJSBluetoothAdvertisingEvent = Class;
  16. TJSBluetoothRemoteGATTServer = Class;
  17. TJSBluetoothRemoteGATTService = Class;
  18. TJSBluetoothRemoteGATTCharacteristic = Class;
  19. TJSBluetoothCharacteristicProperties = Class;
  20. TJSBluetoothRemoteGATTDescriptor = Class;
  21. TJSCharacteristicEventHandlers = Class;
  22. TJSBluetoothDeviceEventHandlers = Class;
  23. TJSServiceEventHandlers = Class;
  24. TJSBluetoothUUID = Class;
  25. TJSBluetoothDataFilterInit = Class;
  26. TJSBluetoothLEScanFilterInit = Class;
  27. TJSRequestDeviceOptions = Class;
  28. TJSBluetoothPermissionDescriptor = Class;
  29. TJSAllowedBluetoothDevice = Class;
  30. TJSBluetoothPermissionData = Class;
  31. TJSValueEventInit = Class;
  32. TJSBluetoothAdvertisingEventInit = Class;
  33. UUID = String;
  34. // Union of DOMString, unsigned long
  35. BluetoothServiceUUID = JSValue;
  36. // Union of DOMString, unsigned long
  37. BluetoothCharacteristicUUID = JSValue;
  38. // Union of DOMString, unsigned long
  39. BluetoothDescriptorUUID = JSValue;
  40. { --------------------------------------------------------------------
  41. TJSBluetoothDataFilterInit
  42. --------------------------------------------------------------------}
  43. TJSBluetoothDataFilterInit = class(TJSObject)
  44. dataPrefix : TJSBufferSource;
  45. mask : TJSBufferSource;
  46. end;
  47. { --------------------------------------------------------------------
  48. TJSBluetoothLEScanFilterInit
  49. --------------------------------------------------------------------}
  50. TBluetoothServiceUUIDDynArray = Array of BluetoothServiceUUID;
  51. TJSBluetoothLEScanFilterInit = class(TJSObject)
  52. services : TBluetoothServiceUUIDDynArray;
  53. name : String;
  54. namePrefix : String;
  55. manufacturerData : TJSObject;
  56. serviceData : TJSObject;
  57. end;
  58. { --------------------------------------------------------------------
  59. TJSRequestDeviceOptions
  60. --------------------------------------------------------------------}
  61. TTJSBluetoothLEScanFilterInitDynArray = Array of TJSBluetoothLEScanFilterInit;
  62. TJSRequestDeviceOptions = class(TJSObject)
  63. filters : TTJSBluetoothLEScanFilterInitDynArray;
  64. optionalServices : TBluetoothServiceUUIDDynArray;
  65. acceptAllDevices : boolean;
  66. end;
  67. { --------------------------------------------------------------------
  68. TJSBluetoothPermissionDescriptor
  69. --------------------------------------------------------------------}
  70. TJSBluetoothPermissionDescriptor = class(TJSObject)
  71. deviceId : String;
  72. filters : TTJSBluetoothLEScanFilterInitDynArray;
  73. optionalServices : TBluetoothServiceUUIDDynArray;
  74. acceptAllDevices : boolean;
  75. end;
  76. { --------------------------------------------------------------------
  77. TJSAllowedBluetoothDevice
  78. --------------------------------------------------------------------}
  79. TJSAllowedBluetoothDevice = class(TJSObject)
  80. deviceId : String;
  81. mayUseGATT : boolean;
  82. allowedServices : JSValue;
  83. end;
  84. { --------------------------------------------------------------------
  85. TJSBluetoothPermissionData
  86. --------------------------------------------------------------------}
  87. TTJSAllowedBluetoothDeviceDynArray = Array of TJSAllowedBluetoothDevice;
  88. TJSBluetoothPermissionData = class(TJSObject)
  89. allowedDevices : TTJSAllowedBluetoothDeviceDynArray;
  90. end;
  91. { --------------------------------------------------------------------
  92. TJSValueEventInit
  93. --------------------------------------------------------------------}
  94. TJSValueEventInit = class(TJSObject)
  95. value : JSValue;
  96. end;
  97. { --------------------------------------------------------------------
  98. TJSBluetoothAdvertisingEventInit
  99. --------------------------------------------------------------------}
  100. TJSValueDynArray = Array of JSValue;
  101. TJSBluetoothAdvertisingEventInit = class(TJSObject)
  102. device : TJSBluetoothDevice;
  103. uuids : TJSValueDynArray;
  104. name : String;
  105. appearance : Cardinal;
  106. txPower : byte;
  107. rssi : byte;
  108. manufacturerData : TJSObject;
  109. serviceData : TJSObject;
  110. end;
  111. { --------------------------------------------------------------------
  112. TJSBluetooth
  113. --------------------------------------------------------------------}
  114. TJSBluetooth = class external name 'Bluetooth' (TJSEventTarget)
  115. Private
  116. FreferringDevice : TJSBluetoothDevice; external name 'referringDevice';
  117. Public
  118. onavailabilitychanged : TJSEventHandler;
  119. function getAvailability: TJSPromise;
  120. function requestDevice(options : TJSRequestDeviceOptions): TJSPromise; overload;
  121. function requestDevice: TJSPromise; overload;
  122. Property referringDevice : TJSBluetoothDevice Read FreferringDevice;
  123. end;
  124. { --------------------------------------------------------------------
  125. TJSBluetoothPermissionResult
  126. --------------------------------------------------------------------}
  127. TTJSBluetoothDeviceDynArray = Array of TJSBluetoothDevice;
  128. TJSBluetoothPermissionResult = class external name 'BluetoothPermissionResult' (TJSOBject)
  129. Private
  130. Public
  131. devices : TTJSBluetoothDeviceDynArray;
  132. end;
  133. { --------------------------------------------------------------------
  134. TJSValueEvent
  135. --------------------------------------------------------------------}
  136. TJSValueEvent = class external name 'ValueEvent' (TJSEvent)
  137. Private
  138. Fvalue : JSValue; external name 'value';
  139. Public
  140. Property value : JSValue Read Fvalue;
  141. end;
  142. { --------------------------------------------------------------------
  143. TJSBluetoothDevice
  144. --------------------------------------------------------------------}
  145. TJSBluetoothDevice = class external name 'BluetoothDevice'
  146. Private
  147. Fid : String; external name 'id';
  148. Fname : String; external name 'name';
  149. Fgatt : TJSBluetoothRemoteGATTServer; external name 'gatt';
  150. FwatchingAdvertisements : boolean; external name 'watchingAdvertisements';
  151. Public
  152. function watchAdvertisements: TJSPromise;
  153. Procedure unwatchAdvertisements;
  154. Property id : String Read Fid;
  155. Property name : String Read Fname;
  156. Property gatt : TJSBluetoothRemoteGATTServer Read Fgatt;
  157. Property watchingAdvertisements : boolean Read FwatchingAdvertisements;
  158. end;
  159. { --------------------------------------------------------------------
  160. TJSBluetoothManufacturerDataMap
  161. --------------------------------------------------------------------}
  162. TJSBluetoothManufacturerDataMap = class external name 'BluetoothManufacturerDataMap'
  163. Private
  164. Public
  165. end;
  166. { --------------------------------------------------------------------
  167. TJSBluetoothServiceDataMap
  168. --------------------------------------------------------------------}
  169. TJSBluetoothServiceDataMap = class external name 'BluetoothServiceDataMap'
  170. Private
  171. Public
  172. end;
  173. { --------------------------------------------------------------------
  174. TJSBluetoothAdvertisingEvent
  175. --------------------------------------------------------------------}
  176. TUUIDDynArray = Array of UUID;
  177. TJSBluetoothAdvertisingEvent = class external name 'BluetoothAdvertisingEvent' (TJSEvent)
  178. Private
  179. Fdevice : TJSBluetoothDevice; external name 'device';
  180. Fuuids : TUUIDDynArray; external name 'uuids';
  181. Fname : String; external name 'name';
  182. Fappearance : Cardinal; external name 'appearance';
  183. FtxPower : byte; external name 'txPower';
  184. Frssi : byte; external name 'rssi';
  185. FmanufacturerData : TJSBluetoothManufacturerDataMap; external name 'manufacturerData';
  186. FserviceData : TJSBluetoothServiceDataMap; external name 'serviceData';
  187. Public
  188. Property device : TJSBluetoothDevice Read Fdevice;
  189. Property uuids : TUUIDDynArray Read Fuuids;
  190. Property name : String Read Fname;
  191. Property appearance : Cardinal Read Fappearance;
  192. Property txPower : byte Read FtxPower;
  193. Property rssi : byte Read Frssi;
  194. Property manufacturerData : TJSBluetoothManufacturerDataMap Read FmanufacturerData;
  195. Property serviceData : TJSBluetoothServiceDataMap Read FserviceData;
  196. end;
  197. { --------------------------------------------------------------------
  198. TJSBluetoothRemoteGATTServer
  199. --------------------------------------------------------------------}
  200. TJSBluetoothRemoteGATTServer = class external name 'BluetoothRemoteGATTServer'
  201. Private
  202. Fdevice : TJSBluetoothDevice; external name 'device';
  203. Fconnected : boolean; external name 'connected';
  204. Public
  205. function connect: TJSPromise;
  206. Procedure disconnect;
  207. function getPrimaryService(service : BluetoothServiceUUID): TJSPromise;
  208. function getPrimaryServices(service : BluetoothServiceUUID): TJSPromise; overload;
  209. function getPrimaryServices: TJSPromise; overload;
  210. Property device : TJSBluetoothDevice Read Fdevice;
  211. Property connected : boolean Read Fconnected;
  212. end;
  213. { --------------------------------------------------------------------
  214. TJSBluetoothRemoteGATTService
  215. --------------------------------------------------------------------}
  216. TJSBluetoothRemoteGATTService = class external name 'BluetoothRemoteGATTService'
  217. Private
  218. Fdevice : TJSBluetoothDevice; external name 'device';
  219. Fuuid : UUID; external name 'uuid';
  220. FisPrimary : boolean; external name 'isPrimary';
  221. Public
  222. function getCharacteristic(characteristic : BluetoothCharacteristicUUID): TJSPromise;
  223. function getCharacteristics(characteristic : BluetoothCharacteristicUUID): TJSPromise; overload;
  224. function getCharacteristics: TJSPromise; overload;
  225. function getIncludedService(service : BluetoothServiceUUID): TJSPromise;
  226. function getIncludedServices(service : BluetoothServiceUUID): TJSPromise; overload;
  227. function getIncludedServices: TJSPromise; overload;
  228. Property device : TJSBluetoothDevice Read Fdevice;
  229. Property _uuid : UUID Read Fuuid;
  230. Property isPrimary : boolean Read FisPrimary;
  231. end;
  232. { --------------------------------------------------------------------
  233. TJSBluetoothRemoteGATTCharacteristic
  234. --------------------------------------------------------------------}
  235. TJSBluetoothRemoteGATTCharacteristic = class external name 'BluetoothRemoteGATTCharacteristic'
  236. Private
  237. Fservice : TJSBluetoothRemoteGATTService; external name 'service';
  238. Fuuid : UUID; external name 'uuid';
  239. Fproperties : TJSBluetoothCharacteristicProperties; external name 'properties';
  240. Fvalue : TJSDataView; external name 'value';
  241. Public
  242. function getDescriptor(descriptor : BluetoothDescriptorUUID): TJSPromise;
  243. function getDescriptors(descriptor : BluetoothDescriptorUUID): TJSPromise; overload;
  244. function getDescriptors: TJSPromise; overload;
  245. function readValue: TJSPromise;
  246. function writeValue(value : TJSBufferSource): TJSPromise;
  247. function startNotifications: TJSPromise;
  248. function stopNotifications: TJSPromise;
  249. Property service : TJSBluetoothRemoteGATTService Read Fservice;
  250. Property _uuid : UUID Read Fuuid;
  251. Property properties : TJSBluetoothCharacteristicProperties Read Fproperties;
  252. Property value : TJSDataView Read Fvalue;
  253. end;
  254. { --------------------------------------------------------------------
  255. TJSBluetoothCharacteristicProperties
  256. --------------------------------------------------------------------}
  257. TJSBluetoothCharacteristicProperties = class external name 'BluetoothCharacteristicProperties'
  258. Private
  259. Fbroadcast : boolean; external name 'broadcast';
  260. Fread : boolean; external name 'read';
  261. FwriteWithoutResponse : boolean; external name 'writeWithoutResponse';
  262. Fwrite : boolean; external name 'write';
  263. Fnotify : boolean; external name 'notify';
  264. Findicate : boolean; external name 'indicate';
  265. FauthenticatedSignedWrites : boolean; external name 'authenticatedSignedWrites';
  266. FreliableWrite : boolean; external name 'reliableWrite';
  267. FwritableAuxiliaries : boolean; external name 'writableAuxiliaries';
  268. Public
  269. Property broadcast : boolean Read Fbroadcast;
  270. Property read : boolean Read Fread;
  271. Property writeWithoutResponse : boolean Read FwriteWithoutResponse;
  272. Property write : boolean Read Fwrite;
  273. Property notify : boolean Read Fnotify;
  274. Property indicate : boolean Read Findicate;
  275. Property authenticatedSignedWrites : boolean Read FauthenticatedSignedWrites;
  276. Property reliableWrite : boolean Read FreliableWrite;
  277. Property writableAuxiliaries : boolean Read FwritableAuxiliaries;
  278. end;
  279. { --------------------------------------------------------------------
  280. TJSBluetoothRemoteGATTDescriptor
  281. --------------------------------------------------------------------}
  282. TJSBluetoothRemoteGATTDescriptor = class external name 'BluetoothRemoteGATTDescriptor'
  283. Private
  284. Fcharacteristic : TJSBluetoothRemoteGATTCharacteristic; external name 'characteristic';
  285. Fuuid : UUID; external name 'uuid';
  286. Fvalue : TJSDataView; external name 'value';
  287. Public
  288. function readValue: TJSPromise;
  289. function writeValue(value : TJSBufferSource): TJSPromise;
  290. Property characteristic : TJSBluetoothRemoteGATTCharacteristic Read Fcharacteristic;
  291. Property _uuid : UUID Read Fuuid;
  292. Property value : TJSDataView Read Fvalue;
  293. end;
  294. { --------------------------------------------------------------------
  295. TJSCharacteristicEventHandlers
  296. --------------------------------------------------------------------}
  297. TJSCharacteristicEventHandlers = class external name 'CharacteristicEventHandlers'
  298. Private
  299. Public
  300. oncharacteristicvaluechanged : TJSEventHandler;
  301. end;
  302. { --------------------------------------------------------------------
  303. TJSBluetoothDeviceEventHandlers
  304. --------------------------------------------------------------------}
  305. TJSBluetoothDeviceEventHandlers = class external name 'BluetoothDeviceEventHandlers'
  306. Private
  307. Public
  308. ongattserverdisconnected : TJSEventHandler;
  309. end;
  310. { --------------------------------------------------------------------
  311. TJSServiceEventHandlers
  312. --------------------------------------------------------------------}
  313. TJSServiceEventHandlers = class external name 'ServiceEventHandlers'
  314. Private
  315. Public
  316. onserviceadded : TJSEventHandler;
  317. onservicechanged : TJSEventHandler;
  318. onserviceremoved : TJSEventHandler;
  319. end;
  320. { --------------------------------------------------------------------
  321. TJSBluetoothUUID
  322. --------------------------------------------------------------------}
  323. TJSBluetoothUUID = class external name 'BluetoothUUID'
  324. Private
  325. Public
  326. function getService(name : JSValue): UUID;
  327. function getCharacteristic(name : JSValue): UUID;
  328. function getDescriptor(name : JSValue): UUID;
  329. function canonicalUUID(alias : NativeInt): UUID;
  330. end;
  331. { --------------------------------------------------------------------
  332. TJSNavigator
  333. --------------------------------------------------------------------}
  334. TJSNavigator = class external name 'Navigator'
  335. Private
  336. Fbluetooth : TJSBluetooth; external name 'bluetooth';
  337. Public
  338. Property bluetooth : TJSBluetooth Read Fbluetooth;
  339. end;
  340. implementation
  341. end.