@onekeyfe/hd-transport-react-native 1.2.8-alpha.117 → 1.2.8-alpha.119
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.d.ts +2 -33
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +35 -320
- package/package.json +5 -5
- package/src/__tests__/enumerate.test.ts +139 -0
- package/src/__tests__/protocolV2Link.test.ts +0 -103
- package/src/index.ts +54 -458
- package/src/__tests__/nativeFrameWrite.test.ts +0 -917
|
@@ -214,3 +214,142 @@ describe('ReactNativeBleTransport iOS discovery', () => {
|
|
|
214
214
|
expect(devices.map(device => device.id)).toEqual(['wallet-peripheral']);
|
|
215
215
|
});
|
|
216
216
|
});
|
|
217
|
+
|
|
218
|
+
describe('ReactNativeBleTransport advertisement color', () => {
|
|
219
|
+
const PRO2_SERVICE_UUIDS = [
|
|
220
|
+
'0000180a-0000-1000-8000-00805f9b34fb',
|
|
221
|
+
'0000180f-0000-1000-8000-00805f9b34fb',
|
|
222
|
+
'0000fffd-0000-1000-8000-00805f9b34fb',
|
|
223
|
+
ONEKEY_SERVICE_UUID,
|
|
224
|
+
];
|
|
225
|
+
const manufacturerData = (bytes: number[]) => Buffer.from(bytes).toString('base64');
|
|
226
|
+
|
|
227
|
+
const scanOnce = async (advertisements: Record<string, unknown>[]) => {
|
|
228
|
+
const blePlxManager = {
|
|
229
|
+
startDeviceScan: jest.fn((_serviceUUIDs, _options, listener) => {
|
|
230
|
+
queueMicrotask(() => {
|
|
231
|
+
advertisements.forEach(advertisement => listener(null, advertisement));
|
|
232
|
+
});
|
|
233
|
+
}),
|
|
234
|
+
stopDeviceScan: jest.fn(),
|
|
235
|
+
};
|
|
236
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
|
|
237
|
+
transport.blePlxManager = blePlxManager as never;
|
|
238
|
+
transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
|
|
239
|
+
return transport.enumerate();
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
beforeEach(() => {
|
|
243
|
+
jest.clearAllMocks();
|
|
244
|
+
jest.mocked(getConnectedDeviceIds).mockResolvedValue([]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
afterEach(() => {
|
|
248
|
+
Object.assign(Platform, { OS: 'ios', Version: undefined });
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test('keeps the color from an unnamed iOS advertisement for the named device', async () => {
|
|
252
|
+
const devices = await scanOnce([
|
|
253
|
+
{
|
|
254
|
+
id: 'COLOR-UNNAMED',
|
|
255
|
+
name: null,
|
|
256
|
+
localName: null,
|
|
257
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
258
|
+
manufacturerData: manufacturerData([0x42, 0, 0, 0, 0]),
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
id: 'COLOR-UNNAMED',
|
|
262
|
+
name: 'Pro 2 769D',
|
|
263
|
+
localName: 'Pro 2 769D',
|
|
264
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
265
|
+
manufacturerData: null,
|
|
266
|
+
},
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
expect(devices).toHaveLength(1);
|
|
270
|
+
expect(devices[0]).toMatchObject({ id: 'COLOR-UNNAMED', bleColorCode: 'B' });
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('fills a later color without letting a legacy payload clear it', async () => {
|
|
274
|
+
Object.assign(Platform, { OS: 'android', Version: 30 });
|
|
275
|
+
|
|
276
|
+
const devices = await scanOnce([
|
|
277
|
+
{
|
|
278
|
+
id: 'AA:BB:CC:00:00:01',
|
|
279
|
+
name: 'Neo 1A2B',
|
|
280
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
281
|
+
manufacturerData: null,
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
id: 'AA:BB:CC:00:00:01',
|
|
285
|
+
name: 'Neo 1A2B',
|
|
286
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
287
|
+
manufacturerData: manufacturerData([0x45, 0, 0, 0, 0]),
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
id: 'AA:BB:CC:00:00:01',
|
|
291
|
+
name: 'Neo 1A2B',
|
|
292
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
293
|
+
manufacturerData: manufacturerData([0, 0, 0, 0]),
|
|
294
|
+
},
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
expect(devices).toHaveLength(1);
|
|
298
|
+
expect(devices[0]).toMatchObject({ id: 'AA:BB:CC:00:00:01', bleColorCode: 'E' });
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('reports no color for legacy, unset or undecodable payloads', async () => {
|
|
302
|
+
const devices = await scanOnce([
|
|
303
|
+
{
|
|
304
|
+
id: 'LEGACY-NONCE',
|
|
305
|
+
name: 'Pro 2 0001',
|
|
306
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
307
|
+
manufacturerData: manufacturerData([0, 0, 0, 0]),
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
id: 'SN-NOT-SYNCED',
|
|
311
|
+
name: 'Pro 2 0002',
|
|
312
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
313
|
+
manufacturerData: manufacturerData([0, 0, 0, 0, 0]),
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
id: 'INVALID-BASE64',
|
|
317
|
+
name: 'Pro 2 0003',
|
|
318
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
319
|
+
manufacturerData: '%%%',
|
|
320
|
+
},
|
|
321
|
+
]);
|
|
322
|
+
|
|
323
|
+
expect(devices.map(device => device.id)).toEqual([
|
|
324
|
+
'LEGACY-NONCE',
|
|
325
|
+
'SN-NOT-SYNCED',
|
|
326
|
+
'INVALID-BASE64',
|
|
327
|
+
]);
|
|
328
|
+
devices.forEach(device => expect(device).not.toHaveProperty('bleColorCode'));
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('reuses an earlier color for a connected peripheral that no longer advertises', async () => {
|
|
332
|
+
await scanOnce([
|
|
333
|
+
{
|
|
334
|
+
id: 'CONNECTED-NEO',
|
|
335
|
+
name: 'Neo 3C4D',
|
|
336
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
337
|
+
manufacturerData: manufacturerData([0x46, 0, 0, 0, 0]),
|
|
338
|
+
},
|
|
339
|
+
]);
|
|
340
|
+
// iOS reports already-connected peripherals with lowercased ids.
|
|
341
|
+
jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([
|
|
342
|
+
{
|
|
343
|
+
id: 'connected-neo',
|
|
344
|
+
name: 'Neo 3C4D',
|
|
345
|
+
localName: null,
|
|
346
|
+
serviceUUIDs: [ONEKEY_SERVICE_UUID],
|
|
347
|
+
},
|
|
348
|
+
] as never);
|
|
349
|
+
|
|
350
|
+
const devices = await scanOnce([]);
|
|
351
|
+
|
|
352
|
+
expect(devices).toHaveLength(1);
|
|
353
|
+
expect(devices[0]).toMatchObject({ id: 'connected-neo', bleColorCode: 'F' });
|
|
354
|
+
});
|
|
355
|
+
});
|
|
@@ -12,7 +12,6 @@ import ReactNativeBleTransport, {
|
|
|
12
12
|
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
13
13
|
configureProtocolV2BleTuning,
|
|
14
14
|
getFirmwareUploadWriteRetryType,
|
|
15
|
-
getProtocolV2BleTuning,
|
|
16
15
|
resetProtocolV2BleTuning,
|
|
17
16
|
} from '../index';
|
|
18
17
|
|
|
@@ -2091,108 +2090,6 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
2091
2090
|
expect(device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
|
|
2092
2091
|
});
|
|
2093
2092
|
|
|
2094
|
-
describe('Android connection priority by API level', () => {
|
|
2095
|
-
const setPlatformVersion = (version: number | undefined) => {
|
|
2096
|
-
const reactNative: { Platform: { Version?: number } } = jest.requireMock('react-native');
|
|
2097
|
-
reactNative.Platform.Version = version;
|
|
2098
|
-
};
|
|
2099
|
-
|
|
2100
|
-
afterEach(() => {
|
|
2101
|
-
setPlatformVersion(undefined);
|
|
2102
|
-
});
|
|
2103
|
-
|
|
2104
|
-
const runHighVolumeCalls = async (harness: ReturnType<typeof createHarness>) => {
|
|
2105
|
-
await harness.transport.acquire({ uuid: harness.uuid, expectedProtocol: 'V2' });
|
|
2106
|
-
await harness.transport.call(harness.uuid, 'FileWrite', {});
|
|
2107
|
-
await harness.transport.call(harness.uuid, 'FileWrite', {});
|
|
2108
|
-
};
|
|
2109
|
-
|
|
2110
|
-
test('leaves the priority to the peripheral on Android 17 by default', async () => {
|
|
2111
|
-
setPlatformOS('android');
|
|
2112
|
-
setPlatformVersion(37);
|
|
2113
|
-
const harness = createHarness();
|
|
2114
|
-
harness.device.mtu = 517;
|
|
2115
|
-
|
|
2116
|
-
await runHighVolumeCalls(harness);
|
|
2117
|
-
await harness.transport.release(harness.uuid, true);
|
|
2118
|
-
|
|
2119
|
-
expect(harness.device.requestConnectionPriority).not.toHaveBeenCalled();
|
|
2120
|
-
expect(
|
|
2121
|
-
harness.logger.debug.mock.calls.filter(
|
|
2122
|
-
([message]) =>
|
|
2123
|
-
message ===
|
|
2124
|
-
'[ReactNativeBleTransport] Android BLE connection priority left to peripheral'
|
|
2125
|
-
)
|
|
2126
|
-
).toHaveLength(1);
|
|
2127
|
-
});
|
|
2128
|
-
|
|
2129
|
-
test('keeps requesting HIGH below Android 17 by default', async () => {
|
|
2130
|
-
setPlatformOS('android');
|
|
2131
|
-
setPlatformVersion(36);
|
|
2132
|
-
const harness = createHarness();
|
|
2133
|
-
harness.device.mtu = 517;
|
|
2134
|
-
|
|
2135
|
-
await runHighVolumeCalls(harness);
|
|
2136
|
-
expect(harness.device.requestConnectionPriority).toHaveBeenCalledTimes(1);
|
|
2137
|
-
expect(harness.device.requestConnectionPriority).toHaveBeenCalledWith(1);
|
|
2138
|
-
|
|
2139
|
-
await harness.transport.release(harness.uuid, true);
|
|
2140
|
-
expect(harness.device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
|
|
2141
|
-
});
|
|
2142
|
-
|
|
2143
|
-
test.each([
|
|
2144
|
-
[37, true, true],
|
|
2145
|
-
[36, false, false],
|
|
2146
|
-
] as const)(
|
|
2147
|
-
'applies the tuning switch on API %s when set to %s',
|
|
2148
|
-
async (apiLevel, setting, expectHigh) => {
|
|
2149
|
-
setPlatformOS('android');
|
|
2150
|
-
setPlatformVersion(apiLevel);
|
|
2151
|
-
configureProtocolV2BleTuning({ androidHighConnectionPriority: setting });
|
|
2152
|
-
const harness = createHarness();
|
|
2153
|
-
harness.device.mtu = 517;
|
|
2154
|
-
|
|
2155
|
-
await runHighVolumeCalls(harness);
|
|
2156
|
-
|
|
2157
|
-
if (expectHigh) {
|
|
2158
|
-
expect(harness.device.requestConnectionPriority).toHaveBeenCalledWith(1);
|
|
2159
|
-
} else {
|
|
2160
|
-
expect(harness.device.requestConnectionPriority).not.toHaveBeenCalled();
|
|
2161
|
-
}
|
|
2162
|
-
await harness.transport.release(harness.uuid, true);
|
|
2163
|
-
}
|
|
2164
|
-
);
|
|
2165
|
-
|
|
2166
|
-
test('keeps the switch across unrelated tuning and ignores invalid values', () => {
|
|
2167
|
-
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe('auto');
|
|
2168
|
-
configureProtocolV2BleTuning({ androidHighConnectionPriority: false });
|
|
2169
|
-
configureProtocolV2BleTuning({ androidPacketLength: 200 });
|
|
2170
|
-
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe(false);
|
|
2171
|
-
configureProtocolV2BleTuning({ androidHighConnectionPriority: 'on' as never });
|
|
2172
|
-
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe(false);
|
|
2173
|
-
configureProtocolV2BleTuning({ androidHighConnectionPriority: 'auto' });
|
|
2174
|
-
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe('auto');
|
|
2175
|
-
});
|
|
2176
|
-
|
|
2177
|
-
test('requests HIGH again on the next link after a manual disconnect', async () => {
|
|
2178
|
-
setPlatformOS('android');
|
|
2179
|
-
setPlatformVersion(36);
|
|
2180
|
-
const harness = createHarness();
|
|
2181
|
-
harness.device.mtu = 517;
|
|
2182
|
-
|
|
2183
|
-
await runHighVolumeCalls(harness);
|
|
2184
|
-
expect(harness.device.requestConnectionPriority).toHaveBeenCalledTimes(1);
|
|
2185
|
-
|
|
2186
|
-
await harness.transport.disconnect(harness.uuid);
|
|
2187
|
-
await runHighVolumeCalls(harness);
|
|
2188
|
-
|
|
2189
|
-
expect(
|
|
2190
|
-
harness.device.requestConnectionPriority.mock.calls.filter(([priority]) => priority === 1)
|
|
2191
|
-
).toHaveLength(2);
|
|
2192
|
-
await harness.transport.release(harness.uuid, true);
|
|
2193
|
-
});
|
|
2194
|
-
});
|
|
2195
|
-
|
|
2196
2093
|
test('uses withResponse for an iOS Protocol V2 firmware file write when requested', async () => {
|
|
2197
2094
|
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
2198
2095
|
|