@onekeyfe/hd-transport-react-native 1.2.8-alpha.13 → 1.2.8-alpha.131

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.
@@ -12,6 +12,7 @@ import ReactNativeBleTransport, {
12
12
  BLE_WRITE_PACKET_TIMEOUT_MS,
13
13
  configureProtocolV2BleTuning,
14
14
  getFirmwareUploadWriteRetryType,
15
+ getProtocolV2BleTuning,
15
16
  resetProtocolV2BleTuning,
16
17
  } from '../index';
17
18
 
@@ -206,6 +207,7 @@ const createHarness = ({
206
207
  bleManager,
207
208
  sentSeqs,
208
209
  writeCharacteristic,
210
+ notifyCharacteristic,
209
211
  setShouldRespond(value: boolean) {
210
212
  shouldRespond = value;
211
213
  },
@@ -821,7 +823,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
821
823
 
822
824
  const acquiring = transport.acquire({ uuid });
823
825
  const failure = expect(acquiring).rejects.toMatchObject({
824
- errorCode: HardwareErrorCode.BleTimeoutError,
826
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
825
827
  });
826
828
  const reconnecting = transport.acquire({ uuid, expectedProtocol: 'V2' });
827
829
 
@@ -860,7 +862,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
860
862
  jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
861
863
 
862
864
  await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
863
- errorCode: HardwareErrorCode.RuntimeError,
865
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
866
+ params: { expectedProtocol: 'V2' },
864
867
  });
865
868
 
866
869
  expect(device.cancelConnection).toHaveBeenCalledTimes(1);
@@ -876,10 +879,12 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
876
879
  jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
877
880
 
878
881
  await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
879
- errorCode: HardwareErrorCode.RuntimeError,
882
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
883
+ params: { expectedProtocol: 'V2' },
880
884
  });
881
885
  await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
882
- errorCode: HardwareErrorCode.RuntimeError,
886
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
887
+ params: { expectedProtocol: 'V2' },
883
888
  });
884
889
 
885
890
  expect(device.cancelConnection).toHaveBeenCalledTimes(2);
@@ -897,7 +902,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
897
902
  jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
898
903
 
899
904
  await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
900
- errorCode: HardwareErrorCode.RuntimeError,
905
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
906
+ params: { expectedProtocol: 'V2' },
901
907
  });
902
908
 
903
909
  expect(device.cancelConnection).toHaveBeenCalledTimes(1);
@@ -1732,7 +1738,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
1732
1738
 
1733
1739
  await expect(transport.acquire({ uuid })).rejects.toBeDefined();
1734
1740
  await expect(transport.acquire({ uuid })).rejects.toMatchObject({
1735
- errorCode: HardwareErrorCode.BleTimeoutError,
1741
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
1736
1742
  });
1737
1743
  await transport.disconnect(uuid);
1738
1744
  await transport.stop();
@@ -1840,7 +1846,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
1840
1846
  jest.spyOn(transport as any, 'probeProtocolV1').mockResolvedValue(false);
1841
1847
 
1842
1848
  await expect(transport.acquire({ uuid, protocolHint: 'V2' })).rejects.toMatchObject({
1843
- errorCode: HardwareErrorCode.BleTimeoutError,
1849
+ errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
1844
1850
  });
1845
1851
 
1846
1852
  expect(device.onDisconnected).not.toHaveBeenCalled();
@@ -1881,6 +1887,80 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
1881
1887
  await transport.release(uuid, true);
1882
1888
  });
1883
1889
 
