@onekeyfe/hd-transport-web-device 1.2.11-alpha.13 → 1.2.11-alpha.15
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.
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import transport, { PROTOCOL_V2_CHANNEL_BLE_UART, bytesToHex } from '@onekeyfe/hd-transport';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
EBleDisconnectReason,
|
|
4
|
+
ERRORS,
|
|
5
|
+
HardwareErrorCode,
|
|
6
|
+
createDeferred,
|
|
7
|
+
} from '@onekeyfe/hd-shared';
|
|
3
8
|
import EventEmitter from 'events';
|
|
4
9
|
|
|
5
10
|
import ElectronBleTransport from '../src/electron-ble-transport';
|
|
@@ -1449,4 +1454,194 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1449
1454
|
expect(nobleBle.connect).not.toHaveBeenCalled();
|
|
1450
1455
|
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
1451
1456
|
});
|
|
1457
|
+
|
|
1458
|
+
test('wakes a sleeping legacy V1 Classic with Initialize after silent GetFeatures without probing V2', async () => {
|
|
1459
|
+
const device = { id: 'sleeping-classic-id', name: 'K8141' };
|
|
1460
|
+
const nobleBle = createNobleBle(device);
|
|
1461
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1462
|
+
const probes = bleTransport as any;
|
|
1463
|
+
const probeProtocolV1 = jest.spyOn(probes, 'probeProtocolV1').mockResolvedValue(false);
|
|
1464
|
+
const probeProtocolV2 = jest.spyOn(probes, 'probeProtocolV2').mockResolvedValue(false);
|
|
1465
|
+
const callProtocolV1 = jest.spyOn(probes, 'callProtocolV1').mockResolvedValue({
|
|
1466
|
+
type: 'Features',
|
|
1467
|
+
message: {},
|
|
1468
|
+
});
|
|
1469
|
+
|
|
1470
|
+
await expect(bleTransport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
1471
|
+
uuid: device.id,
|
|
1472
|
+
protocolType: 'V1',
|
|
1473
|
+
});
|
|
1474
|
+
|
|
1475
|
+
expect(probeProtocolV1).toHaveBeenCalledTimes(1);
|
|
1476
|
+
expect(callProtocolV1).toHaveBeenCalledTimes(1);
|
|
1477
|
+
expect(callProtocolV1.mock.calls[0]?.[1]).toBe('Initialize');
|
|
1478
|
+
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
1479
|
+
});
|
|
1480
|
+
|
|
1481
|
+
test('skips Initialize wake on Electron BLE when onlyConnectBleDevice is true', async () => {
|
|
1482
|
+
const device = { id: 'only-connect-classic-id', name: 'K1361' };
|
|
1483
|
+
const nobleBle = createNobleBle(device);
|
|
1484
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1485
|
+
const probes = bleTransport as any;
|
|
1486
|
+
jest.spyOn(probes, 'probeProtocolV1').mockResolvedValue(false);
|
|
1487
|
+
jest.spyOn(probes, 'probeProtocolV2').mockResolvedValue(false);
|
|
1488
|
+
const callProtocolV1 = jest.spyOn(probes, 'callProtocolV1').mockResolvedValue({
|
|
1489
|
+
type: 'Features',
|
|
1490
|
+
message: {},
|
|
1491
|
+
});
|
|
1492
|
+
|
|
1493
|
+
await expect(
|
|
1494
|
+
bleTransport.acquire({ uuid: device.id, onlyConnectBleDevice: true })
|
|
1495
|
+
).rejects.toBeDefined();
|
|
1496
|
+
expect(callProtocolV1).not.toHaveBeenCalled();
|
|
1497
|
+
});
|
|
1498
|
+
|
|
1499
|
+
test('propagates Windows pairing rejection from GetFeatures without consuming the wake for later reconnects', async () => {
|
|
1500
|
+
const device = { id: 'win-pairing-classic-id', name: 'K8141' };
|
|
1501
|
+
const nobleBle = createNobleBle(device);
|
|
1502
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1503
|
+
const probes = bleTransport as any;
|
|
1504
|
+
const probeProtocolV2 = jest.spyOn(probes, 'probeProtocolV2').mockResolvedValue(false);
|
|
1505
|
+
|
|
1506
|
+
// Attempt 1: host aborts GetFeatures write after Windows pairing rejection.
|
|
1507
|
+
nobleBle.write.mockRejectedValueOnce(
|
|
1508
|
+
ERRORS.TypedError(
|
|
1509
|
+
HardwareErrorCode.BleConnectedError,
|
|
1510
|
+
'Write failed with abortable error for device win-pairing-classic-id: status: 3'
|
|
1511
|
+
)
|
|
1512
|
+
);
|
|
1513
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1514
|
+
errorCode: HardwareErrorCode.BleConnectedError,
|
|
1515
|
+
});
|
|
1516
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1517
|
+
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
1518
|
+
|
|
1519
|
+
// Attempt 2: paired successfully, GetFeatures still silent (device asleep), so the
|
|
1520
|
+
// preserved wake sends a one-shot Initialize and confirms V1.
|
|
1521
|
+
nobleBle.write.mockClear();
|
|
1522
|
+
jest.spyOn(probes, 'probeProtocolV1').mockResolvedValueOnce(false);
|
|
1523
|
+
const callProtocolV1 = jest.spyOn(probes, 'callProtocolV1').mockResolvedValueOnce({
|
|
1524
|
+
type: 'Features',
|
|
1525
|
+
message: {},
|
|
1526
|
+
});
|
|
1527
|
+
|
|
1528
|
+
await expect(bleTransport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
1529
|
+
uuid: device.id,
|
|
1530
|
+
protocolType: 'V1',
|
|
1531
|
+
});
|
|
1532
|
+
expect(callProtocolV1).toHaveBeenCalledWith(
|
|
1533
|
+
device.id,
|
|
1534
|
+
'Initialize',
|
|
1535
|
+
{},
|
|
1536
|
+
{ timeoutMs: 3000 },
|
|
1537
|
+
{ oneShot: true }
|
|
1538
|
+
);
|
|
1539
|
+
});
|
|
1540
|
+
|
|
1541
|
+
test('sends Initialize wake as a single one-shot native write and preserves wake when host proves write never started', async () => {
|
|
1542
|
+
const device = { id: 'oneshot-classic-id', name: 'K1361' };
|
|
1543
|
+
const nobleBle = createNobleBle(device);
|
|
1544
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1545
|
+
const probes = bleTransport as any;
|
|
1546
|
+
jest.spyOn(probes, 'probeProtocolV1').mockResolvedValue(false);
|
|
1547
|
+
jest.spyOn(probes, 'probeProtocolV2').mockResolvedValue(false);
|
|
1548
|
+
|
|
1549
|
+
// Attempt 1: host rejects Initialize before submitting a GATT write.
|
|
1550
|
+
nobleBle.write.mockRejectedValueOnce(
|
|
1551
|
+
ERRORS.TypedError(
|
|
1552
|
+
HardwareErrorCode.BleCharacteristicNotFound,
|
|
1553
|
+
'Device oneshot-classic-id not connected or characteristics not available'
|
|
1554
|
+
)
|
|
1555
|
+
);
|
|
1556
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1557
|
+
errorCode: HardwareErrorCode.BleCharacteristicNotFound,
|
|
1558
|
+
});
|
|
1559
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1560
|
+
expect(nobleBle.write.mock.calls[0]?.[2]).toEqual({ oneShot: true });
|
|
1561
|
+
|
|
1562
|
+
// Attempt 2: wake is still armed and executes one native write with { oneShot: true }.
|
|
1563
|
+
nobleBle.write.mockClear();
|
|
1564
|
+
let notifyHandler: ((id: string, hex: string) => void) | undefined;
|
|
1565
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
1566
|
+
notifyHandler = handler;
|
|
1567
|
+
return jest.fn();
|
|
1568
|
+
});
|
|
1569
|
+
nobleBle.write.mockImplementationOnce((id: string) => {
|
|
1570
|
+
const reply = ProtocolV1.encodeTransportPackets(schemas.protocolV1, 'Success', {
|
|
1571
|
+
message: 'ok',
|
|
1572
|
+
});
|
|
1573
|
+
const packetHex = reply[0].toString('hex');
|
|
1574
|
+
const payloadLen = parseInt(packetHex.slice(10, 18), 16);
|
|
1575
|
+
notifyHandler?.(id, packetHex.slice(0, (9 + payloadLen) * 2));
|
|
1576
|
+
return Promise.resolve();
|
|
1577
|
+
});
|
|
1578
|
+
|
|
1579
|
+
await expect(bleTransport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
1580
|
+
uuid: device.id,
|
|
1581
|
+
protocolType: 'V1',
|
|
1582
|
+
});
|
|
1583
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1584
|
+
expect(nobleBle.write.mock.calls[0]?.[2]).toEqual({ oneShot: true });
|
|
1585
|
+
});
|
|
1586
|
+
|
|
1587
|
+
test('sends initial V1 GetFeatures probe with oneShot and rethrows TransportCallInProgress without waking', async () => {
|
|
1588
|
+
const device = { id: 'oneshot-getfeatures-id', name: 'K8141' };
|
|
1589
|
+
const nobleBle = createNobleBle(device);
|
|
1590
|
+
let notifyHandler: ((id: string, hex: string) => void) | undefined;
|
|
1591
|
+
nobleBle.onNotification.mockImplementation(handler => {
|
|
1592
|
+
notifyHandler = handler;
|
|
1593
|
+
return jest.fn();
|
|
1594
|
+
});
|
|
1595
|
+
nobleBle.write.mockImplementationOnce((id: string) => {
|
|
1596
|
+
const reply = ProtocolV1.encodeTransportPackets(schemas.protocolV1, 'Success', {
|
|
1597
|
+
message: 'ok',
|
|
1598
|
+
});
|
|
1599
|
+
const packetHex = reply[0].toString('hex');
|
|
1600
|
+
const payloadLen = parseInt(packetHex.slice(10, 18), 16);
|
|
1601
|
+
notifyHandler?.(id, packetHex.slice(0, (9 + payloadLen) * 2));
|
|
1602
|
+
return Promise.resolve();
|
|
1603
|
+
});
|
|
1604
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1605
|
+
|
|
1606
|
+
await expect(bleTransport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
1607
|
+
uuid: device.id,
|
|
1608
|
+
protocolType: 'V1',
|
|
1609
|
+
});
|
|
1610
|
+
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1611
|
+
expect(nobleBle.write.mock.calls[0]?.[2]).toEqual({ oneShot: true });
|
|
1612
|
+
|
|
1613
|
+
await bleTransport.release(device.id);
|
|
1614
|
+
|
|
1615
|
+
const probes = bleTransport as any;
|
|
1616
|
+
const inProgressErr = ERRORS.TypedError(HardwareErrorCode.TransportCallInProgress);
|
|
1617
|
+
const callSpy = jest.spyOn(probes, 'callProtocolV1').mockRejectedValueOnce(inProgressErr);
|
|
1618
|
+
const wakeSpy = jest.spyOn(probes, 'wakeSilentProtocolV1Device');
|
|
1619
|
+
|
|
1620
|
+
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1621
|
+
errorCode: HardwareErrorCode.TransportCallInProgress,
|
|
1622
|
+
});
|
|
1623
|
+
expect(callSpy).toHaveBeenCalledTimes(1);
|
|
1624
|
+
expect(wakeSpy).not.toHaveBeenCalled();
|
|
1625
|
+
});
|
|
1626
|
+
|
|
1627
|
+
test('skips Electron Initialize wake if abortSignal aborts during probe state reset', async () => {
|
|
1628
|
+
const device = { id: 'abort-reset-classic-id', name: 'K8141' };
|
|
1629
|
+
const nobleBle = createNobleBle(device);
|
|
1630
|
+
const bleTransport = configureTransport(nobleBle);
|
|
1631
|
+
const probes = bleTransport as any;
|
|
1632
|
+
const abortController = new AbortController();
|
|
1633
|
+
jest.spyOn(probes, 'probeProtocolV1').mockResolvedValue(false);
|
|
1634
|
+
jest.spyOn(probes, 'resetProbeStateAfterProtocolProbe').mockImplementation(() => {
|
|
1635
|
+
abortController.abort();
|
|
1636
|
+
return Promise.resolve();
|
|
1637
|
+
});
|
|
1638
|
+
const callProtocolV1 = jest.spyOn(probes, 'callProtocolV1');
|
|
1639
|
+
|
|
1640
|
+
await expect(
|
|
1641
|
+
bleTransport.acquire({ uuid: device.id, abortSignal: abortController.signal })
|
|
1642
|
+
).rejects.toMatchObject({
|
|
1643
|
+
errorCode: HardwareErrorCode.DeviceInterruptedFromUser,
|
|
1644
|
+
});
|
|
1645
|
+
expect(callProtocolV1).not.toHaveBeenCalled();
|
|
1646
|
+
});
|
|
1452
1647
|
});
|
|
@@ -13,6 +13,8 @@ export type BleAcquireInput = {
|
|
|
13
13
|
forceCleanRunPromise?: boolean;
|
|
14
14
|
expectedProtocol?: ProtocolType;
|
|
15
15
|
protocolHint?: ProtocolType;
|
|
16
|
+
onlyConnectBleDevice?: boolean;
|
|
17
|
+
abortSignal?: AbortSignal;
|
|
16
18
|
};
|
|
17
19
|
export default class ElectronBleTransport {
|
|
18
20
|
private _messages;
|
|
@@ -32,6 +34,7 @@ export default class ElectronBleTransport {
|
|
|
32
34
|
private stopPromise?;
|
|
33
35
|
private deviceProtocol;
|
|
34
36
|
private deviceProtocolHints;
|
|
37
|
+
private silentDetections;
|
|
35
38
|
private deviceMtus;
|
|
36
39
|
private devicePacketCapacities;
|
|
37
40
|
private v1Buffers;
|
|
@@ -77,6 +80,7 @@ export default class ElectronBleTransport {
|
|
|
77
80
|
private createProtocolDetectionError;
|
|
78
81
|
private clearProbeProtocol;
|
|
79
82
|
private detectProtocol;
|
|
83
|
+
private wakeSilentProtocolV1Device;
|
|
80
84
|
private createNotificationSubscription;
|
|
81
85
|
private resetProbeStateAfterProtocolProbe;
|
|
82
86
|
private probeProtocolV1;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":"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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -237,6 +237,8 @@ type BleAcquireInput = {
|
|
|
237
237
|
forceCleanRunPromise?: boolean;
|
|
238
238
|
expectedProtocol?: ProtocolType;
|
|
239
239
|
protocolHint?: ProtocolType;
|
|
240
|
+
onlyConnectBleDevice?: boolean;
|
|
241
|
+
abortSignal?: AbortSignal;
|
|
240
242
|
};
|
|
241
243
|
/**
|
|
242
244
|
* Desktop Electron BLE transport with automatic Protocol V1/V2 detection.
|
|
@@ -269,6 +271,7 @@ declare class ElectronBleTransport {
|
|
|
269
271
|
private stopPromise?;
|
|
270
272
|
private deviceProtocol;
|
|
271
273
|
private deviceProtocolHints;
|
|
274
|
+
private silentDetections;
|
|
272
275
|
private deviceMtus;
|
|
273
276
|
private devicePacketCapacities;
|
|
274
277
|
private v1Buffers;
|
|
@@ -328,6 +331,7 @@ declare class ElectronBleTransport {
|
|
|
328
331
|
private createProtocolDetectionError;
|
|
329
332
|
private clearProbeProtocol;
|
|
330
333
|
private detectProtocol;
|
|
334
|
+
private wakeSilentProtocolV1Device;
|
|
331
335
|
private createNotificationSubscription;
|
|
332
336
|
private resetProbeStateAfterProtocolProbe;
|
|
333
337
|
private probeProtocolV1;
|
package/dist/index.js
CHANGED
|
@@ -1524,6 +1524,31 @@ const BLE_PACKET_SIZE_MAXIMUM = 244;
|
|
|
1524
1524
|
const BLE_WRITE_DELAY_MS = 5;
|
|
1525
1525
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
1526
1526
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
1527
|
+
const shouldRethrowElectronProtocolProbeError = (error) => {
|
|
1528
|
+
if (transport.isProtocolV2LinkDisabledError(error)) {
|
|
1529
|
+
return true;
|
|
1530
|
+
}
|
|
1531
|
+
const code = error === null || error === void 0 ? void 0 : error.errorCode;
|
|
1532
|
+
return (code === hdShared.HardwareErrorCode.TransportCallInProgress ||
|
|
1533
|
+
code === hdShared.HardwareErrorCode.BleConnectedError ||
|
|
1534
|
+
code === hdShared.HardwareErrorCode.BleDeviceNotBonded ||
|
|
1535
|
+
code === hdShared.HardwareErrorCode.BleDeviceBondedCanceled ||
|
|
1536
|
+
code === hdShared.HardwareErrorCode.BleDeviceDisconnected ||
|
|
1537
|
+
code === hdShared.HardwareErrorCode.BleCharacteristicNotFound ||
|
|
1538
|
+
code === hdShared.HardwareErrorCode.BleCharacteristicNotifyError ||
|
|
1539
|
+
code === hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure ||
|
|
1540
|
+
code === hdShared.HardwareErrorCode.BleServiceNotFound ||
|
|
1541
|
+
code === hdShared.HardwareErrorCode.BlePoweredOff ||
|
|
1542
|
+
code === hdShared.HardwareErrorCode.BleUnsupported ||
|
|
1543
|
+
code === hdShared.HardwareErrorCode.BleLocationError);
|
|
1544
|
+
};
|
|
1545
|
+
const isUnstartedElectronWakeError = (error) => {
|
|
1546
|
+
const code = error === null || error === void 0 ? void 0 : error.errorCode;
|
|
1547
|
+
return (code === hdShared.HardwareErrorCode.BleCharacteristicNotFound ||
|
|
1548
|
+
code === hdShared.HardwareErrorCode.BlePoweredOff ||
|
|
1549
|
+
code === hdShared.HardwareErrorCode.BleUnsupported ||
|
|
1550
|
+
code === hdShared.HardwareErrorCode.BleLocationError);
|
|
1551
|
+
};
|
|
1527
1552
|
class ElectronBleTransport {
|
|
1528
1553
|
constructor() {
|
|
1529
1554
|
this.name = 'ElectronBleTransport';
|
|
@@ -1537,6 +1562,7 @@ class ElectronBleTransport {
|
|
|
1537
1562
|
this.stopped = false;
|
|
1538
1563
|
this.deviceProtocol = new Map();
|
|
1539
1564
|
this.deviceProtocolHints = new Map();
|
|
1565
|
+
this.silentDetections = new Map();
|
|
1540
1566
|
this.deviceMtus = new Map();
|
|
1541
1567
|
this.devicePacketCapacities = new Map();
|
|
1542
1568
|
this.v1Buffers = new Map();
|
|
@@ -1679,6 +1705,7 @@ class ElectronBleTransport {
|
|
|
1679
1705
|
yield Promise.all(Array.from(trackedDeviceIds, deviceId => this.releaseNative(deviceId)));
|
|
1680
1706
|
this.acquiringDevices.clear();
|
|
1681
1707
|
this.protocolV2CallCounts.clear();
|
|
1708
|
+
this.silentDetections.clear();
|
|
1682
1709
|
yield this.protocolV2Links
|
|
1683
1710
|
.dispose('Electron BLE transport stopped')
|
|
1684
1711
|
.catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] link dispose failed:', error); });
|
|
@@ -1816,7 +1843,11 @@ class ElectronBleTransport {
|
|
|
1816
1843
|
this.assertRunning();
|
|
1817
1844
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
1818
1845
|
this.notificationCleanups.set(uuid, cleanup);
|
|
1819
|
-
const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint
|
|
1846
|
+
const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint, {
|
|
1847
|
+
isKnownLegacyV1Device: hdShared.isLegacyProtocolV1BleName(device.name),
|
|
1848
|
+
onlyConnectBleDevice: input.onlyConnectBleDevice,
|
|
1849
|
+
abortSignal: input.abortSignal,
|
|
1850
|
+
});
|
|
1820
1851
|
this.assertRunning();
|
|
1821
1852
|
if (protocolType === 'V2') {
|
|
1822
1853
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug('[Electron BLE] Protocol V2 write configured', {
|
|
@@ -1943,18 +1974,21 @@ class ElectronBleTransport {
|
|
|
1943
1974
|
this.deviceProtocol.delete(uuid);
|
|
1944
1975
|
}
|
|
1945
1976
|
}
|
|
1946
|
-
detectProtocol(uuid, expectedProtocol, protocolHint) {
|
|
1947
|
-
var _a, _b, _c;
|
|
1977
|
+
detectProtocol(uuid, expectedProtocol, protocolHint, options = {}) {
|
|
1978
|
+
var _a, _b, _c, _d;
|
|
1948
1979
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1980
|
+
if ((_a = options.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
1981
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromUser);
|
|
1982
|
+
}
|
|
1949
1983
|
if (expectedProtocol === 'V1') {
|
|
1950
1984
|
this.deviceProtocol.set(uuid, 'V1');
|
|
1951
|
-
(
|
|
1985
|
+
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> V1 (expected, no probe)`);
|
|
1952
1986
|
return 'V1';
|
|
1953
1987
|
}
|
|
1954
1988
|
if (expectedProtocol === 'V2') {
|
|
1955
1989
|
if (yield this.probeProtocolV2(uuid, expectedProtocol)) {
|
|
1956
1990
|
this.deviceProtocol.set(uuid, 'V2');
|
|
1957
|
-
(
|
|
1991
|
+
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> V2 (expected)`);
|
|
1958
1992
|
return 'V2';
|
|
1959
1993
|
}
|
|
1960
1994
|
throw this.createProtocolMismatchError(expectedProtocol);
|
|
@@ -1967,18 +2001,82 @@ class ElectronBleTransport {
|
|
|
1967
2001
|
yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
1968
2002
|
this.assertRunning();
|
|
1969
2003
|
}
|
|
1970
|
-
|
|
2004
|
+
let detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
|
|
2005
|
+
if (!detected && protocol === 'V1') {
|
|
2006
|
+
detected = yield this.wakeSilentProtocolV1Device(uuid, probeOrder, options);
|
|
2007
|
+
}
|
|
1971
2008
|
this.assertRunning();
|
|
1972
2009
|
if (detected) {
|
|
1973
2010
|
this.deviceProtocol.set(uuid, protocol);
|
|
1974
|
-
|
|
2011
|
+
this.silentDetections.delete(uuid);
|
|
2012
|
+
(_d = this.Log) === null || _d === void 0 ? void 0 : _d.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
1975
2013
|
return protocol;
|
|
1976
2014
|
}
|
|
1977
2015
|
}
|
|
2016
|
+
if (!this.silentDetections.has(uuid)) {
|
|
2017
|
+
this.silentDetections.set(uuid, 'silent');
|
|
2018
|
+
}
|
|
1978
2019
|
this.deviceProtocol.delete(uuid);
|
|
1979
2020
|
throw this.createProtocolDetectionError();
|
|
1980
2021
|
});
|
|
1981
2022
|
}
|
|
2023
|
+
wakeSilentProtocolV1Device(uuid, probeOrder, options = {}) {
|
|
2024
|
+
var _a, _b, _c, _d, _e;
|
|
2025
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2026
|
+
if (options.onlyConnectBleDevice ||
|
|
2027
|
+
probeOrder[0] !== 'V1' ||
|
|
2028
|
+
this.silentDetections.get(uuid) === 'woken' ||
|
|
2029
|
+
(!options.isKnownLegacyV1Device && this.silentDetections.get(uuid) !== 'silent')) {
|
|
2030
|
+
return false;
|
|
2031
|
+
}
|
|
2032
|
+
if ((_a = options.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
2033
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromUser);
|
|
2034
|
+
}
|
|
2035
|
+
this.assertRunning();
|
|
2036
|
+
yield this.resetProbeStateAfterProtocolProbe(uuid, 'V1');
|
|
2037
|
+
if ((_b = options.abortSignal) === null || _b === void 0 ? void 0 : _b.aborted) {
|
|
2038
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromUser);
|
|
2039
|
+
}
|
|
2040
|
+
this.assertRunning();
|
|
2041
|
+
const previousSilentState = this.silentDetections.get(uuid);
|
|
2042
|
+
this.silentDetections.set(uuid, 'woken');
|
|
2043
|
+
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug('[Electron BLE] sending Protocol V1 Initialize wake', {
|
|
2044
|
+
connectIdSuffix: uuid.slice(-8),
|
|
2045
|
+
source: options.isKnownLegacyV1Device ? 'legacy-v1-name' : 'silent-retry',
|
|
2046
|
+
});
|
|
2047
|
+
let answeredV1 = false;
|
|
2048
|
+
try {
|
|
2049
|
+
this.deviceProtocol.set(uuid, 'V1');
|
|
2050
|
+
const response = yield this.callProtocolV1(uuid, 'Initialize', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS }, { oneShot: true });
|
|
2051
|
+
answeredV1 = typeof (response === null || response === void 0 ? void 0 : response.type) === 'string';
|
|
2052
|
+
}
|
|
2053
|
+
catch (error) {
|
|
2054
|
+
this.clearProbeProtocol(uuid, 'V1');
|
|
2055
|
+
if (isUnstartedElectronWakeError(error)) {
|
|
2056
|
+
if (previousSilentState) {
|
|
2057
|
+
this.silentDetections.set(uuid, previousSilentState);
|
|
2058
|
+
}
|
|
2059
|
+
else {
|
|
2060
|
+
this.silentDetections.delete(uuid);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
(_d = this.Log) === null || _d === void 0 ? void 0 : _d.debug('[Electron BLE] Protocol V1 Initialize wake failed:', error);
|
|
2064
|
+
if (shouldRethrowElectronProtocolProbeError(error)) {
|
|
2065
|
+
throw error;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
if (answeredV1) {
|
|
2069
|
+
return true;
|
|
2070
|
+
}
|
|
2071
|
+
if ((_e = options.abortSignal) === null || _e === void 0 ? void 0 : _e.aborted) {
|
|
2072
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInterruptedFromUser);
|
|
2073
|
+
}
|
|
2074
|
+
this.assertRunning();
|
|
2075
|
+
yield this.resetProbeStateAfterProtocolProbe(uuid, 'V1');
|
|
2076
|
+
this.assertRunning();
|
|
2077
|
+
return this.probeProtocolV1(uuid);
|
|
2078
|
+
});
|
|
2079
|
+
}
|
|
1982
2080
|
createNotificationSubscription(uuid) {
|
|
1983
2081
|
var _a;
|
|
1984
2082
|
if (!((_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle)) {
|
|
@@ -2020,13 +2118,13 @@ class ElectronBleTransport {
|
|
|
2020
2118
|
}
|
|
2021
2119
|
try {
|
|
2022
2120
|
this.deviceProtocol.set(uuid, 'V1');
|
|
2023
|
-
yield this.callProtocolV1(uuid, 'GetFeatures', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS });
|
|
2121
|
+
yield this.callProtocolV1(uuid, 'GetFeatures', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS }, { oneShot: true });
|
|
2024
2122
|
return true;
|
|
2025
2123
|
}
|
|
2026
2124
|
catch (error) {
|
|
2027
2125
|
this.clearProbeProtocol(uuid, 'V1');
|
|
2028
2126
|
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
2029
|
-
if (
|
|
2127
|
+
if (shouldRethrowElectronProtocolProbeError(error)) {
|
|
2030
2128
|
throw error;
|
|
2031
2129
|
}
|
|
2032
2130
|
return false;
|
|
@@ -2273,7 +2371,7 @@ class ElectronBleTransport {
|
|
|
2273
2371
|
yield this.callProtocolV1(uuid, name, data);
|
|
2274
2372
|
});
|
|
2275
2373
|
}
|
|
2276
|
-
callProtocolV1(uuid, name, data, options) {
|
|
2374
|
+
callProtocolV1(uuid, name, data, options, writeOptions) {
|
|
2277
2375
|
var _a, _b;
|
|
2278
2376
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2279
2377
|
if (!this._messages) {
|
|
@@ -2303,7 +2401,9 @@ class ElectronBleTransport {
|
|
|
2303
2401
|
if (hexString.length === 0) {
|
|
2304
2402
|
throw new Error(`Buffer ${i + 1} is empty`);
|
|
2305
2403
|
}
|
|
2306
|
-
yield invokeNobleBle(
|
|
2404
|
+
yield invokeNobleBle(writeOptions
|
|
2405
|
+
? window.desktopApi.nobleBle.write(uuid, hexString, writeOptions)
|
|
2406
|
+
: window.desktopApi.nobleBle.write(uuid, hexString));
|
|
2307
2407
|
}
|
|
2308
2408
|
const response = yield Promise.race([
|
|
2309
2409
|
runPromise.promise,
|
|
@@ -2325,7 +2425,8 @@ class ElectronBleTransport {
|
|
|
2325
2425
|
}
|
|
2326
2426
|
catch (e) {
|
|
2327
2427
|
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.error('[Electron BLE] Protocol V1 call error:', e);
|
|
2328
|
-
const isProbeTimeout = name === 'GetFeatures'
|
|
2428
|
+
const isProbeTimeout = (name === 'GetFeatures' || name === 'Initialize') &&
|
|
2429
|
+
(options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS;
|
|
2329
2430
|
if ((e === null || e === void 0 ? void 0 : e.errorCode) === hdShared.HardwareErrorCode.BleTimeoutError) {
|
|
2330
2431
|
this.v1Buffers.set(uuid, { buffer: [], bufferLength: 0 });
|
|
2331
2432
|
const notifyCleanup = this.notificationCleanups.get(uuid);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-web-device",
|
|
3
|
-
"version": "1.2.11-alpha.
|
|
3
|
+
"version": "1.2.11-alpha.15",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
"lint:fix": "eslint . --fix"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.15",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.15",
|
|
26
26
|
"bytebuffer": "^5.0.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.
|
|
29
|
+
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.15",
|
|
30
30
|
"@types/w3c-web-usb": "^1.0.6",
|
|
31
31
|
"@types/web-bluetooth": "^0.0.17"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "9cf72875d3f29f904f1e337f80a2f2e20bf88aed"
|
|
34
34
|
}
|
|
@@ -19,12 +19,17 @@ import {
|
|
|
19
19
|
HardwareErrorCodeMessage,
|
|
20
20
|
createDeferred,
|
|
21
21
|
isHeaderChunk,
|
|
22
|
+
isLegacyProtocolV1BleName,
|
|
22
23
|
} from '@onekeyfe/hd-shared';
|
|
23
24
|
|
|
24
25
|
import { resolveBlePacketCapacity } from './ble-packet-capacity';
|
|
25
26
|
|
|
26
27
|
import type { Deferred } from '@onekeyfe/hd-shared';
|
|
27
|
-
import type {
|
|
28
|
+
import type {
|
|
29
|
+
DesktopAPI,
|
|
30
|
+
NobleBleIpcErrorResponse,
|
|
31
|
+
NobleBleWriteOptions,
|
|
32
|
+
} from '@onekeyfe/hd-transport-electron';
|
|
28
33
|
import type {
|
|
29
34
|
AcquireInput,
|
|
30
35
|
OneKeyDeviceInfo,
|
|
@@ -47,6 +52,8 @@ export type BleAcquireInput = {
|
|
|
47
52
|
forceCleanRunPromise?: boolean;
|
|
48
53
|
expectedProtocol?: ProtocolType;
|
|
49
54
|
protocolHint?: ProtocolType;
|
|
55
|
+
onlyConnectBleDevice?: boolean;
|
|
56
|
+
abortSignal?: AbortSignal;
|
|
50
57
|
};
|
|
51
58
|
|
|
52
59
|
interface PacketProcessResult {
|
|
@@ -91,6 +98,40 @@ const BLE_WRITE_DELAY_MS = 5;
|
|
|
91
98
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
92
99
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
93
100
|
|
|
101
|
+
const shouldRethrowElectronProtocolProbeError = (error: unknown): boolean => {
|
|
102
|
+
if (isProtocolV2LinkDisabledError(error)) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
const code = (error as { errorCode?: unknown })?.errorCode;
|
|
106
|
+
// Pairing rejections, disconnects, and missing GATT state are link failures rather than
|
|
107
|
+
// evidence of a protocol mismatch. Rethrow them instead of entering wake or probing V2.
|
|
108
|
+
return (
|
|
109
|
+
code === HardwareErrorCode.TransportCallInProgress ||
|
|
110
|
+
code === HardwareErrorCode.BleConnectedError ||
|
|
111
|
+
code === HardwareErrorCode.BleDeviceNotBonded ||
|
|
112
|
+
code === HardwareErrorCode.BleDeviceBondedCanceled ||
|
|
113
|
+
code === HardwareErrorCode.BleDeviceDisconnected ||
|
|
114
|
+
code === HardwareErrorCode.BleCharacteristicNotFound ||
|
|
115
|
+
code === HardwareErrorCode.BleCharacteristicNotifyError ||
|
|
116
|
+
code === HardwareErrorCode.BleCharacteristicNotifyChangeFailure ||
|
|
117
|
+
code === HardwareErrorCode.BleServiceNotFound ||
|
|
118
|
+
code === HardwareErrorCode.BlePoweredOff ||
|
|
119
|
+
code === HardwareErrorCode.BleUnsupported ||
|
|
120
|
+
code === HardwareErrorCode.BleLocationError
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const isUnstartedElectronWakeError = (error: unknown): boolean => {
|
|
125
|
+
const code = (error as { errorCode?: unknown })?.errorCode;
|
|
126
|
+
// Errors returned before a GATT write is submitted to the peripheral.
|
|
127
|
+
return (
|
|
128
|
+
code === HardwareErrorCode.BleCharacteristicNotFound ||
|
|
129
|
+
code === HardwareErrorCode.BlePoweredOff ||
|
|
130
|
+
code === HardwareErrorCode.BleUnsupported ||
|
|
131
|
+
code === HardwareErrorCode.BleLocationError
|
|
132
|
+
);
|
|
133
|
+
};
|
|
134
|
+
|
|
94
135
|
/**
|
|
95
136
|
* Desktop Electron BLE transport with automatic Protocol V1/V2 detection.
|
|
96
137
|
*
|
|
@@ -139,6 +180,8 @@ export default class ElectronBleTransport {
|
|
|
139
180
|
|
|
140
181
|
private deviceProtocolHints: Map<string, ProtocolType> = new Map();
|
|
141
182
|
|
|
183
|
+
private silentDetections = new Map<string, 'silent' | 'woken'>();
|
|
184
|
+
|
|
142
185
|
private deviceMtus: Map<string, number> = new Map();
|
|
143
186
|
|
|
144
187
|
private devicePacketCapacities: Map<string, number> = new Map();
|
|
@@ -322,6 +365,7 @@ export default class ElectronBleTransport {
|
|
|
322
365
|
await Promise.all(Array.from(trackedDeviceIds, deviceId => this.releaseNative(deviceId)));
|
|
323
366
|
this.acquiringDevices.clear();
|
|
324
367
|
this.protocolV2CallCounts.clear();
|
|
368
|
+
this.silentDetections.clear();
|
|
325
369
|
await this.protocolV2Links
|
|
326
370
|
.dispose('Electron BLE transport stopped')
|
|
327
371
|
.catch(error => this.Log?.debug('[Electron BLE] link dispose failed:', error));
|
|
@@ -493,7 +537,11 @@ export default class ElectronBleTransport {
|
|
|
493
537
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
494
538
|
this.notificationCleanups.set(uuid, cleanup);
|
|
495
539
|
|
|
496
|
-
const protocolType = await this.detectProtocol(uuid, expectedProtocol, protocolHint
|
|
540
|
+
const protocolType = await this.detectProtocol(uuid, expectedProtocol, protocolHint, {
|
|
541
|
+
isKnownLegacyV1Device: isLegacyProtocolV1BleName(device.name),
|
|
542
|
+
onlyConnectBleDevice: input.onlyConnectBleDevice,
|
|
543
|
+
abortSignal: input.abortSignal,
|
|
544
|
+
});
|
|
497
545
|
this.assertRunning();
|
|
498
546
|
if (protocolType === 'V2') {
|
|
499
547
|
this.Log?.debug('[Electron BLE] Protocol V2 write configured', {
|
|
@@ -633,8 +681,17 @@ export default class ElectronBleTransport {
|
|
|
633
681
|
private async detectProtocol(
|
|
634
682
|
uuid: string,
|
|
635
683
|
expectedProtocol?: ProtocolType,
|
|
636
|
-
protocolHint?: ProtocolType
|
|
684
|
+
protocolHint?: ProtocolType,
|
|
685
|
+
options: {
|
|
686
|
+
isKnownLegacyV1Device?: boolean;
|
|
687
|
+
onlyConnectBleDevice?: boolean;
|
|
688
|
+
abortSignal?: AbortSignal;
|
|
689
|
+
} = {}
|
|
637
690
|
): Promise<ProtocolType> {
|
|
691
|
+
if (options.abortSignal?.aborted) {
|
|
692
|
+
throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromUser);
|
|
693
|
+
}
|
|
694
|
+
|
|
638
695
|
// A declared V1 is taken at face value, as the React Native transport
|
|
639
696
|
// already does on iOS: the caller reads the protocol off its own device
|
|
640
697
|
// record, so probing re-asks a question that is already answered and adds a
|
|
@@ -674,20 +731,102 @@ export default class ElectronBleTransport {
|
|
|
674
731
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
675
732
|
this.assertRunning();
|
|
676
733
|
}
|
|
677
|
-
|
|
734
|
+
let detected =
|
|
678
735
|
protocol === 'V1' ? await this.probeProtocolV1(uuid) : await this.probeProtocolV2(uuid);
|
|
736
|
+
if (!detected && protocol === 'V1') {
|
|
737
|
+
detected = await this.wakeSilentProtocolV1Device(uuid, probeOrder, options);
|
|
738
|
+
}
|
|
679
739
|
this.assertRunning();
|
|
680
740
|
if (detected) {
|
|
681
741
|
this.deviceProtocol.set(uuid, protocol);
|
|
742
|
+
this.silentDetections.delete(uuid);
|
|
682
743
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
683
744
|
return protocol;
|
|
684
745
|
}
|
|
685
746
|
}
|
|
686
747
|
|
|
748
|
+
if (!this.silentDetections.has(uuid)) {
|
|
749
|
+
this.silentDetections.set(uuid, 'silent');
|
|
750
|
+
}
|
|
687
751
|
this.deviceProtocol.delete(uuid);
|
|
688
752
|
throw this.createProtocolDetectionError();
|
|
689
753
|
}
|
|
690
754
|
|
|
755
|
+
private async wakeSilentProtocolV1Device(
|
|
756
|
+
uuid: string,
|
|
757
|
+
probeOrder: ProtocolType[],
|
|
758
|
+
options: {
|
|
759
|
+
isKnownLegacyV1Device?: boolean;
|
|
760
|
+
onlyConnectBleDevice?: boolean;
|
|
761
|
+
abortSignal?: AbortSignal;
|
|
762
|
+
} = {}
|
|
763
|
+
): Promise<boolean> {
|
|
764
|
+
if (
|
|
765
|
+
options.onlyConnectBleDevice ||
|
|
766
|
+
probeOrder[0] !== 'V1' ||
|
|
767
|
+
this.silentDetections.get(uuid) === 'woken' ||
|
|
768
|
+
(!options.isKnownLegacyV1Device && this.silentDetections.get(uuid) !== 'silent')
|
|
769
|
+
) {
|
|
770
|
+
return false;
|
|
771
|
+
}
|
|
772
|
+
if (options.abortSignal?.aborted) {
|
|
773
|
+
throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromUser);
|
|
774
|
+
}
|
|
775
|
+
this.assertRunning();
|
|
776
|
+
|
|
777
|
+
// A timed-out GetFeatures probe retires the renderer notification listener;
|
|
778
|
+
// re-attach it on the live physical link before sending Initialize.
|
|
779
|
+
await this.resetProbeStateAfterProtocolProbe(uuid, 'V1');
|
|
780
|
+
if (options.abortSignal?.aborted) {
|
|
781
|
+
throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromUser);
|
|
782
|
+
}
|
|
783
|
+
this.assertRunning();
|
|
784
|
+
|
|
785
|
+
const previousSilentState = this.silentDetections.get(uuid);
|
|
786
|
+
this.silentDetections.set(uuid, 'woken');
|
|
787
|
+
this.Log?.debug('[Electron BLE] sending Protocol V1 Initialize wake', {
|
|
788
|
+
connectIdSuffix: uuid.slice(-8),
|
|
789
|
+
source: options.isKnownLegacyV1Device ? 'legacy-v1-name' : 'silent-retry',
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
let answeredV1 = false;
|
|
793
|
+
try {
|
|
794
|
+
this.deviceProtocol.set(uuid, 'V1');
|
|
795
|
+
const response = await this.callProtocolV1(
|
|
796
|
+
uuid,
|
|
797
|
+
'Initialize',
|
|
798
|
+
{},
|
|
799
|
+
{ timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS },
|
|
800
|
+
{ oneShot: true }
|
|
801
|
+
);
|
|
802
|
+
answeredV1 = typeof response?.type === 'string';
|
|
803
|
+
} catch (error) {
|
|
804
|
+
this.clearProbeProtocol(uuid, 'V1');
|
|
805
|
+
if (isUnstartedElectronWakeError(error)) {
|
|
806
|
+
if (previousSilentState) {
|
|
807
|
+
this.silentDetections.set(uuid, previousSilentState);
|
|
808
|
+
} else {
|
|
809
|
+
this.silentDetections.delete(uuid);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
this.Log?.debug('[Electron BLE] Protocol V1 Initialize wake failed:', error);
|
|
813
|
+
if (shouldRethrowElectronProtocolProbeError(error)) {
|
|
814
|
+
throw error;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
if (answeredV1) {
|
|
819
|
+
return true;
|
|
820
|
+
}
|
|
821
|
+
if (options.abortSignal?.aborted) {
|
|
822
|
+
throw ERRORS.TypedError(HardwareErrorCode.DeviceInterruptedFromUser);
|
|
823
|
+
}
|
|
824
|
+
this.assertRunning();
|
|
825
|
+
await this.resetProbeStateAfterProtocolProbe(uuid, 'V1');
|
|
826
|
+
this.assertRunning();
|
|
827
|
+
return this.probeProtocolV1(uuid);
|
|
828
|
+
}
|
|
829
|
+
|
|
691
830
|
private createNotificationSubscription(uuid: string) {
|
|
692
831
|
if (!window.desktopApi?.nobleBle) {
|
|
693
832
|
throw new Error('Noble BLE API not available');
|
|
@@ -736,13 +875,21 @@ export default class ElectronBleTransport {
|
|
|
736
875
|
try {
|
|
737
876
|
this.deviceProtocol.set(uuid, 'V1');
|
|
738
877
|
// GetFeatures identifies Protocol V1 without resetting an existing wallet
|
|
739
|
-
// session before Core has a chance to restore a hidden wallet.
|
|
740
|
-
|
|
878
|
+
// session before Core has a chance to restore a hidden wallet. Use a one-shot
|
|
879
|
+
// native write so a fresh Windows link does not stall 28s in the 15-attempt
|
|
880
|
+
// pairing retry loop when a sleeping Classic silently ignores GetFeatures.
|
|
881
|
+
await this.callProtocolV1(
|
|
882
|
+
uuid,
|
|
883
|
+
'GetFeatures',
|
|
884
|
+
{},
|
|
885
|
+
{ timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS },
|
|
886
|
+
{ oneShot: true }
|
|
887
|
+
);
|
|
741
888
|
return true;
|
|
742
889
|
} catch (error) {
|
|
743
890
|
this.clearProbeProtocol(uuid, 'V1');
|
|
744
891
|
this.Log?.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
745
|
-
if (
|
|
892
|
+
if (shouldRethrowElectronProtocolProbeError(error)) {
|
|
746
893
|
throw error;
|
|
747
894
|
}
|
|
748
895
|
return false;
|
|
@@ -1016,7 +1163,8 @@ export default class ElectronBleTransport {
|
|
|
1016
1163
|
uuid: string,
|
|
1017
1164
|
name: string,
|
|
1018
1165
|
data: Record<string, unknown>,
|
|
1019
|
-
options?: TransportCallOptions
|
|
1166
|
+
options?: TransportCallOptions,
|
|
1167
|
+
writeOptions?: NobleBleWriteOptions
|
|
1020
1168
|
) {
|
|
1021
1169
|
if (!this._messages) {
|
|
1022
1170
|
throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
|
|
@@ -1049,7 +1197,11 @@ export default class ElectronBleTransport {
|
|
|
1049
1197
|
if (hexString.length === 0) {
|
|
1050
1198
|
throw new Error(`Buffer ${i + 1} is empty`);
|
|
1051
1199
|
}
|
|
1052
|
-
await invokeNobleBle(
|
|
1200
|
+
await invokeNobleBle(
|
|
1201
|
+
writeOptions
|
|
1202
|
+
? window.desktopApi.nobleBle.write(uuid, hexString, writeOptions)
|
|
1203
|
+
: window.desktopApi.nobleBle.write(uuid, hexString)
|
|
1204
|
+
);
|
|
1053
1205
|
}
|
|
1054
1206
|
|
|
1055
1207
|
const response = await Promise.race([
|
|
@@ -1076,7 +1228,8 @@ export default class ElectronBleTransport {
|
|
|
1076
1228
|
} catch (e) {
|
|
1077
1229
|
this.Log?.error('[Electron BLE] Protocol V1 call error:', e);
|
|
1078
1230
|
const isProbeTimeout =
|
|
1079
|
-
name === 'GetFeatures'
|
|
1231
|
+
(name === 'GetFeatures' || name === 'Initialize') &&
|
|
1232
|
+
options?.timeoutMs === PROTOCOL_PROBE_TIMEOUT_MS;
|
|
1080
1233
|
if ((e as { errorCode?: unknown })?.errorCode === HardwareErrorCode.BleTimeoutError) {
|
|
1081
1234
|
this.v1Buffers.set(uuid, { buffer: [], bufferLength: 0 });
|
|
1082
1235
|
const notifyCleanup = this.notificationCleanups.get(uuid);
|