From 455874305641ee76071f8dff175e427e960a4d42 Mon Sep 17 00:00:00 2001 From: Stephane Portron Date: Thu, 9 Dec 2021 14:52:46 +0100 Subject: [PATCH] LL-8068: Improve the performance of the BLE data transfers --- packages/devices/src/index.ts | 4 ++ .../src/BleTransport.ts | 45 +++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/packages/devices/src/index.ts b/packages/devices/src/index.ts index 24d60f72d..05429925b 100644 --- a/packages/devices/src/index.ts +++ b/packages/devices/src/index.ts @@ -77,11 +77,13 @@ const devices: { [key in DeviceModelId]: DeviceModel } = { serviceUuid: "d973f2e0-b19e-11e2-9e96-0800200c9a66", notifyUuid: "d973f2e1-b19e-11e2-9e96-0800200c9a66", writeUuid: "d973f2e2-b19e-11e2-9e96-0800200c9a66", + writeCmdUuid: "d973f2e3-b19e-11e2-9e96-0800200c9a66", }, { serviceUuid: "13d63400-2c97-0004-0000-4c6564676572", notifyUuid: "13d63400-2c97-0004-0001-4c6564676572", writeUuid: "13d63400-2c97-0004-0002-4c6564676572", + writeCmdUuid: "13d63400-2c97-0004-0003-4c6564676572", }, ], }, @@ -196,6 +198,7 @@ export interface DeviceModel { bluetoothSpec?: { serviceUuid: string; writeUuid: string; + writeCmdUuid: string; notifyUuid: string; }[]; } @@ -207,5 +210,6 @@ export interface BluetoothInfos { deviceModel: DeviceModel; serviceUuid: string; writeUuid: string; + writeCmdUuid: string; notifyUuid: string; } diff --git a/packages/react-native-hw-transport-ble/src/BleTransport.ts b/packages/react-native-hw-transport-ble/src/BleTransport.ts index b1e2362e8..01b24dfc0 100644 --- a/packages/react-native-hw-transport-ble/src/BleTransport.ts +++ b/packages/react-native-hw-transport-ble/src/BleTransport.ts @@ -26,6 +26,7 @@ import { awaitsBleOn } from "./awaitsBleOn"; import { decoratePromiseErrors, remapError } from "./remapErrors"; let connectOptions: Record = { requestMTU: 156, + connectionPriority:1, }; const transportsCache = {}; const bleManager = new BleManager(); @@ -147,7 +148,7 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) { throw new TransportError("service not found", "BLEServiceNotFound"); } - const { deviceModel, serviceUuid, writeUuid, notifyUuid } = res; + const { deviceModel, serviceUuid, writeUuid, writeCmdUuid, notifyUuid } = res; if (!characteristics) { characteristics = await device.characteristicsForService(serviceUuid); @@ -158,11 +159,14 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) { } let writeC; + let writeCmdC; let notifyC; for (const c of characteristics) { if (c.uuid === writeUuid) { writeC = c; + } else if (c.uuid === writeCmdUuid) { + writeCmdC = c; } else if (c.uuid === notifyUuid) { notifyC = c; } @@ -196,6 +200,15 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) { ); } + if (writeCmdC) { + if (!writeCmdC.isWritableWithoutResponse) { + throw new TransportError( + "write cmd characteristic not writableWithoutResponse", + "BLEChracteristicInvalid" + ); + } + } + log("ble-verbose", `device.mtu=${device.mtu}`); const notifyObservable = monitorCharacteristic(notifyC).pipe( tap((value) => { @@ -207,6 +220,7 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) { const transport = new BluetoothTransport( device, writeC, + writeCmdC, notifyObservable, deviceModel ); @@ -373,6 +387,7 @@ export default class BluetoothTransport extends Transport { device: Device; mtuSize = 20; writeCharacteristic: Characteristic; + writeCmdCharacteristic: Characteristic; notifyObservable: Observable; deviceModel: DeviceModel; notYetDisconnected = true; @@ -380,6 +395,7 @@ export default class BluetoothTransport extends Transport { constructor( device: Device, writeCharacteristic: Characteristic, + writeCmdCharacteristic: Characteristic, notifyObservable: Observable, deviceModel: DeviceModel ) { @@ -387,6 +403,7 @@ export default class BluetoothTransport extends Transport { this.id = device.id; this.device = device; this.writeCharacteristic = writeCharacteristic; + this.writeCmdCharacteristic = writeCmdCharacteristic; this.notifyObservable = notifyObservable; this.deviceModel = deviceModel; log("ble-verbose", `BleTransport(${String(this.id)}) new instance`); @@ -470,13 +487,25 @@ export default class BluetoothTransport extends Transport { write = async (buffer: Buffer, txid?: string | null | undefined) => { log("ble-frame", "=> " + buffer.toString("hex")); - try { - await this.writeCharacteristic.writeWithResponse( - buffer.toString("base64"), - txid - ); - } catch (e: any) { - throw new DisconnectedDeviceDuringOperation(e.message); + if (!this.writeCmdCharacteristic) { + try { + await this.writeCharacteristic.writeWithResponse( + buffer.toString("base64"), + txid + ); + } catch (e: any) { + throw new DisconnectedDeviceDuringOperation(e.message); + } + } + else { + try { + await this.writeCmdCharacteristic.writeWithoutResponse( + buffer.toString("base64"), + txid + ); + } catch (e: any) { + throw new DisconnectedDeviceDuringOperation(e.message); + } } };