@onekeyfe/hd-transport-react-native 1.2.0-alpha.67 → 1.2.0-alpha.69

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,4 +1,4 @@
1
- import { ANDROID_DEFAULT_MTU, ANDROID_PACKET_LENGTH, IOS_PACKET_LENGTH } from './constants';
1
+ import { ANDROID_PROTOCOL_V2_PACKET_LENGTH, IOS_PROTOCOL_V2_PACKET_LENGTH } from './constants';
2
2
 
3
3
  export type BlePlatform = 'ios' | 'android' | string;
4
4
 
@@ -13,8 +13,8 @@ export function hasWritableCapability(characteristic: BleWriteCapability) {
13
13
 
14
14
  export function resolveProtocolV2PacketCapacity({
15
15
  platform,
16
- iosPacketLength = IOS_PACKET_LENGTH,
17
- androidPacketLength = ANDROID_PACKET_LENGTH,
16
+ iosPacketLength = IOS_PROTOCOL_V2_PACKET_LENGTH,
17
+ androidPacketLength = ANDROID_PROTOCOL_V2_PACKET_LENGTH,
18
18
  mtu,
19
19
  }: {
20
20
  platform: BlePlatform;
@@ -22,14 +22,27 @@ export function resolveProtocolV2PacketCapacity({
22
22
  androidPacketLength?: number;
23
23
  mtu?: number | null;
24
24
  }) {
25
- if (platform === 'ios') {
26
- return iosPacketLength;
25
+ if (typeof mtu !== 'number' || !Number.isFinite(mtu) || mtu <= 3) {
26
+ throw new Error(`Protocol V2 BLE requires a negotiated MTU, received: ${String(mtu)}`);
27
27
  }
28
28
 
29
- if (platform === 'android') {
30
- const payloadLength = Math.max((mtu ?? ANDROID_DEFAULT_MTU) - 3, 1);
31
- return Math.min(androidPacketLength, payloadLength);
32
- }
29
+ const payloadLength = Math.floor(mtu) - 3;
30
+ const configuredPacketLength = platform === 'ios' ? iosPacketLength : androidPacketLength;
31
+ return Math.min(configuredPacketLength, payloadLength);
32
+ }
33
33
 
34
- return androidPacketLength;
34
+ export function shouldWriteProtocolV2WithResponse({
35
+ platform,
36
+ highVolume,
37
+ requestedWithResponse,
38
+ characteristic,
39
+ }: {
40
+ platform: BlePlatform;
41
+ highVolume: boolean;
42
+ requestedWithResponse?: boolean;
43
+ characteristic: BleWriteCapability;
44
+ }) {
45
+ if (!characteristic.isWritableWithResponse) return false;
46
+ if (!characteristic.isWritableWithoutResponse) return true;
47
+ return requestedWithResponse === true || (platform === 'ios' && !highVolume);
35
48
  }
package/src/constants.ts CHANGED
@@ -2,7 +2,8 @@ import { createKnownBleUuidAliases, matchesKnownBleUuid } from '@onekeyfe/hd-sha
2
2
 
3
3
  export const IOS_PACKET_LENGTH = 128;
4
4
  export const ANDROID_PACKET_LENGTH = 192;
5
- export const ANDROID_DEFAULT_MTU = 23;
5
+ export const IOS_PROTOCOL_V2_PACKET_LENGTH = 244;
6
+ export const ANDROID_PROTOCOL_V2_PACKET_LENGTH = 514;
6
7
 
7
8
  type BluetoothServices = Record<
8
9
  string,