1890
+ test.each(['ios', 'android'] as const)(
1891
+ 'reuses the connection, monitor and V2 sequence after a kept-alive release on %s',
1892
+ async platform => {
1893
+ setPlatformOS(platform);
1894
+ const { transport, uuid, device, bleManager, notifyCharacteristic, sentSeqs } =
1895
+ createHarness();
1896
+ await transport.acquire({ uuid, expectedProtocol: 'V2' });
1897
+ await transport.call(uuid, 'Ping', { message: 'before-release' });
1898
+ const connectCount = device.connect.mock.calls.length;
1899
+ const cancelTransactionCount = bleManager.cancelTransaction.mock.calls.length;
1900
+ const subscription = notifyCharacteristic.monitor.mock.results[0].value;
1901
+ const invalidateLink = jest.spyOn((transport as any).protocolV2Links, 'invalidateLink');
1902
+
1903
+ try {
1904
+ await transport.release(uuid, false, true);
1905
+ expect(transport.getProtocolType(uuid)).toBe('V2');
1906
+ expect(subscription.remove).not.toHaveBeenCalled();
1907
+ expect(invalidateLink).not.toHaveBeenCalled();
1908
+ expect(bleManager.cancelTransaction).toHaveBeenCalledTimes(cancelTransactionCount);
1909
+ expect(bleManager.cancelDeviceConnection).not.toHaveBeenCalled();
1910
+
1911
+ await transport.acquire({ uuid, expectedProtocol: 'V2' });
1912
+ await expect(
1913
+ transport.call(uuid, 'Ping', { message: 'after-release' })
1914
+ ).resolves.toMatchObject({
1915
+ type: 'Success',
1916
+ });
1917
+ expect(device.connect).toHaveBeenCalledTimes(connectCount);
1918
+ expect(notifyCharacteristic.monitor).toHaveBeenCalledTimes(1);
1919
+ expect(device.onDisconnected).toHaveBeenCalledTimes(1);
1920
+ expect(sentSeqs).toEqual([1, 2, 3]);
1921
+ } finally {
1922
+ await transport.disconnect(uuid);
1923
+ }
1924
+ }
1925
+ );
1926
+
1927
+ test.each(['ios', 'android'] as const)(
1928
+ 'keeps the legacy release path without retention and for V1 on %s',
1929
+ async platform => {
1930
+ setPlatformOS(platform);
1931
+ for (const protocol of ['V1', 'V2'] as const) {
1932
+ const { transport, uuid } = protocol === 'V1' ? createV1Harness() : createHarness();
1933
+ await transport.acquire({ uuid, expectedProtocol: protocol });
1934
+ const releaseNative = jest.spyOn(transport as any, 'releaseNative');
1935
+ await transport.release(uuid, false, protocol === 'V1');
1936
+ expect(releaseNative).toHaveBeenCalledWith(uuid, false);
1937
+ await transport.release(uuid, true);
1938
+ }
1939
+ }
1940
+ );
1941
+
1942
+ test.each([
1943
+ ['ios', 'disconnect'],
1944
+ ['android', 'disconnect'],
1945
+ ['ios', 'stop'],
1946
+ ['android', 'stop'],
1947
+ ['ios', 'onclose'],
1948
+ ['android', 'onclose'],
1949
+ ] as const)('cleans up a retained link on %s / %s', async (platform, action) => {
1950
+ setPlatformOS(platform);
1951
+ const { transport, uuid, notifyCharacteristic } = createHarness();
1952
+ await transport.acquire({ uuid, expectedProtocol: 'V2' });
1953
+ await transport.release(uuid, false, true);
1954
+ const subscription = notifyCharacteristic.monitor.mock.results[0].value;
1955
+
1956
+ if (action === 'onclose') await transport.release(uuid, true, true);
1957
+ else if (action === 'stop') await transport.stop();
1958
+ else await transport.disconnect(uuid);
1959
+
1960
+ expect(subscription.remove).toHaveBeenCalledTimes(1);
1961
+ expect(transport.getProtocolType(uuid)).toBeUndefined();
1962
+ });
1963
+
1884
1964
  test('rejects the active Protocol V2 reader when the current monitor errors', async () => {
1885
1965
  const harness = createHarness();
1886
1966
  const { transport, uuid, sentSeqs } = harness;
@@ -1924,6 +2004,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
1924
2004
  return Promise.resolve();
1925
2005
  });
1926
2006
 
2007
+ await transport.acquire({ uuid, expectedProtocol: 'V2' });
2008
+ await transport.release(uuid, false, true);
1927
2009
  await transport.acquire({ uuid, expectedProtocol: 'V2' });
1928
2010
  harness.setShouldRespond(false);
