@onekeyfe/hd-transport-electron 1.1.2-alpha.0 → 1.1.2-alpha.2
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.
|
@@ -32,7 +32,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
32
32
|
|
|
33
33
|
function initNobleBleSupport(webContents) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-
|
|
35
|
+
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-bf2b2a66.js'); });
|
|
36
36
|
setupNobleBleHandlers(webContents);
|
|
37
37
|
});
|
|
38
38
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var index = require('./index-
|
|
3
|
+
var index = require('./index-732ee055.js');
|
|
4
4
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
5
5
|
var hdTransport = require('@onekeyfe/hd-transport');
|
|
6
6
|
|
|
@@ -26,7 +26,6 @@ const connectedDevices = new Map();
|
|
|
26
26
|
const deviceCharacteristics = new Map();
|
|
27
27
|
const notificationCallbacks = new Map();
|
|
28
28
|
const subscribedDevices = new Map();
|
|
29
|
-
const subscriptionOperations = new Map();
|
|
30
29
|
const devicePacketStates = new Map();
|
|
31
30
|
const recentWriteOperations = new Map();
|
|
32
31
|
const WRITE_DISCONNECT_THRESHOLD = 1000;
|
|
@@ -57,13 +56,7 @@ function processNotificationData(deviceId, data) {
|
|
|
57
56
|
packetState.buffer = [...data.subarray(3)];
|
|
58
57
|
packetState.packetCount = 1;
|
|
59
58
|
packetState.messageId = messageId;
|
|
60
|
-
if (packetState.bufferLength
|
|
61
|
-
logger === null || logger === void 0 ? void 0 : logger.error('[NobleBLE] Invalid negative packet length detected:', {
|
|
62
|
-
length: packetState.bufferLength,
|
|
63
|
-
dataLength: data.length,
|
|
64
|
-
rawHeader: data.subarray(0, Math.min(16, data.length)).toString('hex'),
|
|
65
|
-
lengthBytes: data.subarray(5, 9).toString('hex'),
|
|
66
|
-
});
|
|
59
|
+
if (packetState.bufferLength <= 0) {
|
|
67
60
|
resetPacketState(packetState);
|
|
68
61
|
return { isComplete: false, error: 'Invalid packet length in header' };
|
|
69
62
|
}
|
|
@@ -212,19 +205,13 @@ function cleanupDeviceState(deviceId) {
|
|
|
212
205
|
devicePacketStates.delete(deviceId);
|
|
213
206
|
subscribedDevices.delete(deviceId);
|
|
214
207
|
recentWriteOperations.delete(deviceId);
|
|
215
|
-
subscriptionOperations.delete(deviceId);
|
|
216
208
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Device state cleaned up:', deviceId);
|
|
217
209
|
}
|
|
218
210
|
function handleDeviceDisconnect(deviceId, webContents) {
|
|
219
|
-
var _a
|
|
220
|
-
logger === null || logger === void 0 ? void 0 : logger.
|
|
221
|
-
deviceId,
|
|
222
|
-
hasPeripheral: connectedDevices.has(deviceId),
|
|
223
|
-
hasCharacteristics: deviceCharacteristics.has(deviceId),
|
|
224
|
-
stackTrace: (_a = new Error().stack) === null || _a === void 0 ? void 0 : _a.split('\n').slice(1, 5),
|
|
225
|
-
});
|
|
211
|
+
var _a;
|
|
212
|
+
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Device disconnected:', deviceId);
|
|
226
213
|
const peripheral = connectedDevices.get(deviceId);
|
|
227
|
-
const deviceName = ((
|
|
214
|
+
const deviceName = ((_a = peripheral === null || peripheral === void 0 ? void 0 : peripheral.advertisement) === null || _a === void 0 ? void 0 : _a.localName) || 'Unknown Device';
|
|
228
215
|
const recentWriteTime = recentWriteOperations.get(deviceId);
|
|
229
216
|
const now = Date.now();
|
|
230
217
|
const isPairingRejection = recentWriteTime && now - recentWriteTime < WRITE_DISCONNECT_THRESHOLD;
|
|
@@ -458,14 +445,6 @@ function discoverServicesAndCharacteristics(peripheral) {
|
|
|
458
445
|
}
|
|
459
446
|
function connectDevice(deviceId, webContents) {
|
|
460
447
|
return index.__awaiter(this, void 0, void 0, function* () {
|
|
461
|
-
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Connect device request:', {
|
|
462
|
-
deviceId,
|
|
463
|
-
hasDiscovered: discoveredDevices.has(deviceId),
|
|
464
|
-
hasConnected: connectedDevices.has(deviceId),
|
|
465
|
-
hasCharacteristics: deviceCharacteristics.has(deviceId),
|
|
466
|
-
totalDiscovered: discoveredDevices.size,
|
|
467
|
-
totalConnected: connectedDevices.size,
|
|
468
|
-
});
|
|
469
448
|
let peripheral = discoveredDevices.get(deviceId);
|
|
470
449
|
if (!peripheral) {
|
|
471
450
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Device not discovered, attempting targeted scan for:', deviceId);
|
|
@@ -499,26 +478,18 @@ function connectDevice(deviceId, webContents) {
|
|
|
499
478
|
}
|
|
500
479
|
if (deviceCharacteristics.has(deviceId)) {
|
|
501
480
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Device characteristics already available');
|
|
502
|
-
const
|
|
503
|
-
if (
|
|
504
|
-
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE]
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Found orphaned characteristics without active subscription, cleaning up');
|
|
515
|
-
const existingCharacteristics = deviceCharacteristics.get(deviceId);
|
|
516
|
-
if (existingCharacteristics) {
|
|
517
|
-
existingCharacteristics.notify.removeAllListeners('data');
|
|
481
|
+
const existingCallback = notificationCallbacks.get(deviceId);
|
|
482
|
+
if (existingCallback) {
|
|
483
|
+
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Cleaning up existing notification state');
|
|
484
|
+
const existingCharacteristics = deviceCharacteristics.get(deviceId);
|
|
485
|
+
if (existingCharacteristics) {
|
|
486
|
+
existingCharacteristics.notify.removeAllListeners('data');
|
|
487
|
+
}
|
|
488
|
+
notificationCallbacks.delete(deviceId);
|
|
489
|
+
devicePacketStates.delete(deviceId);
|
|
490
|
+
subscribedDevices.delete(deviceId);
|
|
518
491
|
}
|
|
519
|
-
|
|
520
|
-
devicePacketStates.delete(deviceId);
|
|
521
|
-
subscribedDevices.delete(deviceId);
|
|
492
|
+
return;
|
|
522
493
|
}
|
|
523
494
|
try {
|
|
524
495
|
const characteristics = yield discoverServicesAndCharacteristics(peripheral);
|
|
@@ -654,7 +625,6 @@ function subscribeNotifications(deviceId, callback) {
|
|
|
654
625
|
}
|
|
655
626
|
const { notify: notifyCharacteristic } = characteristics;
|
|
656
627
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Subscribing to notifications for device:', deviceId);
|
|
657
|
-
subscriptionOperations.set(deviceId, 'subscribing');
|
|
658
628
|
if (subscribedDevices.get(deviceId)) {
|
|
659
629
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Device already subscribed to characteristic, updating callback only');
|
|
660
630
|
notificationCallbacks.set(deviceId, callback);
|
|
@@ -664,7 +634,6 @@ function subscribeNotifications(deviceId, callback) {
|
|
|
664
634
|
packetCount: 0,
|
|
665
635
|
messageId: undefined,
|
|
666
636
|
});
|
|
667
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
668
637
|
return Promise.resolve();
|
|
669
638
|
}
|
|
670
639
|
if (notificationCallbacks.has(deviceId)) {
|
|
@@ -679,17 +648,14 @@ function subscribeNotifications(deviceId, callback) {
|
|
|
679
648
|
messageId: undefined,
|
|
680
649
|
});
|
|
681
650
|
return new Promise((resolve, reject) => {
|
|
682
|
-
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] 🔄 Starting subscription process...', { deviceId });
|
|
683
651
|
notifyCharacteristic.subscribe((error) => {
|
|
684
652
|
if (error) {
|
|
685
|
-
logger === null || logger === void 0 ? void 0 : logger.error('[NobleBLE]
|
|
686
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
653
|
+
logger === null || logger === void 0 ? void 0 : logger.error('[NobleBLE] Notification subscription failed:', error);
|
|
687
654
|
reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyError, error));
|
|
688
655
|
return;
|
|
689
656
|
}
|
|
690
|
-
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE]
|
|
657
|
+
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Notification subscription successful');
|
|
691
658
|
subscribedDevices.set(deviceId, true);
|
|
692
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
693
659
|
notifyCharacteristic.on('data', (data) => {
|
|
694
660
|
const result = processNotificationData(deviceId, data);
|
|
695
661
|
if (result.error) {
|
|
@@ -718,7 +684,6 @@ function unsubscribeNotifications(deviceId) {
|
|
|
718
684
|
}
|
|
719
685
|
const { notify: notifyCharacteristic } = characteristics;
|
|
720
686
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Unsubscribing from notifications for device:', deviceId);
|
|
721
|
-
subscriptionOperations.set(deviceId, 'unsubscribing');
|
|
722
687
|
return new Promise(resolve => {
|
|
723
688
|
notifyCharacteristic.unsubscribe((error) => {
|
|
724
689
|
if (error) {
|
|
@@ -731,7 +696,6 @@ function unsubscribeNotifications(deviceId) {
|
|
|
731
696
|
notificationCallbacks.delete(deviceId);
|
|
732
697
|
devicePacketStates.delete(deviceId);
|
|
733
698
|
subscribedDevices.delete(deviceId);
|
|
734
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
735
699
|
resolve();
|
|
736
700
|
});
|
|
737
701
|
});
|
|
@@ -758,12 +722,6 @@ function setupNobleBleHandlers(webContents) {
|
|
|
758
722
|
}));
|
|
759
723
|
ipcMain.handle(hdShared.EOneKeyBleMessageKeys.NOBLE_BLE_GET_DEVICE, (_event, deviceId) => getDevice(deviceId));
|
|
760
724
|
ipcMain.handle(hdShared.EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT, (_event, deviceId) => index.__awaiter(this, void 0, void 0, function* () {
|
|
761
|
-
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] IPC CONNECT request received:', {
|
|
762
|
-
deviceId,
|
|
763
|
-
hasPeripheral: connectedDevices.has(deviceId),
|
|
764
|
-
hasCharacteristics: deviceCharacteristics.has(deviceId),
|
|
765
|
-
totalConnectedDevices: connectedDevices.size,
|
|
766
|
-
});
|
|
767
725
|
yield connectDevice(deviceId, webContents);
|
|
768
726
|
}));
|
|
769
727
|
ipcMain.handle(hdShared.EOneKeyBleMessageKeys.NOBLE_BLE_DISCONNECT, (_event, deviceId) => index.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -812,7 +770,6 @@ function setupNobleBleHandlers(webContents) {
|
|
|
812
770
|
notificationCallbacks.clear();
|
|
813
771
|
devicePacketStates.clear();
|
|
814
772
|
subscribedDevices.clear();
|
|
815
|
-
subscriptionOperations.clear();
|
|
816
773
|
});
|
|
817
774
|
safeLog(logger, 'info', 'Noble BLE IPC handlers setup completed');
|
|
818
775
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,WAAW,EAAsB,MAAM,UAAU,CAAC;AA88BhE,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CA8HpE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-electron",
|
|
3
|
-
"version": "1.1.2-alpha.
|
|
3
|
+
"version": "1.1.2-alpha.2",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"electron-log": ">=4.0.0"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@onekeyfe/hd-shared": "^1.1.2-alpha.
|
|
29
|
+
"@onekeyfe/hd-shared": "^1.1.2-alpha.2"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/web-bluetooth": "^0.0.17",
|
|
33
33
|
"electron": "^25.0.0",
|
|
34
34
|
"typescript": "^5.3.3"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "3d9a4eec5eddf20b1a01c41f5c80793446924c6e"
|
|
37
37
|
}
|
package/src/noble-ble-handler.ts
CHANGED
|
@@ -14,7 +14,6 @@ import {
|
|
|
14
14
|
isHeaderChunk,
|
|
15
15
|
ERRORS,
|
|
16
16
|
HardwareErrorCode,
|
|
17
|
-
wait,
|
|
18
17
|
} from '@onekeyfe/hd-shared';
|
|
19
18
|
import { COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport';
|
|
20
19
|
import type { WebContents, IpcMainInvokeEvent } from 'electron';
|
|
@@ -47,9 +46,6 @@ const deviceCharacteristics = new Map<string, CharacteristicPair>();
|
|
|
47
46
|
const notificationCallbacks = new Map<string, (data: string) => void>();
|
|
48
47
|
const subscribedDevices = new Map<string, boolean>(); // Track subscription status
|
|
49
48
|
|
|
50
|
-
// 🔒 Add subscription operation state tracking to prevent race conditions
|
|
51
|
-
const subscriptionOperations = new Map<string, 'subscribing' | 'unsubscribing' | 'idle'>();
|
|
52
|
-
|
|
53
49
|
// Packet reassembly state for each device
|
|
54
50
|
interface PacketAssemblyState {
|
|
55
51
|
bufferLength: number;
|
|
@@ -116,14 +112,8 @@ function processNotificationData(deviceId: string, data: Buffer): PacketProcessR
|
|
|
116
112
|
packetState.packetCount = 1;
|
|
117
113
|
packetState.messageId = messageId;
|
|
118
114
|
|
|
119
|
-
//
|
|
120
|
-
if (packetState.bufferLength
|
|
121
|
-
logger?.error('[NobleBLE] Invalid negative packet length detected:', {
|
|
122
|
-
length: packetState.bufferLength,
|
|
123
|
-
dataLength: data.length,
|
|
124
|
-
rawHeader: data.subarray(0, Math.min(16, data.length)).toString('hex'),
|
|
125
|
-
lengthBytes: data.subarray(5, 9).toString('hex'),
|
|
126
|
-
});
|
|
115
|
+
// Validate expected length is reasonable
|
|
116
|
+
if (packetState.bufferLength <= 0) {
|
|
127
117
|
resetPacketState(packetState);
|
|
128
118
|
return { isComplete: false, error: 'Invalid packet length in header' };
|
|
129
119
|
}
|
|
@@ -309,19 +299,12 @@ function cleanupDeviceState(deviceId: string): void {
|
|
|
309
299
|
devicePacketStates.delete(deviceId);
|
|
310
300
|
subscribedDevices.delete(deviceId);
|
|
311
301
|
recentWriteOperations.delete(deviceId);
|
|
312
|
-
// 🔒 Clear operation state
|
|
313
|
-
subscriptionOperations.delete(deviceId);
|
|
314
302
|
logger?.info('[NobleBLE] Device state cleaned up:', deviceId);
|
|
315
303
|
}
|
|
316
304
|
|
|
317
305
|
// Handle device disconnection - unified handler for all disconnect scenarios
|
|
318
306
|
function handleDeviceDisconnect(deviceId: string, webContents: WebContents): void {
|
|
319
|
-
logger?.
|
|
320
|
-
deviceId,
|
|
321
|
-
hasPeripheral: connectedDevices.has(deviceId),
|
|
322
|
-
hasCharacteristics: deviceCharacteristics.has(deviceId),
|
|
323
|
-
stackTrace: new Error().stack?.split('\n').slice(1, 5),
|
|
324
|
-
});
|
|
307
|
+
logger?.info('[NobleBLE] Device disconnected:', deviceId);
|
|
325
308
|
|
|
326
309
|
// Get device info before cleanup
|
|
327
310
|
const peripheral = connectedDevices.get(deviceId);
|
|
@@ -653,15 +636,6 @@ async function discoverServicesAndCharacteristics(
|
|
|
653
636
|
|
|
654
637
|
// Connect to device - supports both discovered and direct connection modes
|
|
655
638
|
async function connectDevice(deviceId: string, webContents: WebContents): Promise<void> {
|
|
656
|
-
logger?.info('[NobleBLE] Connect device request:', {
|
|
657
|
-
deviceId,
|
|
658
|
-
hasDiscovered: discoveredDevices.has(deviceId),
|
|
659
|
-
hasConnected: connectedDevices.has(deviceId),
|
|
660
|
-
hasCharacteristics: deviceCharacteristics.has(deviceId),
|
|
661
|
-
totalDiscovered: discoveredDevices.size,
|
|
662
|
-
totalConnected: connectedDevices.size,
|
|
663
|
-
});
|
|
664
|
-
|
|
665
639
|
let peripheral = discoveredDevices.get(deviceId);
|
|
666
640
|
|
|
667
641
|
// If device not discovered, try a targeted scan for this specific device
|
|
@@ -714,45 +688,19 @@ async function connectDevice(deviceId: string, webContents: WebContents): Promis
|
|
|
714
688
|
// Check if we already have characteristics for this device
|
|
715
689
|
if (deviceCharacteristics.has(deviceId)) {
|
|
716
690
|
logger?.info('[NobleBLE] Device characteristics already available');
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
);
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
// Retry connection after waiting
|
|
729
|
-
return connectDevice(deviceId, webContents);
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
// Don't clean up notification state if device is already properly connected
|
|
733
|
-
// The existing notification subscription is still valid and working
|
|
734
|
-
const hasActiveSubscription = subscribedDevices.has(deviceId);
|
|
735
|
-
const hasCallback = notificationCallbacks.has(deviceId);
|
|
736
|
-
|
|
737
|
-
if (hasActiveSubscription && hasCallback) {
|
|
738
|
-
logger?.info(
|
|
739
|
-
'[NobleBLE] Device already has active notification subscription, reusing connection'
|
|
740
|
-
);
|
|
741
|
-
return;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
// Only clean up if subscription is broken
|
|
745
|
-
logger?.info(
|
|
746
|
-
'[NobleBLE] Found orphaned characteristics without active subscription, cleaning up'
|
|
747
|
-
);
|
|
748
|
-
const existingCharacteristics = deviceCharacteristics.get(deviceId);
|
|
749
|
-
if (existingCharacteristics) {
|
|
750
|
-
existingCharacteristics.notify.removeAllListeners('data');
|
|
691
|
+
// Clean up existing notification state to avoid conflicts
|
|
692
|
+
const existingCallback = notificationCallbacks.get(deviceId);
|
|
693
|
+
if (existingCallback) {
|
|
694
|
+
logger?.info('[NobleBLE] Cleaning up existing notification state');
|
|
695
|
+
const existingCharacteristics = deviceCharacteristics.get(deviceId);
|
|
696
|
+
if (existingCharacteristics) {
|
|
697
|
+
existingCharacteristics.notify.removeAllListeners('data');
|
|
698
|
+
}
|
|
699
|
+
notificationCallbacks.delete(deviceId);
|
|
700
|
+
devicePacketStates.delete(deviceId);
|
|
701
|
+
subscribedDevices.delete(deviceId);
|
|
751
702
|
}
|
|
752
|
-
|
|
753
|
-
devicePacketStates.delete(deviceId);
|
|
754
|
-
subscribedDevices.delete(deviceId);
|
|
755
|
-
// Continue to re-setup the connection properly
|
|
703
|
+
return;
|
|
756
704
|
}
|
|
757
705
|
|
|
758
706
|
// Discover services and characteristics
|
|
@@ -940,9 +888,6 @@ async function subscribeNotifications(
|
|
|
940
888
|
|
|
941
889
|
logger?.info('[NobleBLE] Subscribing to notifications for device:', deviceId);
|
|
942
890
|
|
|
943
|
-
// 🔒 Set operation state to prevent race conditions
|
|
944
|
-
subscriptionOperations.set(deviceId, 'subscribing');
|
|
945
|
-
|
|
946
891
|
// Check if already subscribed at the characteristic level
|
|
947
892
|
if (subscribedDevices.get(deviceId)) {
|
|
948
893
|
logger?.info('[NobleBLE] Device already subscribed to characteristic, updating callback only');
|
|
@@ -958,8 +903,6 @@ async function subscribeNotifications(
|
|
|
958
903
|
messageId: undefined,
|
|
959
904
|
});
|
|
960
905
|
|
|
961
|
-
// 🔒 Clear operation state
|
|
962
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
963
906
|
return Promise.resolve();
|
|
964
907
|
}
|
|
965
908
|
|
|
@@ -982,23 +925,16 @@ async function subscribeNotifications(
|
|
|
982
925
|
|
|
983
926
|
return new Promise((resolve, reject) => {
|
|
984
927
|
// Subscribe to notifications only if not already subscribed
|
|
985
|
-
logger?.info('[NobleBLE] 🔄 Starting subscription process...', { deviceId });
|
|
986
|
-
|
|
987
928
|
notifyCharacteristic.subscribe((error: string) => {
|
|
988
929
|
if (error) {
|
|
989
|
-
logger?.error('[NobleBLE]
|
|
990
|
-
// 🔒 Clear operation state on error
|
|
991
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
930
|
+
logger?.error('[NobleBLE] Notification subscription failed:', error);
|
|
992
931
|
reject(ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotifyError, error));
|
|
993
932
|
return;
|
|
994
933
|
}
|
|
995
934
|
|
|
996
|
-
logger?.info('[NobleBLE]
|
|
935
|
+
logger?.info('[NobleBLE] Notification subscription successful');
|
|
997
936
|
subscribedDevices.set(deviceId, true);
|
|
998
937
|
|
|
999
|
-
// 🔒 Clear operation state on success
|
|
1000
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
1001
|
-
|
|
1002
938
|
// Set up data handler with robust packet reassembly
|
|
1003
939
|
notifyCharacteristic.on('data', (data: Buffer) => {
|
|
1004
940
|
const result = processNotificationData(deviceId, data);
|
|
@@ -1035,9 +971,6 @@ async function unsubscribeNotifications(deviceId: string): Promise<void> {
|
|
|
1035
971
|
|
|
1036
972
|
logger?.info('[NobleBLE] Unsubscribing from notifications for device:', deviceId);
|
|
1037
973
|
|
|
1038
|
-
// 🔒 Set operation state to prevent race conditions
|
|
1039
|
-
subscriptionOperations.set(deviceId, 'unsubscribing');
|
|
1040
|
-
|
|
1041
974
|
return new Promise<void>(resolve => {
|
|
1042
975
|
notifyCharacteristic.unsubscribe((error: string) => {
|
|
1043
976
|
if (error) {
|
|
@@ -1051,9 +984,6 @@ async function unsubscribeNotifications(deviceId: string): Promise<void> {
|
|
|
1051
984
|
notificationCallbacks.delete(deviceId);
|
|
1052
985
|
devicePacketStates.delete(deviceId);
|
|
1053
986
|
subscribedDevices.delete(deviceId);
|
|
1054
|
-
|
|
1055
|
-
// 🔒 Clear operation state
|
|
1056
|
-
subscriptionOperations.set(deviceId, 'idle');
|
|
1057
987
|
resolve();
|
|
1058
988
|
});
|
|
1059
989
|
});
|
|
@@ -1098,12 +1028,6 @@ export function setupNobleBleHandlers(webContents: WebContents): void {
|
|
|
1098
1028
|
ipcMain.handle(
|
|
1099
1029
|
EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT,
|
|
1100
1030
|
async (_event: IpcMainInvokeEvent, deviceId: string) => {
|
|
1101
|
-
logger?.info('[NobleBLE] IPC CONNECT request received:', {
|
|
1102
|
-
deviceId,
|
|
1103
|
-
hasPeripheral: connectedDevices.has(deviceId),
|
|
1104
|
-
hasCharacteristics: deviceCharacteristics.has(deviceId),
|
|
1105
|
-
totalConnectedDevices: connectedDevices.size,
|
|
1106
|
-
});
|
|
1107
1031
|
await connectDevice(deviceId, webContents);
|
|
1108
1032
|
}
|
|
1109
1033
|
);
|
|
@@ -1185,9 +1109,6 @@ export function setupNobleBleHandlers(webContents: WebContents): void {
|
|
|
1185
1109
|
notificationCallbacks.clear();
|
|
1186
1110
|
devicePacketStates.clear();
|
|
1187
1111
|
subscribedDevices.clear();
|
|
1188
|
-
|
|
1189
|
-
// Clear operation states
|
|
1190
|
-
subscriptionOperations.clear();
|
|
1191
1112
|
});
|
|
1192
1113
|
|
|
1193
1114
|
safeLog(logger, 'info', 'Noble BLE IPC handlers setup completed');
|