@onekeyfe/hd-transport-electron 1.2.8-alpha.107 → 1.2.8-alpha.108
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/dist/{index-21e34fdb.js → index-7ebbf8b8.js} +2 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/{noble-ble-handler-c4530a1b.js → noble-ble-handler-e0817284.js} +22 -3
- package/dist/noble-ble-handler.d.ts.map +1 -1
- package/dist/types/desktop-api.d.ts +1 -0
- package/dist/types/desktop-api.d.ts.map +1 -1
- package/dist/types/noble-extended.d.ts +2 -2
- package/dist/types/noble-extended.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/noble-ble-handler.test.ts +161 -0
- package/src/noble-ble-handler.ts +28 -1
- package/src/types/desktop-api.ts +1 -1
- package/src/types/noble-extended.ts +4 -2
|
@@ -55,13 +55,13 @@ function invokeNobleBleIpc(request) {
|
|
|
55
55
|
|
|
56
56
|
function initNobleBleSupport(webContents) {
|
|
57
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-
|
|
58
|
+
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-e0817284.js'); });
|
|
59
59
|
setupNobleBleHandlers(webContents);
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
62
|
function disposeNobleBleSupport(releaseNoble) {
|
|
63
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
const handler = yield Promise.resolve().then(function () { return require('./noble-ble-handler-
|
|
64
|
+
const handler = yield Promise.resolve().then(function () { return require('./noble-ble-handler-e0817284.js'); });
|
|
65
65
|
yield handler.disposeNobleBleSupport(releaseNoble);
|
|
66
66
|
});
|
|
67
67
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { WebContents } from 'electron';
|
|
2
2
|
import { Characteristic, Peripheral } from '@stoprocent/noble';
|
|
3
|
-
import { OneKeyDeviceInfoBase } from '@onekeyfe/hd-transport';
|
|
3
|
+
import { OneKeyDeviceInfoBase, OneKeyMobileDeviceInfo } from '@onekeyfe/hd-transport';
|
|
4
4
|
import { EBleDisconnectReason } from '@onekeyfe/hd-shared';
|
|
5
5
|
|
|
6
|
-
interface DeviceInfo extends OneKeyDeviceInfoBase {
|
|
6
|
+
interface DeviceInfo extends OneKeyDeviceInfoBase, Pick<OneKeyMobileDeviceInfo, 'bleColorCode'> {
|
|
7
7
|
id: string;
|
|
8
8
|
name: string;
|
|
9
9
|
state: string;
|
|
@@ -60,6 +60,7 @@ interface NobleBleAPI {
|
|
|
60
60
|
enumerate: () => Promise<{
|
|
61
61
|
id: string;
|
|
62
62
|
name: string;
|
|
63
|
+
bleColorCode?: string;
|
|
63
64
|
}[]>;
|
|
64
65
|
getDevice: (uuid: string) => Promise<{
|
|
65
66
|
id: string;
|
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-7ebbf8b8.js');
|
|
4
4
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
5
5
|
var pRetry = require('p-retry');
|
|
6
6
|
|
|
@@ -771,6 +771,18 @@ function rememberBleName(deviceId, name) {
|
|
|
771
771
|
bleNamesById.set(deviceId, trimmed);
|
|
772
772
|
}
|
|
773
773
|
}
|
|
774
|
+
const bleColorCodesById = new Map();
|
|
775
|
+
function rememberBleColorCode(peripheral) {
|
|
776
|
+
var _a;
|
|
777
|
+
const colorCode = hdShared.parseOnekeyBleColorCode((_a = peripheral.advertisement) === null || _a === void 0 ? void 0 : _a.manufacturerData);
|
|
778
|
+
if (colorCode && bleColorCodesById.get(peripheral.id) !== colorCode) {
|
|
779
|
+
bleColorCodesById.set(peripheral.id, colorCode);
|
|
780
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('[NobleBLE] BLE color code', {
|
|
781
|
+
deviceId: peripheral.id,
|
|
782
|
+
bleColorCode: colorCode,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
}
|
|
774
786
|
function resolveBleName(deviceId) {
|
|
775
787
|
var _a, _b, _c, _d, _e, _f;
|
|
776
788
|
const live = ((_c = (_b = (_a = connectedDevices.get(deviceId)) === null || _a === void 0 ? void 0 : _a.advertisement) === null || _b === void 0 ? void 0 : _b.localName) === null || _c === void 0 ? void 0 : _c.trim()) ||
|
|
@@ -781,18 +793,21 @@ function shouldConnectByIdFirst(deviceId) {
|
|
|
781
793
|
return hdShared.isPro2FamilyBleName(resolveBleName(deviceId));
|
|
782
794
|
}
|
|
783
795
|
function handleDeviceDiscovered(peripheral) {
|
|
784
|
-
var _a, _b, _c;
|
|
796
|
+
var _a, _b, _c, _d, _e, _f;
|
|
785
797
|
if (!isOneKeyPeripheral(peripheral)) {
|
|
786
798
|
return;
|
|
787
799
|
}
|
|
788
800
|
const isNewDevice = !discoveredDevices.has(peripheral.id);
|
|
789
801
|
discoveredDevices.set(peripheral.id, peripheral);
|
|
790
802
|
rememberBleName(peripheral.id, (_a = peripheral.advertisement) === null || _a === void 0 ? void 0 : _a.localName);
|
|
803
|
+
rememberBleColorCode(peripheral);
|
|
791
804
|
if (isNewDevice) {
|
|
792
805
|
logger === null || logger === void 0 ? void 0 : logger.debug('[NobleBLE] OneKey BLE device discovered', {
|
|
793
806
|
deviceId: peripheral.id,
|
|
794
807
|
name: ((_b = peripheral.advertisement) === null || _b === void 0 ? void 0 : _b.localName) || 'Unknown Device',
|
|
795
808
|
serviceUUIDs: ((_c = peripheral.advertisement) === null || _c === void 0 ? void 0 : _c.serviceUuids) || [],
|
|
809
|
+
manufacturerDataLength: (_f = (_e = (_d = peripheral.advertisement) === null || _d === void 0 ? void 0 : _d.manufacturerData) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0,
|
|
810
|
+
bleColorCode: bleColorCodesById.get(peripheral.id),
|
|
796
811
|
});
|
|
797
812
|
}
|
|
798
813
|
}
|
|
@@ -918,6 +933,10 @@ function enumerateDevices(isWindowDestroyed) {
|
|
|
918
933
|
state: peripheral.state || 'disconnected',
|
|
919
934
|
});
|
|
920
935
|
};
|
|
936
|
+
const withBleColorCode = (device) => {
|
|
937
|
+
const bleColorCode = bleColorCodesById.get(device.id);
|
|
938
|
+
return bleColorCode ? Object.assign(Object.assign({}, device), { bleColorCode }) : device;
|
|
939
|
+
};
|
|
921
940
|
const checkDevices = () => {
|
|
922
941
|
discoveredDevices.forEach(pushDevice);
|
|
923
942
|
connectedDevices.forEach(pushDevice);
|
|
@@ -928,7 +947,7 @@ function enumerateDevices(isWindowDestroyed) {
|
|
|
928
947
|
checkDevices();
|
|
929
948
|
yield cleanup();
|
|
930
949
|
logger === null || logger === void 0 ? void 0 : logger.info('[NobleBLE] Scan completed, found devices:', devices.length);
|
|
931
|
-
resolve(devices);
|
|
950
|
+
resolve(devices.map(withBleColorCode));
|
|
932
951
|
}), DEVICE_SCAN_TIMEOUT);
|
|
933
952
|
const cancel = () => cleanup().then(() => resolve([]), reject);
|
|
934
953
|
pendingCancellations.add(cancel);
|
|
@@ -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":"AAkCA,OAAO,KAAK,EAA+B,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAiC1F,KAAK,mBAAmB,GAAG,KAAK,GAAG;IACjC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,wBAAgB,6BAA6B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CAAC;CACxB"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Characteristic, Peripheral } from '@stoprocent/noble';
|
|
2
|
-
import type { OneKeyDeviceInfoBase } from '@onekeyfe/hd-transport';
|
|
3
|
-
export interface DeviceInfo extends OneKeyDeviceInfoBase {
|
|
2
|
+
import type { OneKeyDeviceInfoBase, OneKeyMobileDeviceInfo } from '@onekeyfe/hd-transport';
|
|
3
|
+
export interface DeviceInfo extends OneKeyDeviceInfoBase, Pick<OneKeyMobileDeviceInfo, 'bleColorCode'> {
|
|
4
4
|
id: string;
|
|
5
5
|
name: string;
|
|
6
6
|
state: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noble-extended.d.ts","sourceRoot":"","sources":["../../src/types/noble-extended.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"noble-extended.d.ts","sourceRoot":"","sources":["../../src/types/noble-extended.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAG3F,MAAM,WAAW,UACf,SAAQ,oBAAoB,EAC1B,IAAI,CAAC,sBAAsB,EAAE,cAAc,CAAC;IAC9C,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAGD,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,cAAc,CAAC;IACtB,MAAM,EAAE,cAAc,CAAC;CACxB;AAGD,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,aAAa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AG,OAAO,EACjC,OAAO,EAAE,MAAM,EACf,GAAG,IAAI,EAAE,GAAG,EAAE,GACb,IAAI,CAMN"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-electron",
|
|
3
|
-
"version": "1.2.8-alpha.
|
|
3
|
+
"version": "1.2.8-alpha.108",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"electron-log": ">=4.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@onekeyfe/hd-core": "1.2.8-alpha.
|
|
29
|
-
"@onekeyfe/hd-shared": "1.2.8-alpha.
|
|
30
|
-
"@onekeyfe/hd-transport": "1.2.8-alpha.
|
|
28
|
+
"@onekeyfe/hd-core": "1.2.8-alpha.108",
|
|
29
|
+
"@onekeyfe/hd-shared": "1.2.8-alpha.108",
|
|
30
|
+
"@onekeyfe/hd-transport": "1.2.8-alpha.108",
|
|
31
31
|
"@stoprocent/noble": "2.3.16",
|
|
32
32
|
"p-retry": "^4.6.2"
|
|
33
33
|
},
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"electron": "^25.0.0",
|
|
37
37
|
"typescript": "^5.3.3"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "dee244daa863a0694dfdd0f8dd7463d98573fbba"
|
|
40
40
|
}
|
|
@@ -1211,3 +1211,164 @@ describe('Noble BLE process shutdown', () => {
|
|
|
1211
1211
|
await sdk.disposeNobleBleSupport();
|
|
1212
1212
|
});
|
|
1213
1213
|
});
|
|
1214
|
+
|
|
1215
|
+
describe('Electron Noble BLE advertisement color', () => {
|
|
1216
|
+
afterEach(() => {
|
|
1217
|
+
jest.useRealTimers();
|
|
1218
|
+
jest.resetModules();
|
|
1219
|
+
jest.clearAllMocks();
|
|
1220
|
+
});
|
|
1221
|
+
|
|
1222
|
+
test('applies a color that arrives after the device was listed in the same scan', async () => {
|
|
1223
|
+
jest.useFakeTimers({ doNotFake: ['performance'] });
|
|
1224
|
+
|
|
1225
|
+
const handlers = new Map<string, IpcHandler>();
|
|
1226
|
+
const ipcMain = {
|
|
1227
|
+
handle: jest.fn((channel: string, handler: IpcHandler) => {
|
|
1228
|
+
handlers.set(channel, handler);
|
|
1229
|
+
}),
|
|
1230
|
+
removeHandler: jest.fn((channel: string) => {
|
|
1231
|
+
handlers.delete(channel);
|
|
1232
|
+
}),
|
|
1233
|
+
};
|
|
1234
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
1235
|
+
state: string;
|
|
1236
|
+
startScanning: jest.Mock;
|
|
1237
|
+
stopScanning: jest.Mock;
|
|
1238
|
+
};
|
|
1239
|
+
let resolveScanStarted = () => undefined;
|
|
1240
|
+
const scanStarted = new Promise<void>(resolve => {
|
|
1241
|
+
resolveScanStarted = resolve;
|
|
1242
|
+
});
|
|
1243
|
+
const peripheral = createPeripheral('late-color-device', 'Neo 1A2B');
|
|
1244
|
+
noble.state = 'poweredOn';
|
|
1245
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
1246
|
+
callback?.();
|
|
1247
|
+
Object.assign(peripheral.advertisement, { manufacturerData: Buffer.from([0, 0, 0, 0, 0]) });
|
|
1248
|
+
noble.emit('discover', peripheral);
|
|
1249
|
+
resolveScanStarted();
|
|
1250
|
+
});
|
|
1251
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
1252
|
+
|
|
1253
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
1254
|
+
jest.doMock('electron', () => ({ ipcMain }));
|
|
1255
|
+
jest.doMock('electron-log', () => ({
|
|
1256
|
+
info: jest.fn(),
|
|
1257
|
+
debug: jest.fn(),
|
|
1258
|
+
error: jest.fn(),
|
|
1259
|
+
}));
|
|
1260
|
+
|
|
1261
|
+
const { setupNobleBleHandlers } = await import('../noble-ble-handler');
|
|
1262
|
+
setupNobleBleHandlers({
|
|
1263
|
+
on: jest.fn(),
|
|
1264
|
+
send: jest.fn(),
|
|
1265
|
+
} as unknown as WebContents);
|
|
1266
|
+
|
|
1267
|
+
const enumerate = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE);
|
|
1268
|
+
if (!enumerate) {
|
|
1269
|
+
throw new Error('Electron Noble BLE enumerate handler was not registered');
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
const devicesPromise = Promise.resolve(enumerate());
|
|
1273
|
+
await scanStarted;
|
|
1274
|
+
// The first periodic check lists the device before its color is advertised.
|
|
1275
|
+
jest.advanceTimersByTime(600);
|
|
1276
|
+
Object.assign(peripheral.advertisement, {
|
|
1277
|
+
manufacturerData: Buffer.from([0x45, 0, 0, 0, 0]),
|
|
1278
|
+
});
|
|
1279
|
+
noble.emit('discover', peripheral);
|
|
1280
|
+
jest.advanceTimersByTime(5000);
|
|
1281
|
+
|
|
1282
|
+
await expect(devicesPromise).resolves.toEqual([
|
|
1283
|
+
{
|
|
1284
|
+
commType: 'electron-ble',
|
|
1285
|
+
id: 'late-color-device',
|
|
1286
|
+
name: 'Neo 1A2B',
|
|
1287
|
+
state: 'disconnected',
|
|
1288
|
+
bleColorCode: 'E',
|
|
1289
|
+
},
|
|
1290
|
+
]);
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1293
|
+
test('keeps the advertised color after Noble overwrites the manufacturer data', async () => {
|
|
1294
|
+
jest.useFakeTimers({ doNotFake: ['performance'] });
|
|
1295
|
+
|
|
1296
|
+
const handlers = new Map<string, IpcHandler>();
|
|
1297
|
+
const ipcMain = {
|
|
1298
|
+
handle: jest.fn((channel: string, handler: IpcHandler) => {
|
|
1299
|
+
handlers.set(channel, handler);
|
|
1300
|
+
}),
|
|
1301
|
+
removeHandler: jest.fn((channel: string) => {
|
|
1302
|
+
handlers.delete(channel);
|
|
1303
|
+
}),
|
|
1304
|
+
};
|
|
1305
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
1306
|
+
state: string;
|
|
1307
|
+
startScanning: jest.Mock;
|
|
1308
|
+
stopScanning: jest.Mock;
|
|
1309
|
+
};
|
|
1310
|
+
let resolveScanStarted = () => undefined;
|
|
1311
|
+
const scanStarted = new Promise<void>(resolve => {
|
|
1312
|
+
resolveScanStarted = resolve;
|
|
1313
|
+
});
|
|
1314
|
+
const coloredPeripheral = createPeripheral('colored-device', 'Pro 2 A1B2');
|
|
1315
|
+
const legacyPeripheral = createPeripheral('legacy-device', 'Pro 2 C3D4');
|
|
1316
|
+
noble.state = 'poweredOn';
|
|
1317
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
1318
|
+
callback?.();
|
|
1319
|
+
Object.assign(coloredPeripheral.advertisement, {
|
|
1320
|
+
manufacturerData: Buffer.from([0x44, 0, 0, 0, 0]),
|
|
1321
|
+
});
|
|
1322
|
+
noble.emit('discover', coloredPeripheral);
|
|
1323
|
+
// macOS and Windows replace the stored payload with an empty buffer when a
|
|
1324
|
+
// later event carries no 0xFF section.
|
|
1325
|
+
Object.assign(coloredPeripheral.advertisement, { manufacturerData: Buffer.alloc(0) });
|
|
1326
|
+
noble.emit('discover', coloredPeripheral);
|
|
1327
|
+
Object.assign(legacyPeripheral.advertisement, {
|
|
1328
|
+
manufacturerData: Buffer.from([0, 0, 0, 0]),
|
|
1329
|
+
});
|
|
1330
|
+
noble.emit('discover', legacyPeripheral);
|
|
1331
|
+
resolveScanStarted();
|
|
1332
|
+
});
|
|
1333
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
1334
|
+
|
|
1335
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
1336
|
+
jest.doMock('electron', () => ({ ipcMain }));
|
|
1337
|
+
jest.doMock('electron-log', () => ({
|
|
1338
|
+
info: jest.fn(),
|
|
1339
|
+
debug: jest.fn(),
|
|
1340
|
+
error: jest.fn(),
|
|
1341
|
+
}));
|
|
1342
|
+
|
|
1343
|
+
const { setupNobleBleHandlers } = await import('../noble-ble-handler');
|
|
1344
|
+
setupNobleBleHandlers({
|
|
1345
|
+
on: jest.fn(),
|
|
1346
|
+
send: jest.fn(),
|
|
1347
|
+
} as unknown as WebContents);
|
|
1348
|
+
|
|
1349
|
+
const enumerate = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE);
|
|
1350
|
+
if (!enumerate) {
|
|
1351
|
+
throw new Error('Electron Noble BLE enumerate handler was not registered');
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
const devicesPromise = Promise.resolve(enumerate());
|
|
1355
|
+
await scanStarted;
|
|
1356
|
+
jest.advanceTimersByTime(5000);
|
|
1357
|
+
|
|
1358
|
+
await expect(devicesPromise).resolves.toEqual([
|
|
1359
|
+
{
|
|
1360
|
+
commType: 'electron-ble',
|
|
1361
|
+
id: 'colored-device',
|
|
1362
|
+
name: 'Pro 2 A1B2',
|
|
1363
|
+
state: 'disconnected',
|
|
1364
|
+
bleColorCode: 'D',
|
|
1365
|
+
},
|
|
1366
|
+
{
|
|
1367
|
+
commType: 'electron-ble',
|
|
1368
|
+
id: 'legacy-device',
|
|
1369
|
+
name: 'Pro 2 C3D4',
|
|
1370
|
+
state: 'disconnected',
|
|
1371
|
+
},
|
|
1372
|
+
]);
|
|
1373
|
+
});
|
|
1374
|
+
});
|
package/src/noble-ble-handler.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
isOnekeyBluetoothDevice,
|
|
20
20
|
isPro2FamilyBleName,
|
|
21
21
|
matchesKnownBleUuid,
|
|
22
|
+
parseOnekeyBleColorCode,
|
|
22
23
|
wait,
|
|
23
24
|
} from '@onekeyfe/hd-shared';
|
|
24
25
|
import pRetry from 'p-retry';
|
|
@@ -1012,6 +1013,22 @@ function rememberBleName(deviceId: string, name?: string | null): void {
|
|
|
1012
1013
|
}
|
|
1013
1014
|
}
|
|
1014
1015
|
|
|
1016
|
+
// Noble reuses one Peripheral per id, and on macOS/Windows an event without 0xFF data
|
|
1017
|
+
// replaces manufacturerData with an empty buffer. Snapshot the color when the OneKey
|
|
1018
|
+
// advertisement is seen; it is kept like the name because a linked device stops advertising.
|
|
1019
|
+
const bleColorCodesById = new Map<string, string>();
|
|
1020
|
+
|
|
1021
|
+
function rememberBleColorCode(peripheral: Peripheral): void {
|
|
1022
|
+
const colorCode = parseOnekeyBleColorCode(peripheral.advertisement?.manufacturerData);
|
|
1023
|
+
if (colorCode && bleColorCodesById.get(peripheral.id) !== colorCode) {
|
|
1024
|
+
bleColorCodesById.set(peripheral.id, colorCode);
|
|
1025
|
+
logger?.debug('[NobleBLE] BLE color code', {
|
|
1026
|
+
deviceId: peripheral.id,
|
|
1027
|
+
bleColorCode: colorCode,
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1015
1032
|
function resolveBleName(deviceId: string): string | undefined {
|
|
1016
1033
|
const live =
|
|
1017
1034
|
connectedDevices.get(deviceId)?.advertisement?.localName?.trim() ||
|
|
@@ -1040,11 +1057,15 @@ function handleDeviceDiscovered(peripheral: Peripheral): void {
|
|
|
1040
1057
|
const isNewDevice = !discoveredDevices.has(peripheral.id);
|
|
1041
1058
|
discoveredDevices.set(peripheral.id, peripheral);
|
|
1042
1059
|
rememberBleName(peripheral.id, peripheral.advertisement?.localName);
|
|
1060
|
+
rememberBleColorCode(peripheral);
|
|
1043
1061
|
if (isNewDevice) {
|
|
1044
1062
|
logger?.debug('[NobleBLE] OneKey BLE device discovered', {
|
|
1045
1063
|
deviceId: peripheral.id,
|
|
1046
1064
|
name: peripheral.advertisement?.localName || 'Unknown Device',
|
|
1047
1065
|
serviceUUIDs: peripheral.advertisement?.serviceUuids || [],
|
|
1066
|
+
// 5 = advertises a color slot; 4 = BLE firmware before 1.0.23.
|
|
1067
|
+
manufacturerDataLength: peripheral.advertisement?.manufacturerData?.length ?? 0,
|
|
1068
|
+
bleColorCode: bleColorCodesById.get(peripheral.id),
|
|
1048
1069
|
});
|
|
1049
1070
|
}
|
|
1050
1071
|
}
|
|
@@ -1200,6 +1221,12 @@ async function enumerateDevices(isWindowDestroyed: () => boolean): Promise<Devic
|
|
|
1200
1221
|
state: peripheral.state || 'disconnected',
|
|
1201
1222
|
});
|
|
1202
1223
|
};
|
|
1224
|
+
// A device kept from an earlier round is pushed before this round's
|
|
1225
|
+
// advertisements arrive, so apply colors when the scan resolves.
|
|
1226
|
+
const withBleColorCode = (device: DeviceInfo): DeviceInfo => {
|
|
1227
|
+
const bleColorCode = bleColorCodesById.get(device.id);
|
|
1228
|
+
return bleColorCode ? { ...device, bleColorCode } : device;
|
|
1229
|
+
};
|
|
1203
1230
|
const checkDevices = () => {
|
|
1204
1231
|
discoveredDevices.forEach(pushDevice);
|
|
1205
1232
|
// A linked device stops advertising, so the scan never rediscovers it.
|
|
@@ -1213,7 +1240,7 @@ async function enumerateDevices(isWindowDestroyed: () => boolean): Promise<Devic
|
|
|
1213
1240
|
checkDevices();
|
|
1214
1241
|
await cleanup();
|
|
1215
1242
|
logger?.info('[NobleBLE] Scan completed, found devices:', devices.length);
|
|
1216
|
-
resolve(devices);
|
|
1243
|
+
resolve(devices.map(withBleColorCode));
|
|
1217
1244
|
}, DEVICE_SCAN_TIMEOUT);
|
|
1218
1245
|
|
|
1219
1246
|
const cancel = () => cleanup().then(() => resolve([]), reject);
|
package/src/types/desktop-api.ts
CHANGED
|
@@ -55,7 +55,7 @@ export async function invokeNobleBleIpc<T>(
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export interface NobleBleAPI {
|
|
58
|
-
enumerate: () => Promise<{ id: string; name: string }[]>;
|
|
58
|
+
enumerate: () => Promise<{ id: string; name: string; bleColorCode?: string }[]>;
|
|
59
59
|
getDevice: (uuid: string) => Promise<{ id: string; name: string; mtu?: number } | null>;
|
|
60
60
|
connect: (uuid: string) => Promise<void>;
|
|
61
61
|
// Logical end-of-operation: link stays up, idle countdown starts. Optional —
|
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { Characteristic, Peripheral } from '@stoprocent/noble';
|
|
7
|
-
import type { OneKeyDeviceInfoBase } from '@onekeyfe/hd-transport';
|
|
7
|
+
import type { OneKeyDeviceInfoBase, OneKeyMobileDeviceInfo } from '@onekeyfe/hd-transport';
|
|
8
8
|
|
|
9
9
|
// Device info interface for our API
|
|
10
|
-
export interface DeviceInfo
|
|
10
|
+
export interface DeviceInfo
|
|
11
|
+
extends OneKeyDeviceInfoBase,
|
|
12
|
+
Pick<OneKeyMobileDeviceInfo, 'bleColorCode'> {
|
|
11
13
|
id: string;
|
|
12
14
|
name: string;
|
|
13
15
|
state: string;
|