1929
2011
  await expect(transport.call(uuid, 'Ping', {}, { timeoutMs: 10 })).rejects.toMatchObject({
@@ -2086,6 +2168,101 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
2086
2168
  expect(device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
2087
2169
  });
2088
2170
 
2171
+ describe('Android connection priority by API level', () => {
2172
+ const setPlatformVersion = (version: number | undefined) => {
2173
+ const reactNative: { Platform: { Version?: number } } = jest.requireMock('react-native');
2174
+ reactNative.Platform.Version = version;
2175
+ };
2176
+
2177
+ afterEach(() => {
2178
+ setPlatformVersion(undefined);
2179
+ });
2180
+
2181
+ const runHighVolumeCalls = async (harness: ReturnType<typeof createHarness>) => {
2182
+ await harness.transport.acquire({ uuid: harness.uuid, expectedProtocol: 'V2' });
2183
+ await harness.transport.call(harness.uuid, 'FileWrite', {});
2184
+ await harness.transport.call(harness.uuid, 'FileWrite', {});
2185
+ };
2186
+
2187
+ const createAndroidHarness = (apiLevel: number) => {
2188
+ setPlatformOS('android');
2189
+ setPlatformVersion(apiLevel);
2190
+ const harness = createHarness();
2191
+ harness.device.mtu = 517;
2192
+ return harness;
2193
+ };
2194
+
2195
+ test('leaves the priority to the peripheral on Android 17 by default', async () => {
2196
+ const harness = createAndroidHarness(37);
2197
+
2198
+ await runHighVolumeCalls(harness);
2199
+ await harness.transport.release(harness.uuid, true);
2200
+
2201
+ expect(harness.device.requestConnectionPriority).not.toHaveBeenCalled();
2202
+ expect(
2203
+ harness.logger.debug.mock.calls.filter(
2204
+ ([message]) =>
2205
+ message ===
2206
+ '[ReactNativeBleTransport] Android BLE connection priority left to peripheral'
2207
+ )
2208
+ ).toHaveLength(1);
2209
+ });
2210
+
2211
+ test('keeps requesting HIGH below Android 17 by default', async () => {
2212
+ const harness = createAndroidHarness(36);
2213
+
2214
+ await runHighVolumeCalls(harness);
2215
+ expect(harness.device.requestConnectionPriority).toHaveBeenCalledTimes(1);
2216
+ expect(harness.device.requestConnectionPriority).toHaveBeenCalledWith(1);
2217
+
2218
+ await harness.transport.release(harness.uuid, true);
2219
+ expect(harness.device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
2220
+ });
2221
+
2222
+ test.each([
2223
+ [37, true],
2224
+ [36, false],
2225
+ ] as const)('applies the tuning switch on API %s when set to %s', async (apiLevel, setting) => {
2226
+ configureProtocolV2BleTuning({ androidHighConnectionPriority: setting });
2227
+ const harness = createAndroidHarness(apiLevel);
2228
+
2229
+ await runHighVolumeCalls(harness);
2230
+
2231
+ if (setting) {
2232
+ expect(harness.device.requestConnectionPriority).toHaveBeenCalledWith(1);
2233
+ } else {
2234
+ expect(harness.device.requestConnectionPriority).not.toHaveBeenCalled();
2235
+ }
2236
+ await harness.transport.release(harness.uuid, true);
2237
+ });
2238
+
2239
+ test('keeps the switch across unrelated tuning and ignores invalid values', () => {
2240
+ expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe('auto');
2241
+ configureProtocolV2BleTuning({ androidHighConnectionPriority: false });
2242
+ configureProtocolV2BleTuning({ androidPacketLength: 200 });
2243
+ expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe(false);
2244
+ configureProtocolV2BleTuning({ androidHighConnectionPriority: 'on' as never });
2245
+ expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe(false);
2246
+ configureProtocolV2BleTuning({ androidHighConnectionPriority: 'auto' });
2247
+ expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe('auto');
2248
+ });
2249
+
2250
+ test('requests HIGH again on the next link after a manual disconnect', async () => {
2251
+ const harness = createAndroidHarness(36);
2252
+
2253
+ await runHighVolumeCalls(harness);
2254
+ expect(harness.device.requestConnectionPriority).toHaveBeenCalledTimes(1);
2255
+
2256
+ await harness.transport.disconnect(harness.uuid);
2257
+ await runHighVolumeCalls(harness);
2258
+
2259
+ expect(
2260
+ harness.device.requestConnectionPriority.mock.calls.filter(([priority]) => priority === 1)
2261
+ ).toHaveLength(2);
2262
+ await harness.transport.release(harness.uuid, true);
2263
+ });
2264
+ });
2265
+
2089
2266
  test('uses withResponse for an iOS Protocol V2 firmware file write when requested', async () => {
2090
2267
  const { transport, uuid, writeCharacteristic } = createHarness();
2091
2268