Skip to content

Commit 1ed844d

Browse files
committed
feat: add BLE write polyfill for writeValueWithResponse and writeValueWithoutResponse
1 parent 1f81fbb commit 1ed844d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/bleWritePolyfill.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Polyfill for writeValueWithResponse and writeValueWithoutResponse
2+
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
if (typeof (window as any).BluetoothRemoteGATTCharacteristic !== 'undefined') {
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
const proto = (window as any).BluetoothRemoteGATTCharacteristic.prototype as any;
7+
8+
if (!proto.writeValueWithResponse) {
9+
proto.writeValueWithResponse = function (value: BufferSource) {
10+
// Fallback to writeValue if available
11+
if (typeof this.writeValue === 'function') {
12+
return this.writeValue(value);
13+
}
14+
return Promise.reject(
15+
new Error(
16+
'writeValueWithResponse and writeValue are not supported on this device',
17+
),
18+
);
19+
};
20+
}
21+
22+
if (!proto.writeValueWithoutResponse) {
23+
proto.writeValueWithoutResponse = function (value: BufferSource) {
24+
// Fallback to writeValue if available
25+
if (typeof this.writeValue === 'function') {
26+
return this.writeValue(value);
27+
}
28+
return Promise.reject(
29+
new Error(
30+
'writeValueWithoutResponse and writeValue are not supported on this device',
31+
),
32+
);
33+
};
34+
}
35+
}
36+
37+
export {};
38+
// --- IGNORE ---

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) 2020-2023 The Pybricks Authors
33

4+
import './bleWritePolyfill';
5+
46
import './index.scss';
57
import { HotkeysProvider, OverlayToaster } from '@blueprintjs/core';
68
import { configureStore } from '@reduxjs/toolkit';

0 commit comments

Comments
 (0)