@bun-win32/bluetoothapis 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AI.md ADDED
@@ -0,0 +1,71 @@
1
+ # AI Guide for @bun-win32/bluetoothapis
2
+
3
+ How to use this package, not what the Win32 API does.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import BluetoothApis, { BLUETOOTH_SERVICE_ENABLE } from '@bun-win32/bluetoothapis';
9
+
10
+ // Methods bind lazily on first call
11
+ const isDiscoverable = BluetoothApis.BluetoothIsDiscoverable(0n);
12
+
13
+ // Preload: array, single string, or no args (all symbols)
14
+ BluetoothApis.Preload(['BluetoothFindFirstRadio', 'BluetoothFindNextRadio']);
15
+ BluetoothApis.Preload('BluetoothGetRadioInfo');
16
+ BluetoothApis.Preload();
17
+ ```
18
+
19
+ ## Where To Look
20
+
21
+ | Need | Read |
22
+ | --------------------------------- | --------------------------- |
23
+ | Find a method or its MS Docs link | `structs/BluetoothApis.ts` |
24
+ | Find types, enums, constants | `types/BluetoothApis.ts` |
25
+ | Quick examples | `README.md` |
26
+
27
+ `index.ts` re-exports the class and all types — import from `@bun-win32/bluetoothapis` directly.
28
+
29
+ ## Calling Convention
30
+
31
+ All documented `bluetoothapis.dll` exports are bound. Each method maps 1:1 to its DLL export. Names, parameter names, and order match Microsoft Docs.
32
+
33
+ ### Strings
34
+
35
+ `W` methods take UTF-16LE NUL-terminated buffers. `A` methods take ANSI strings.
36
+
37
+ ```ts
38
+ const wide = Buffer.from('Hello\0', 'utf16le'); // LPCWSTR
39
+ BluetoothApis.BluetoothSendAuthenticationResponse(0n, pbtdi, wide.ptr);
40
+
41
+ // Reading a wide string back from a buffer:
42
+ const text = new TextDecoder('utf-16').decode(buf).replace(/\0.*$/, '');
43
+ ```
44
+
45
+ ### Return types
46
+
47
+ - `HANDLE`, `HWND`, etc. → `bigint`
48
+ - `DWORD`, `UINT`, `BOOL`, `INT`, `LONG` → `number`
49
+ - `LPVOID`, `LPWSTR`, etc. → `Pointer`
50
+ - Win32 `BOOL` is `number` (0 or non-zero), **not** JS `boolean`. Do not compare with `=== true`.
51
+
52
+ ### Pointers, handles, out-parameters
53
+
54
+ - **Pointer** params (`LP*`, `P*`, `Pointer`): pass `buffer.ptr` from a caller-allocated `Buffer`.
55
+ - **Handle** params (`HANDLE`, `HWND`, etc.): pass a `bigint` value.
56
+ - **Out-parameters**: allocate a `Buffer`, pass `.ptr`, read the result after the call.
57
+
58
+ ```ts
59
+ const out = Buffer.alloc(8);
60
+ BluetoothApis.BluetoothFindFirstRadio(params.ptr, out.ptr);
61
+ const hRadio = out.readBigUInt64LE(0);
62
+ ```
63
+
64
+ ### Nullability
65
+
66
+ - `| NULL` in a signature → pass `null` (optional pointer).
67
+ - `| 0n` in a signature → pass `0n` (optional handle).
68
+
69
+ ## Errors and Cleanup
70
+
71
+ Return values are raw. If the Win32 function uses last-error semantics, read via `GetLastError()`. Resource cleanup is your responsibility — same as raw Win32.
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # @bun-win32/bluetoothapis
2
+
3
+ Zero-dependency, zero-overhead Win32 BluetoothApis bindings for [Bun](https://bun.sh) on Windows.
4
+
5
+ ## Overview
6
+
7
+ `@bun-win32/bluetoothapis` exposes the `bluetoothapis.dll` exports using [Bun](https://bun.sh)'s FFI. It provides a single class, `BluetoothApis`, which lazily binds native symbols on first use. You can optionally preload a subset or all symbols up-front via `Preload()`.
8
+
9
+ The bindings are strongly typed for a smooth DX in TypeScript.
10
+
11
+ ## Features
12
+
13
+ - [Bun](https://bun.sh)-first ergonomics on Windows 10/11.
14
+ - Direct FFI to `bluetoothapis.dll` (Bluetooth Classic radio/device discovery, BLE GATT, SDP, authentication).
15
+ - In-source docs in `structs/BluetoothApis.ts` with links to Microsoft Docs.
16
+ - Lazy binding on first call; optional eager preload (`BluetoothApis.Preload()`).
17
+ - No wrapper overhead; calls map 1:1 to native APIs.
18
+ - Strongly-typed Win32 aliases (see `types/BluetoothApis.ts`).
19
+
20
+ ## Requirements
21
+
22
+ - [Bun](https://bun.sh) runtime
23
+ - Windows 10 or later
24
+
25
+ ## Installation
26
+
27
+ ```sh
28
+ bun add @bun-win32/bluetoothapis
29
+ ```
30
+
31
+ ## Quick Start
32
+
33
+ ```ts
34
+ import BluetoothApis from '@bun-win32/bluetoothapis';
35
+ import Kernel32 from '@bun-win32/kernel32';
36
+
37
+ // BLUETOOTH_FIND_RADIO_PARAMS: { dwSize: DWORD }
38
+ const params = Buffer.alloc(4);
39
+ params.writeUInt32LE(4, 0);
40
+
41
+ const hRadio = Buffer.alloc(8);
42
+ const hFind = BluetoothApis.BluetoothFindFirstRadio(params.ptr, hRadio.ptr);
43
+
44
+ if (hFind !== 0n) {
45
+ const radioHandle = hRadio.readBigUInt64LE(0);
46
+ console.log('Found radio handle:', radioHandle);
47
+
48
+ // Get radio info
49
+ const radioInfo = Buffer.alloc(520); // BLUETOOTH_RADIO_INFO size
50
+ radioInfo.writeUInt32LE(520, 0); // dwSize
51
+ const err = BluetoothApis.BluetoothGetRadioInfo(radioHandle, radioInfo.ptr);
52
+ if (err === 0) {
53
+ const name = new TextDecoder('utf-16le').decode(radioInfo.subarray(264, 264 + 496)).replace(/\0.*$/, '');
54
+ console.log('Radio name:', name);
55
+ }
56
+
57
+ Kernel32.CloseHandle(radioHandle);
58
+ BluetoothApis.BluetoothFindRadioClose(hFind);
59
+ }
60
+ ```
61
+
62
+ > [!NOTE]
63
+ > AI agents: see `AI.md` for the package binding contract and source-navigation guidance. It explains how to use the package without scanning the entire implementation.
64
+
65
+ ## Examples
66
+
67
+ Run the included examples:
68
+
69
+ ```sh
70
+ cd packages/bluetoothapis && bun run example/bluetoothapis.ts
71
+ ```
72
+
73
+ ## Notes
74
+
75
+ - Either rely on lazy binding or call `BluetoothApis.Preload()`.
76
+ - Windows only. Bun runtime required.
package/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ import BluetoothApis from './structs/BluetoothApis';
2
+
3
+ export * from './types/BluetoothApis';
4
+ export default BluetoothApis;
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "author": "Stev Peifer <stev@bell.net>",
3
+ "bugs": {
4
+ "url": "https://github.com/ObscuritySRL/bun-win32/issues"
5
+ },
6
+ "dependencies": {
7
+ "@bun-win32/core": "1.1.1"
8
+ },
9
+ "description": "Zero-dependency, zero-overhead Win32 BLUETOOTHAPIS bindings for Bun (FFI) on Windows.",
10
+ "devDependencies": {
11
+ "@types/bun": "latest"
12
+ },
13
+ "exports": {
14
+ ".": "./index.ts"
15
+ },
16
+ "license": "MIT",
17
+ "module": "index.ts",
18
+ "name": "@bun-win32/bluetoothapis",
19
+ "peerDependencies": {
20
+ "typescript": "^5"
21
+ },
22
+ "private": false,
23
+ "homepage": "https://github.com/ObscuritySRL/bun-win32#readme",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git://github.com/ObscuritySRL/bun-win32.git",
27
+ "directory": "packages/bluetoothapis"
28
+ },
29
+ "type": "module",
30
+ "version": "1.0.0",
31
+ "main": "./index.ts",
32
+ "keywords": [
33
+ "bun",
34
+ "ffi",
35
+ "win32",
36
+ "windows",
37
+ "bluetoothapis",
38
+ "bluetooth",
39
+ "ble",
40
+ "gatt",
41
+ "bindings",
42
+ "typescript",
43
+ "dll"
44
+ ],
45
+ "files": [
46
+ "AI.md",
47
+ "README.md",
48
+ "index.ts",
49
+ "structs/*.ts",
50
+ "types/*.ts"
51
+ ],
52
+ "sideEffects": false,
53
+ "engines": {
54
+ "bun": ">=1.1.0"
55
+ },
56
+ "scripts": {
57
+ "example": "bun ./example/bluetoothapis.ts"
58
+ }
59
+ }
@@ -0,0 +1,379 @@
1
+ import { type FFIFunction, FFIType } from 'bun:ffi';
2
+
3
+ import { Win32 } from '@bun-win32/core';
4
+
5
+ import type {
6
+ BLUETOOTH_GATT_EVENT_HANDLE,
7
+ BOOL,
8
+ BTH_LE_GATT_EVENT_TYPE,
9
+ BTH_LE_GATT_RELIABLE_WRITE_CONTEXT,
10
+ DWORD,
11
+ HANDLE,
12
+ HBLUETOOTH_AUTHENTICATION_REGISTRATION,
13
+ HBLUETOOTH_DEVICE_FIND,
14
+ HBLUETOOTH_RADIO_FIND,
15
+ HRESULT,
16
+ LPBYTE,
17
+ LPCWSTR,
18
+ LPDWORD,
19
+ LPVOID,
20
+ LPWSTR,
21
+ NULL,
22
+ PBLUETOOTH_ADDRESS,
23
+ PBLUETOOTH_AUTHENTICATE_RESPONSE,
24
+ PBLUETOOTH_DEVICE_INFO,
25
+ PBLUETOOTH_DEVICE_SEARCH_PARAMS,
26
+ PBLUETOOTH_FIND_RADIO_PARAMS,
27
+ PBLUETOOTH_GATT_EVENT_HANDLE,
28
+ PBLUETOOTH_LOCAL_SERVICE_INFO,
29
+ PBLUETOOTH_RADIO_INFO,
30
+ PBTH_LE_GATT_CHARACTERISTIC,
31
+ PBTH_LE_GATT_CHARACTERISTIC_VALUE,
32
+ PBTH_LE_GATT_DESCRIPTOR,
33
+ PBTH_LE_GATT_DESCRIPTOR_VALUE,
34
+ PBTH_LE_GATT_RELIABLE_WRITE_CONTEXT,
35
+ PBTH_LE_GATT_SERVICE,
36
+ PFNBLUETOOTH_GATT_EVENT_CALLBACK,
37
+ PFN_AUTHENTICATION_CALLBACK,
38
+ PFN_AUTHENTICATION_CALLBACK_EX,
39
+ PFN_BLUETOOTH_ENUM_ATTRIBUTES_CALLBACK,
40
+ PGUID,
41
+ PHANDLE,
42
+ PHBLUETOOTH_AUTHENTICATION_REGISTRATION,
43
+ PHBLUETOOTH_CONTAINER_ELEMENT,
44
+ PSDP_ELEMENT_DATA,
45
+ PSDP_STRING_TYPE_DATA,
46
+ PULONG,
47
+ PUSHORT,
48
+ PVOID,
49
+ UCHAR,
50
+ ULONG,
51
+ USHORT,
52
+ } from '../types/BluetoothApis';
53
+
54
+ /**
55
+ * Thin, lazy-loaded FFI bindings for `bluetoothapis.dll`.
56
+ *
57
+ * Each static method corresponds one-to-one with a Win32 export declared in `Symbols`.
58
+ * The first call to a method binds the underlying native symbol via `bun:ffi` and
59
+ * memoizes it on the class for subsequent calls. For bulk, up-front binding, use `Preload`.
60
+ *
61
+ * Symbols are defined with explicit `FFIType` signatures and kept alphabetized.
62
+ * You normally do not access `Symbols` directly; call the static methods or preload
63
+ * a subset for hot paths.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import BluetoothApis from './structs/BluetoothApis';
68
+ *
69
+ * // Lazy: bind on first call
70
+ * const hFind = BluetoothApis.BluetoothFindFirstRadio(params.ptr, hRadio.ptr);
71
+ *
72
+ * // Or preload a subset to avoid per-symbol lazy binding cost
73
+ * BluetoothApis.Preload(['BluetoothFindFirstRadio', 'BluetoothFindNextRadio']);
74
+ * ```
75
+ */
76
+ class BluetoothApis extends Win32 {
77
+ protected static override name = 'bluetoothapis.dll';
78
+
79
+ /** @inheritdoc */
80
+ protected static override readonly Symbols = {
81
+ BluetoothEnableDiscovery: { args: [FFIType.u64, FFIType.i32], returns: FFIType.i32 },
82
+ BluetoothEnableIncomingConnections: { args: [FFIType.u64, FFIType.i32], returns: FFIType.i32 },
83
+ BluetoothEnumerateInstalledServices: { args: [FFIType.u64, FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.u32 },
84
+ BluetoothFindDeviceClose: { args: [FFIType.u64], returns: FFIType.i32 },
85
+ BluetoothFindFirstDevice: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.u64 },
86
+ BluetoothFindFirstRadio: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.u64 },
87
+ BluetoothFindNextDevice: { args: [FFIType.u64, FFIType.ptr], returns: FFIType.i32 },
88
+ BluetoothFindNextRadio: { args: [FFIType.u64, FFIType.ptr], returns: FFIType.i32 },
89
+ BluetoothFindRadioClose: { args: [FFIType.u64], returns: FFIType.i32 },
90
+ BluetoothGATTAbortReliableWrite: { args: [FFIType.u64, FFIType.u64, FFIType.u32], returns: FFIType.i32 },
91
+ BluetoothGATTBeginReliableWrite: { args: [FFIType.u64, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
92
+ BluetoothGATTEndReliableWrite: { args: [FFIType.u64, FFIType.u64, FFIType.u32], returns: FFIType.i32 },
93
+ BluetoothGATTGetCharacteristicValue: { args: [FFIType.u64, FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
94
+ BluetoothGATTGetCharacteristics: { args: [FFIType.u64, FFIType.ptr, FFIType.u16, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
95
+ BluetoothGATTGetDescriptorValue: { args: [FFIType.u64, FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
96
+ BluetoothGATTGetDescriptors: { args: [FFIType.u64, FFIType.ptr, FFIType.u16, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
97
+ BluetoothGATTGetIncludedServices: { args: [FFIType.u64, FFIType.ptr, FFIType.u16, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
98
+ BluetoothGATTGetServices: { args: [FFIType.u64, FFIType.u16, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
99
+ BluetoothGATTRegisterEvent: { args: [FFIType.u64, FFIType.i32, FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
100
+ BluetoothGATTSetCharacteristicValue: { args: [FFIType.u64, FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.u32], returns: FFIType.i32 },
101
+ BluetoothGATTSetDescriptorValue: { args: [FFIType.u64, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.i32 },
102
+ BluetoothGATTUnregisterEvent: { args: [FFIType.u64, FFIType.u32], returns: FFIType.i32 },
103
+ BluetoothGetDeviceInfo: { args: [FFIType.u64, FFIType.ptr], returns: FFIType.u32 },
104
+ BluetoothGetRadioInfo: { args: [FFIType.u64, FFIType.ptr], returns: FFIType.u32 },
105
+ BluetoothIsConnectable: { args: [FFIType.u64], returns: FFIType.i32 },
106
+ BluetoothIsDiscoverable: { args: [FFIType.u64], returns: FFIType.i32 },
107
+ BluetoothIsVersionAvailable: { args: [FFIType.u8, FFIType.u8], returns: FFIType.i32 },
108
+ BluetoothRegisterForAuthentication: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.u32 },
109
+ BluetoothRegisterForAuthenticationEx: { args: [FFIType.ptr, FFIType.ptr, FFIType.ptr, FFIType.ptr], returns: FFIType.u32 },
110
+ BluetoothRemoveDevice: { args: [FFIType.ptr], returns: FFIType.u32 },
111
+ BluetoothSdpEnumAttributes: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
112
+ BluetoothSdpGetAttributeValue: { args: [FFIType.ptr, FFIType.u32, FFIType.u16, FFIType.ptr], returns: FFIType.u32 },
113
+ BluetoothSdpGetContainerElementData: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr], returns: FFIType.u32 },
114
+ BluetoothSdpGetElementData: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr], returns: FFIType.u32 },
115
+ BluetoothSdpGetString: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.u16, FFIType.ptr, FFIType.ptr], returns: FFIType.u32 },
116
+ BluetoothSendAuthenticationResponse: { args: [FFIType.u64, FFIType.ptr, FFIType.ptr], returns: FFIType.u32 },
117
+ BluetoothSendAuthenticationResponseEx: { args: [FFIType.u64, FFIType.ptr], returns: FFIType.u32 },
118
+ BluetoothSetLocalServiceInfo: { args: [FFIType.u64, FFIType.ptr, FFIType.u32, FFIType.ptr], returns: FFIType.u32 },
119
+ BluetoothSetServiceState: { args: [FFIType.u64, FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.u32 },
120
+ BluetoothUnregisterAuthentication: { args: [FFIType.u64], returns: FFIType.i32 },
121
+ BluetoothUpdateDeviceRecord: { args: [FFIType.ptr], returns: FFIType.u32 },
122
+ } as const satisfies Record<string, FFIFunction>;
123
+
124
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothenablediscovery
125
+ public static BluetoothEnableDiscovery(hRadio: HANDLE | 0n, fEnabled: BOOL): BOOL {
126
+ return BluetoothApis.Load('BluetoothEnableDiscovery')(hRadio, fEnabled);
127
+ }
128
+
129
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothenableincomingconnections
130
+ public static BluetoothEnableIncomingConnections(hRadio: HANDLE | 0n, fEnabled: BOOL): BOOL {
131
+ return BluetoothApis.Load('BluetoothEnableIncomingConnections')(hRadio, fEnabled);
132
+ }
133
+
134
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothenumerateinstalledservices
135
+ public static BluetoothEnumerateInstalledServices(hRadio: HANDLE | 0n, pbtdi: PBLUETOOTH_DEVICE_INFO, pcServiceInout: LPDWORD, pGuidServices: PGUID | NULL): DWORD {
136
+ return BluetoothApis.Load('BluetoothEnumerateInstalledServices')(hRadio, pbtdi, pcServiceInout, pGuidServices);
137
+ }
138
+
139
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothfinddeviceclose
140
+ public static BluetoothFindDeviceClose(hFind: HBLUETOOTH_DEVICE_FIND): BOOL {
141
+ return BluetoothApis.Load('BluetoothFindDeviceClose')(hFind);
142
+ }
143
+
144
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothfindfirstdevice
145
+ public static BluetoothFindFirstDevice(pbtsp: PBLUETOOTH_DEVICE_SEARCH_PARAMS, pbtdi: PBLUETOOTH_DEVICE_INFO): HBLUETOOTH_DEVICE_FIND {
146
+ return BluetoothApis.Load('BluetoothFindFirstDevice')(pbtsp, pbtdi);
147
+ }
148
+
149
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothfindfirstradio
150
+ public static BluetoothFindFirstRadio(pbtfrp: PBLUETOOTH_FIND_RADIO_PARAMS, phRadio: PHANDLE): HBLUETOOTH_RADIO_FIND {
151
+ return BluetoothApis.Load('BluetoothFindFirstRadio')(pbtfrp, phRadio);
152
+ }
153
+
154
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothfindnextdevice
155
+ public static BluetoothFindNextDevice(hFind: HBLUETOOTH_DEVICE_FIND, pbtdi: PBLUETOOTH_DEVICE_INFO): BOOL {
156
+ return BluetoothApis.Load('BluetoothFindNextDevice')(hFind, pbtdi);
157
+ }
158
+
159
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothfindnextradio
160
+ public static BluetoothFindNextRadio(hFind: HBLUETOOTH_RADIO_FIND, phRadio: PHANDLE): BOOL {
161
+ return BluetoothApis.Load('BluetoothFindNextRadio')(hFind, phRadio);
162
+ }
163
+
164
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothfindradioclose
165
+ public static BluetoothFindRadioClose(hFind: HBLUETOOTH_RADIO_FIND): BOOL {
166
+ return BluetoothApis.Load('BluetoothFindRadioClose')(hFind);
167
+ }
168
+
169
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattabortreliablewrite
170
+ public static BluetoothGATTAbortReliableWrite(hDevice: HANDLE, ReliableWriteContext: BTH_LE_GATT_RELIABLE_WRITE_CONTEXT, Flags: ULONG): HRESULT {
171
+ return BluetoothApis.Load('BluetoothGATTAbortReliableWrite')(hDevice, ReliableWriteContext, Flags);
172
+ }
173
+
174
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattbeginreliablewrite
175
+ public static BluetoothGATTBeginReliableWrite(hDevice: HANDLE, ReliableWriteContext: PBTH_LE_GATT_RELIABLE_WRITE_CONTEXT, Flags: ULONG): HRESULT {
176
+ return BluetoothApis.Load('BluetoothGATTBeginReliableWrite')(hDevice, ReliableWriteContext, Flags);
177
+ }
178
+
179
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattendreliablewrite
180
+ public static BluetoothGATTEndReliableWrite(hDevice: HANDLE, ReliableWriteContext: BTH_LE_GATT_RELIABLE_WRITE_CONTEXT, Flags: ULONG): HRESULT {
181
+ return BluetoothApis.Load('BluetoothGATTEndReliableWrite')(hDevice, ReliableWriteContext, Flags);
182
+ }
183
+
184
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattgetcharacteristicvalue
185
+ public static BluetoothGATTGetCharacteristicValue(
186
+ hDevice: HANDLE,
187
+ Characteristic: PBTH_LE_GATT_CHARACTERISTIC,
188
+ CharacteristicValueDataSize: ULONG,
189
+ CharacteristicValue: PBTH_LE_GATT_CHARACTERISTIC_VALUE | NULL,
190
+ CharacteristicValueSizeRequired: PUSHORT | NULL,
191
+ Flags: ULONG,
192
+ ): HRESULT {
193
+ return BluetoothApis.Load('BluetoothGATTGetCharacteristicValue')(hDevice, Characteristic, CharacteristicValueDataSize, CharacteristicValue, CharacteristicValueSizeRequired, Flags);
194
+ }
195
+
196
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattgetcharacteristics
197
+ public static BluetoothGATTGetCharacteristics(
198
+ hDevice: HANDLE,
199
+ Service: PBTH_LE_GATT_SERVICE | NULL,
200
+ CharacteristicsBufferCount: USHORT,
201
+ CharacteristicsBuffer: PBTH_LE_GATT_CHARACTERISTIC | NULL,
202
+ CharacteristicsBufferActual: PUSHORT,
203
+ Flags: ULONG,
204
+ ): HRESULT {
205
+ return BluetoothApis.Load('BluetoothGATTGetCharacteristics')(hDevice, Service, CharacteristicsBufferCount, CharacteristicsBuffer, CharacteristicsBufferActual, Flags);
206
+ }
207
+
208
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattgetdescriptorvalue
209
+ public static BluetoothGATTGetDescriptorValue(
210
+ hDevice: HANDLE,
211
+ Descriptor: PBTH_LE_GATT_DESCRIPTOR,
212
+ DescriptorValueDataSize: ULONG,
213
+ DescriptorValue: PBTH_LE_GATT_DESCRIPTOR_VALUE | NULL,
214
+ DescriptorValueSizeRequired: PUSHORT | NULL,
215
+ Flags: ULONG,
216
+ ): HRESULT {
217
+ return BluetoothApis.Load('BluetoothGATTGetDescriptorValue')(hDevice, Descriptor, DescriptorValueDataSize, DescriptorValue, DescriptorValueSizeRequired, Flags);
218
+ }
219
+
220
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattgetdescriptors
221
+ public static BluetoothGATTGetDescriptors(
222
+ hDevice: HANDLE,
223
+ Characteristic: PBTH_LE_GATT_CHARACTERISTIC,
224
+ DescriptorsBufferCount: USHORT,
225
+ DescriptorsBuffer: PBTH_LE_GATT_DESCRIPTOR | NULL,
226
+ DescriptorsBufferActual: PUSHORT,
227
+ Flags: ULONG,
228
+ ): HRESULT {
229
+ return BluetoothApis.Load('BluetoothGATTGetDescriptors')(hDevice, Characteristic, DescriptorsBufferCount, DescriptorsBuffer, DescriptorsBufferActual, Flags);
230
+ }
231
+
232
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattgetincludedservices
233
+ public static BluetoothGATTGetIncludedServices(
234
+ hDevice: HANDLE,
235
+ ParentService: PBTH_LE_GATT_SERVICE | NULL,
236
+ IncludedServicesBufferCount: USHORT,
237
+ IncludedServicesBuffer: PBTH_LE_GATT_SERVICE | NULL,
238
+ IncludedServicesBufferActual: PUSHORT,
239
+ Flags: ULONG,
240
+ ): HRESULT {
241
+ return BluetoothApis.Load('BluetoothGATTGetIncludedServices')(hDevice, ParentService, IncludedServicesBufferCount, IncludedServicesBuffer, IncludedServicesBufferActual, Flags);
242
+ }
243
+
244
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattgetservices
245
+ public static BluetoothGATTGetServices(hDevice: HANDLE, ServicesBufferCount: USHORT, ServicesBuffer: PBTH_LE_GATT_SERVICE | NULL, ServicesBufferActual: PUSHORT, Flags: ULONG): HRESULT {
246
+ return BluetoothApis.Load('BluetoothGATTGetServices')(hDevice, ServicesBufferCount, ServicesBuffer, ServicesBufferActual, Flags);
247
+ }
248
+
249
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattregisterevent
250
+ public static BluetoothGATTRegisterEvent(
251
+ hService: HANDLE,
252
+ EventType: BTH_LE_GATT_EVENT_TYPE,
253
+ EventParameterIn: PVOID,
254
+ Callback: PFNBLUETOOTH_GATT_EVENT_CALLBACK,
255
+ CallbackContext: PVOID | NULL,
256
+ pEventHandle: PBLUETOOTH_GATT_EVENT_HANDLE,
257
+ Flags: ULONG,
258
+ ): HRESULT {
259
+ return BluetoothApis.Load('BluetoothGATTRegisterEvent')(hService, EventType, EventParameterIn, Callback, CallbackContext, pEventHandle, Flags);
260
+ }
261
+
262
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattsetcharacteristicvalue
263
+ public static BluetoothGATTSetCharacteristicValue(
264
+ hDevice: HANDLE,
265
+ Characteristic: PBTH_LE_GATT_CHARACTERISTIC,
266
+ CharacteristicValue: PBTH_LE_GATT_CHARACTERISTIC_VALUE,
267
+ ReliableWriteContext: BTH_LE_GATT_RELIABLE_WRITE_CONTEXT | 0n,
268
+ Flags: ULONG,
269
+ ): HRESULT {
270
+ return BluetoothApis.Load('BluetoothGATTSetCharacteristicValue')(hDevice, Characteristic, CharacteristicValue, ReliableWriteContext, Flags);
271
+ }
272
+
273
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattsetdescriptorvalue
274
+ public static BluetoothGATTSetDescriptorValue(hDevice: HANDLE, Descriptor: PBTH_LE_GATT_DESCRIPTOR, DescriptorValue: PBTH_LE_GATT_DESCRIPTOR_VALUE, Flags: ULONG): HRESULT {
275
+ return BluetoothApis.Load('BluetoothGATTSetDescriptorValue')(hDevice, Descriptor, DescriptorValue, Flags);
276
+ }
277
+
278
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothleapis/nf-bluetoothleapis-bluetoothgattunregisterevent
279
+ public static BluetoothGATTUnregisterEvent(EventHandle: BLUETOOTH_GATT_EVENT_HANDLE, Flags: ULONG): HRESULT {
280
+ return BluetoothApis.Load('BluetoothGATTUnregisterEvent')(EventHandle, Flags);
281
+ }
282
+
283
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothgetdeviceinfo
284
+ public static BluetoothGetDeviceInfo(hRadio: HANDLE, pbtdi: PBLUETOOTH_DEVICE_INFO): DWORD {
285
+ return BluetoothApis.Load('BluetoothGetDeviceInfo')(hRadio, pbtdi);
286
+ }
287
+
288
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothgetradioinfo
289
+ public static BluetoothGetRadioInfo(hRadio: HANDLE, pRadioInfo: PBLUETOOTH_RADIO_INFO): DWORD {
290
+ return BluetoothApis.Load('BluetoothGetRadioInfo')(hRadio, pRadioInfo);
291
+ }
292
+
293
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothisconnectable
294
+ public static BluetoothIsConnectable(hRadio: HANDLE | 0n): BOOL {
295
+ return BluetoothApis.Load('BluetoothIsConnectable')(hRadio);
296
+ }
297
+
298
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothisdiscoverable
299
+ public static BluetoothIsDiscoverable(hRadio: HANDLE | 0n): BOOL {
300
+ return BluetoothApis.Load('BluetoothIsDiscoverable')(hRadio);
301
+ }
302
+
303
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothisversionavailable
304
+ public static BluetoothIsVersionAvailable(MajorVersion: UCHAR, MinorVersion: UCHAR): BOOL {
305
+ return BluetoothApis.Load('BluetoothIsVersionAvailable')(MajorVersion, MinorVersion);
306
+ }
307
+
308
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothregisterforauthentication
309
+ public static BluetoothRegisterForAuthentication(pbtdi: PBLUETOOTH_DEVICE_INFO, phRegHandle: PHBLUETOOTH_AUTHENTICATION_REGISTRATION, pfnCallback: PFN_AUTHENTICATION_CALLBACK, pvParam: PVOID | NULL): DWORD {
310
+ return BluetoothApis.Load('BluetoothRegisterForAuthentication')(pbtdi, phRegHandle, pfnCallback, pvParam);
311
+ }
312
+
313
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothregisterforauthenticationex
314
+ public static BluetoothRegisterForAuthenticationEx(pbtdiIn: PBLUETOOTH_DEVICE_INFO | NULL, phRegHandleOut: PHBLUETOOTH_AUTHENTICATION_REGISTRATION, pfnCallbackIn: PFN_AUTHENTICATION_CALLBACK_EX | NULL, pvParam: PVOID | NULL): DWORD {
315
+ return BluetoothApis.Load('BluetoothRegisterForAuthenticationEx')(pbtdiIn, phRegHandleOut, pfnCallbackIn, pvParam);
316
+ }
317
+
318
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothremovedevice
319
+ public static BluetoothRemoveDevice(pAddress: PBLUETOOTH_ADDRESS): DWORD {
320
+ return BluetoothApis.Load('BluetoothRemoveDevice')(pAddress);
321
+ }
322
+
323
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsdpenumattributes
324
+ public static BluetoothSdpEnumAttributes(pSDPStream: LPBYTE, cbStreamSize: ULONG, pfnCallback: PFN_BLUETOOTH_ENUM_ATTRIBUTES_CALLBACK, pvParam: LPVOID | NULL): BOOL {
325
+ return BluetoothApis.Load('BluetoothSdpEnumAttributes')(pSDPStream, cbStreamSize, pfnCallback, pvParam);
326
+ }
327
+
328
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsdpgetattributevalue
329
+ public static BluetoothSdpGetAttributeValue(pRecordStream: LPBYTE, cbRecordLength: ULONG, usAttributeId: USHORT, pAttributeData: PSDP_ELEMENT_DATA): DWORD {
330
+ return BluetoothApis.Load('BluetoothSdpGetAttributeValue')(pRecordStream, cbRecordLength, usAttributeId, pAttributeData);
331
+ }
332
+
333
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsdpgetcontainerelementdata
334
+ public static BluetoothSdpGetContainerElementData(pContainerStream: LPBYTE, cbContainerLength: ULONG, pElement: PHBLUETOOTH_CONTAINER_ELEMENT, pData: PSDP_ELEMENT_DATA): DWORD {
335
+ return BluetoothApis.Load('BluetoothSdpGetContainerElementData')(pContainerStream, cbContainerLength, pElement, pData);
336
+ }
337
+
338
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsdpgetelementdata
339
+ public static BluetoothSdpGetElementData(pSdpStream: LPBYTE, cbSdpStreamLength: ULONG, pData: PSDP_ELEMENT_DATA): DWORD {
340
+ return BluetoothApis.Load('BluetoothSdpGetElementData')(pSdpStream, cbSdpStreamLength, pData);
341
+ }
342
+
343
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsdpgetstring
344
+ public static BluetoothSdpGetString(pRecordStream: LPBYTE, cbRecordLength: ULONG, pStringData: PSDP_STRING_TYPE_DATA | NULL, usStringOffset: USHORT, pszString: LPWSTR | NULL, pcchStringLength: PULONG): DWORD {
345
+ return BluetoothApis.Load('BluetoothSdpGetString')(pRecordStream, cbRecordLength, pStringData, usStringOffset, pszString, pcchStringLength);
346
+ }
347
+
348
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsendauthenticationresponse
349
+ public static BluetoothSendAuthenticationResponse(hRadio: HANDLE | 0n, pbtdi: PBLUETOOTH_DEVICE_INFO, pszPasskey: LPCWSTR): DWORD {
350
+ return BluetoothApis.Load('BluetoothSendAuthenticationResponse')(hRadio, pbtdi, pszPasskey);
351
+ }
352
+
353
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsendauthenticationresponseex
354
+ public static BluetoothSendAuthenticationResponseEx(hRadioIn: HANDLE | 0n, pauthResponse: PBLUETOOTH_AUTHENTICATE_RESPONSE): DWORD {
355
+ return BluetoothApis.Load('BluetoothSendAuthenticationResponseEx')(hRadioIn, pauthResponse);
356
+ }
357
+
358
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsetlocalserviceinfo
359
+ public static BluetoothSetLocalServiceInfo(hRadioIn: HANDLE | 0n, pClassGuid: PGUID, ulInstance: ULONG, pServiceInfoIn: PBLUETOOTH_LOCAL_SERVICE_INFO): DWORD {
360
+ return BluetoothApis.Load('BluetoothSetLocalServiceInfo')(hRadioIn, pClassGuid, ulInstance, pServiceInfoIn);
361
+ }
362
+
363
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothsetservicestate
364
+ public static BluetoothSetServiceState(hRadio: HANDLE, pbtdi: PBLUETOOTH_DEVICE_INFO, pGuidService: PGUID, dwServiceFlags: DWORD): DWORD {
365
+ return BluetoothApis.Load('BluetoothSetServiceState')(hRadio, pbtdi, pGuidService, dwServiceFlags);
366
+ }
367
+
368
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothunregisterauthentication
369
+ public static BluetoothUnregisterAuthentication(hRegHandle: HBLUETOOTH_AUTHENTICATION_REGISTRATION): BOOL {
370
+ return BluetoothApis.Load('BluetoothUnregisterAuthentication')(hRegHandle);
371
+ }
372
+
373
+ // https://learn.microsoft.com/en-us/windows/win32/api/bluetoothapis/nf-bluetoothapis-bluetoothupdatedevicerecord
374
+ public static BluetoothUpdateDeviceRecord(pbtdi: PBLUETOOTH_DEVICE_INFO): DWORD {
375
+ return BluetoothApis.Load('BluetoothUpdateDeviceRecord')(pbtdi);
376
+ }
377
+ }
378
+
379
+ export default BluetoothApis;
@@ -0,0 +1,52 @@
1
+ import type { Pointer } from 'bun:ffi';
2
+
3
+ export type { BOOL, DWORD, HANDLE, HRESULT, LPBYTE, LPCWSTR, LPDWORD, LPVOID, LPWSTR, NULL, PHANDLE, PULONG, PVOID, ULONG, USHORT } from '@bun-win32/core';
4
+
5
+ export const BLUETOOTH_GATT_FLAG_CONNECTION_AUTHENTICATED = 0x0000_0002;
6
+ export const BLUETOOTH_GATT_FLAG_CONNECTION_ENCRYPTED = 0x0000_0001;
7
+ export const BLUETOOTH_GATT_FLAG_FORCE_READ_FROM_CACHE = 0x0000_0008;
8
+ export const BLUETOOTH_GATT_FLAG_FORCE_READ_FROM_DEVICE = 0x0000_0004;
9
+ export const BLUETOOTH_GATT_FLAG_NONE = 0x0000_0000;
10
+ export const BLUETOOTH_GATT_FLAG_RETURN_ALL = 0x0000_0040;
11
+ export const BLUETOOTH_GATT_FLAG_SIGNED_WRITE = 0x0000_0010;
12
+ export const BLUETOOTH_GATT_FLAG_WRITE_WITHOUT_RESPONSE = 0x0000_0020;
13
+ export const BLUETOOTH_MAX_NAME_SIZE = 248;
14
+ export const BLUETOOTH_MAX_PASSKEY_SIZE = 16;
15
+ export const BLUETOOTH_MAX_SERVICE_NAME_SIZE = 256;
16
+ export const BLUETOOTH_SERVICE_DISABLE = 0x00;
17
+ export const BLUETOOTH_SERVICE_ENABLE = 0x01;
18
+
19
+ export enum BTH_LE_GATT_EVENT_TYPE {
20
+ CharacteristicValueChangedEvent = 0,
21
+ }
22
+
23
+ export type BLUETOOTH_GATT_EVENT_HANDLE = bigint;
24
+ export type BTH_LE_GATT_RELIABLE_WRITE_CONTEXT = bigint;
25
+ export type HBLUETOOTH_AUTHENTICATION_REGISTRATION = bigint;
26
+ export type HBLUETOOTH_DEVICE_FIND = bigint;
27
+ export type HBLUETOOTH_RADIO_FIND = bigint;
28
+ export type PBLUETOOTH_ADDRESS = Pointer;
29
+ export type PBLUETOOTH_AUTHENTICATE_RESPONSE = Pointer;
30
+ export type PBLUETOOTH_DEVICE_INFO = Pointer;
31
+ export type PBLUETOOTH_DEVICE_SEARCH_PARAMS = Pointer;
32
+ export type PBLUETOOTH_FIND_RADIO_PARAMS = Pointer;
33
+ export type PBLUETOOTH_GATT_EVENT_HANDLE = Pointer;
34
+ export type PBLUETOOTH_LOCAL_SERVICE_INFO = Pointer;
35
+ export type PBLUETOOTH_RADIO_INFO = Pointer;
36
+ export type PBTH_LE_GATT_CHARACTERISTIC = Pointer;
37
+ export type PBTH_LE_GATT_CHARACTERISTIC_VALUE = Pointer;
38
+ export type PBTH_LE_GATT_DESCRIPTOR = Pointer;
39
+ export type PBTH_LE_GATT_DESCRIPTOR_VALUE = Pointer;
40
+ export type PBTH_LE_GATT_RELIABLE_WRITE_CONTEXT = Pointer;
41
+ export type PBTH_LE_GATT_SERVICE = Pointer;
42
+ export type PFNBLUETOOTH_GATT_EVENT_CALLBACK = Pointer;
43
+ export type PFN_AUTHENTICATION_CALLBACK = Pointer;
44
+ export type PFN_AUTHENTICATION_CALLBACK_EX = Pointer;
45
+ export type PFN_BLUETOOTH_ENUM_ATTRIBUTES_CALLBACK = Pointer;
46
+ export type PGUID = Pointer;
47
+ export type PHBLUETOOTH_AUTHENTICATION_REGISTRATION = Pointer;
48
+ export type PHBLUETOOTH_CONTAINER_ELEMENT = Pointer;
49
+ export type PSDP_ELEMENT_DATA = Pointer;
50
+ export type PSDP_STRING_TYPE_DATA = Pointer;
51
+ export type PUSHORT = Pointer;
52
+ export type UCHAR = number;