Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
LL-8068: Improve the performance of the BLE data transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane Portron committed Dec 9, 2021
1 parent 4bcf5bb commit 4558743
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/devices/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
},
Expand Down Expand Up @@ -196,6 +198,7 @@ export interface DeviceModel {
bluetoothSpec?: {
serviceUuid: string;
writeUuid: string;
writeCmdUuid: string;
notifyUuid: string;
}[];
}
Expand All @@ -207,5 +210,6 @@ export interface BluetoothInfos {
deviceModel: DeviceModel;
serviceUuid: string;
writeUuid: string;
writeCmdUuid: string;
notifyUuid: string;
}
45 changes: 37 additions & 8 deletions packages/react-native-hw-transport-ble/src/BleTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { awaitsBleOn } from "./awaitsBleOn";
import { decoratePromiseErrors, remapError } from "./remapErrors";
let connectOptions: Record<string, unknown> = {
requestMTU: 156,
connectionPriority:1,
};
const transportsCache = {};
const bleManager = new BleManager();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) => {
Expand All @@ -207,6 +220,7 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) {
const transport = new BluetoothTransport(
device,
writeC,
writeCmdC,
notifyObservable,
deviceModel
);
Expand Down Expand Up @@ -373,20 +387,23 @@ export default class BluetoothTransport extends Transport {
device: Device;
mtuSize = 20;
writeCharacteristic: Characteristic;
writeCmdCharacteristic: Characteristic;
notifyObservable: Observable<Buffer>;
deviceModel: DeviceModel;
notYetDisconnected = true;

constructor(
device: Device,
writeCharacteristic: Characteristic,
writeCmdCharacteristic: Characteristic,
notifyObservable: Observable<Buffer>,
deviceModel: DeviceModel
) {
super();
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`);
Expand Down Expand Up @@ -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);
}
}
};

Expand Down

0 comments on commit 4558743

Please sign in to comment.