Skip to content

Commit 49fe431

Browse files
committed
fix: isConnected should not throw if device is not connected but return false
1 parent 90e5a2b commit 49fe431

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

src/bluetooth.android.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,23 +1394,18 @@ export class Bluetooth extends BluetoothCommon {
13941394

13951395
@bluetoothEnabled
13961396
@prepareArgs
1397-
public isConnected(args) {
1397+
public async isConnected(args) {
13981398
const methodName = 'isConnected';
13991399
// TODO: no try catch for now as we rely on our own connections state object.
14001400
// try {
14011401
if (!args.UUID) {
1402-
return Promise.reject(new BluetoothError(BluetoothCommon.msg_missing_parameter, { type: BluetoothCommon.UUIDKey, method: methodName }));
1402+
throw new BluetoothError(BluetoothCommon.msg_missing_parameter, { type: BluetoothCommon.UUIDKey, method: methodName });
14031403
}
14041404
const stateObject = this.connections[args.UUID];
14051405
if (!stateObject) {
1406-
return Promise.reject(
1407-
new BluetoothError(BluetoothCommon.msg_peripheral_not_connected, {
1408-
method: methodName,
1409-
arguments: args,
1410-
})
1411-
);
1406+
return false;
14121407
}
1413-
return Promise.resolve(stateObject.state === 'connected');
1408+
return stateObject.state === 'connected';
14141409
}
14151410

14161411
public openBluetoothSettings() {

src/bluetooth.ios.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,45 +1098,37 @@ export class Bluetooth extends BluetoothCommon {
10981098

10991099
@bluetoothEnabled
11001100
@prepareArgs
1101-
public isConnected(args) {
1101+
public async isConnected(args) {
11021102
const methodName = 'isConnected';
11031103
try {
11041104
if (!args.UUID) {
1105-
return Promise.reject(
1106-
new BluetoothError(BluetoothCommon.msg_missing_parameter, {
1107-
method: methodName,
1108-
type: BluetoothCommon.UUIDKey,
1109-
arguments: args,
1110-
})
1111-
);
1105+
throw new BluetoothError(BluetoothCommon.msg_missing_parameter, {
1106+
method: methodName,
1107+
type: BluetoothCommon.UUIDKey,
1108+
arguments: args,
1109+
});
1110+
11121111
}
11131112
const peripheral = this.findPeripheral(args.UUID);
11141113
if (peripheral === null) {
1115-
return Promise.reject(
1116-
new BluetoothError(BluetoothCommon.msg_no_peripheral, {
1117-
method: methodName,
1118-
arguments: args,
1119-
})
1120-
);
1114+
return false;
11211115
} else {
11221116
if (Trace.isEnabled()) {
11231117
CLog(CLogTypes.info, methodName, '---- checking connection with peripheral UUID:', args.UUID);
11241118
}
1125-
return Promise.resolve(peripheral.state === CBPeripheralState.Connected);
1119+
return peripheral.state === CBPeripheralState.Connected;
11261120
}
11271121
} catch (ex) {
11281122
if (Trace.isEnabled()) {
11291123
CLog(CLogTypes.error, methodName, '---- error:', ex);
11301124
}
11311125

1132-
return Promise.reject(
1133-
new BluetoothError(ex.message, {
1134-
stack: ex.stack,
1135-
nativeException: ex.nativeException,
1136-
method: methodName,
1137-
arguments: args,
1138-
})
1139-
);
1126+
throw new BluetoothError(ex.message, {
1127+
stack: ex.stack,
1128+
nativeException: ex.nativeException,
1129+
method: methodName,
1130+
arguments: args,
1131+
});
11401132
}
11411133
}
11421134

0 commit comments

Comments
 (0)