Skip to content

Commit ff3bbc8

Browse files
committed
feat: timeout for read / write
1 parent e6fe221 commit ff3bbc8

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/bluetooth.android.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,16 @@ export class Bluetooth extends BluetoothCommon {
18371837
})
18381838
);
18391839
}
1840-
1840+
let timeoutTimer;
1841+
if (args.timeout > 0) {
1842+
timeoutTimer = setTimeout(() => {
1843+
// we need to try catch because the simple fact of creating a new Error actually throws.
1844+
// so we will get an uncaughtException
1845+
try {
1846+
reject(new Error('timeout'));
1847+
} catch {}
1848+
}, args.timeout);
1849+
}
18411850
const pUUID = args.peripheralUUID;
18421851
this.attachSubDelegate(
18431852
{methodName, args, resolve, reject},
@@ -1859,6 +1868,7 @@ export class Bluetooth extends BluetoothCommon {
18591868
);
18601869
}
18611870
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
1871+
timeoutTimer && clearTimeout(timeoutTimer);
18621872
if (status === GATT_SUCCESS) {
18631873
const value = characteristic.getValue();
18641874
resolve({
@@ -1889,6 +1899,8 @@ export class Bluetooth extends BluetoothCommon {
18891899
`${methodName} ---- error: readCharacteristic returned false peripheralUUID:${args.peripheralUUID} serviceUUID:${args.serviceUUID} characteristicUUID:${args.characteristicUUID}`
18901900
);
18911901
}
1902+
timeoutTimer && clearTimeout(timeoutTimer);
1903+
18921904
onError(
18931905
new BluetoothError(BluetoothCommon.msg_error_function_call, {
18941906
method: 'readCharacteristic',
@@ -2029,6 +2041,16 @@ export class Bluetooth extends BluetoothCommon {
20292041
}
20302042

20312043
const pUUID = args.peripheralUUID;
2044+
let timeoutTimer;
2045+
if (args.timeout > 0) {
2046+
timeoutTimer = setTimeout(() => {
2047+
// we need to try catch because the simple fact of creating a new Error actually throws.
2048+
// so we will get an uncaughtException
2049+
try {
2050+
reject(new Error('timeout'));
2051+
} catch {}
2052+
}, args.timeout);
2053+
}
20322054
this.attachSubDelegate(
20332055
{methodName, args, resolve, reject},
20342056
(clearListeners, onError) => ({
@@ -2043,6 +2065,7 @@ export class Bluetooth extends BluetoothCommon {
20432065
const cUUID = uuidToString(characteristic.getUuid());
20442066
const sUUID = uuidToString(characteristic.getService().getUuid());
20452067
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
2068+
timeoutTimer && clearTimeout(timeoutTimer);
20462069
if (status === GATT_SUCCESS) {
20472070
resolve();
20482071
clearListeners();
@@ -2062,6 +2085,7 @@ export class Bluetooth extends BluetoothCommon {
20622085
if (Trace.isEnabled()) {
20632086
CLog(CLogTypes.error, methodName, '---- error: writeCharacteristic returned false');
20642087
}
2088+
timeoutTimer && clearTimeout(timeoutTimer);
20652089
onError(
20662090
new BluetoothError(BluetoothCommon.msg_error_function_call, {
20672091
method: 'writeCharacteristic',
@@ -2690,7 +2714,7 @@ export class Bluetooth extends BluetoothCommon {
26902714
// Returns a list of characteristics included in this service.
26912715
const characteristics = bluetoothGattService.getCharacteristics();
26922716
for (let i = 0; i < characteristics.size(); i++) {
2693-
const c = characteristics.get(i);
2717+
const c = characteristics.get(i) as globalAndroid.bluetooth.BluetoothGattCharacteristic;
26942718
if ((c.getProperties() & charType) !== 0 && characteristicUUID.equals(c.getUuid())) {
26952719
return c;
26962720
}

src/bluetooth.common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,14 @@ export interface CRUDOptions {
461461
}
462462

463463
// tslint:disable-next-line:no-empty-interface
464-
export interface ReadOptions extends CRUDOptions {}
464+
export interface ReadOptions extends CRUDOptions {
465+
timeout?: number;
466+
}
465467

466468
export interface WriteOptions extends CRUDOptions {
467469
value: any;
468470
encoding?: string;
471+
timeout?: number;
469472
}
470473

471474
export interface MtuOptions {

src/bluetooth.ios.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,22 +1169,28 @@ export class Bluetooth extends BluetoothCommon {
11691169

11701170
const pUUID = args.peripheralUUID;
11711171
const p = wrapper.peripheral;
1172+
let timeoutTimer;
1173+
if (args.timeout > 0) {
1174+
timeoutTimer = setTimeout(() => {
1175+
// we need to try catch because the simple fact of creating a new Error actually throws.
1176+
// so we will get an uncaughtException
1177+
try {
1178+
reject(new Error('timeout'));
1179+
} catch {}
1180+
}, args.timeout);
1181+
}
11721182
const subD = {
11731183
peripheralDidUpdateValueForCharacteristicError: (peripheral: CBPeripheral, characteristic: CBCharacteristic, error?: NSError) => {
11741184
if (!characteristic) {
1175-
return reject(
1176-
new BluetoothError(BluetoothCommon.msg_error_function_call, {
1177-
method: 'peripheralDidUpdateValueForCharacteristicError',
1178-
arguments: args,
1179-
})
1180-
);
1185+
return;
11811186
}
11821187

11831188
const UUID = NSUUIDToString(peripheral.identifier);
11841189
const cUUID = CBUUIDToString(characteristic.UUID);
11851190
const sUUID = CBUUIDToString(characteristic.service.UUID);
11861191

11871192
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
1193+
timeoutTimer && clearTimeout(timeoutTimer);
11881194
if (Trace.isEnabled()) {
11891195
CLog(CLogTypes.info, methodName, '---- peripheralDidUpdateValueForCharacteristicError', error);
11901196
}
@@ -1214,6 +1220,7 @@ export class Bluetooth extends BluetoothCommon {
12141220
if (Trace.isEnabled()) {
12151221
CLog(CLogTypes.error, methodName, '---- error:', ex);
12161222
}
1223+
timeoutTimer && clearTimeout(timeoutTimer);
12171224
reject(
12181225
new BluetoothError(ex.message, {
12191226
stack: ex.stack,
@@ -1276,15 +1283,29 @@ export class Bluetooth extends BluetoothCommon {
12761283
}
12771284
const pUUID = args.peripheralUUID;
12781285
const p = wrapper.peripheral;
1286+
let timeoutTimer;
1287+
if (args.timeout > 0) {
1288+
timeoutTimer = setTimeout(() => {
1289+
// we need to try catch because the simple fact of creating a new Error actually throws.
1290+
// so we will get an uncaughtException
1291+
try {
1292+
reject(new Error('timeout'));
1293+
} catch {}
1294+
}, args.timeout);
1295+
}
12791296
const subD = {
12801297
peripheralDidWriteValueForCharacteristicError: (peripheral: CBPeripheral, characteristic: CBCharacteristic, error?: NSError) => {
1298+
if(!characteristic) {
1299+
return;
1300+
}
12811301
if (Trace.isEnabled()) {
12821302
CLog(CLogTypes.info, methodName, '---- peripheralDidWriteValueForCharacteristicError', error);
12831303
}
12841304
const UUID = NSUUIDToString(peripheral.identifier);
12851305
const cUUID = CBUUIDToString(characteristic.UUID);
12861306
const sUUID = CBUUIDToString(characteristic.service.UUID);
12871307
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
1308+
timeoutTimer && clearTimeout(timeoutTimer);
12881309
if (error) {
12891310
reject(
12901311
new BluetoothError(error.localizedDescription, {
@@ -1306,6 +1327,7 @@ export class Bluetooth extends BluetoothCommon {
13061327
if (Trace.isEnabled()) {
13071328
CLog(CLogTypes.error, methodName, '---- error:', ex);
13081329
}
1330+
timeoutTimer && clearTimeout(timeoutTimer);
13091331
p.delegate.removeSubDelegate(subD);
13101332
return reject(
13111333
new BluetoothError(ex.message, {

0 commit comments

Comments
 (0)