Skip to content

Commit aea9fb4

Browse files
committed
fix: revert breaking changes from PR #235
1 parent 2869b2f commit aea9fb4

File tree

2 files changed

+48
-67
lines changed

2 files changed

+48
-67
lines changed

src/bluetooth.android.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ export class Bluetooth extends BluetoothCommon {
17781778
}
17791779

17801780
@prepareArgs
1781-
public disconnect(args: DisconnectOptions) {
1781+
public async disconnect(args: DisconnectOptions) {
17821782
const methodName = 'disconnect';
17831783
if (!args.UUID) {
17841784
return Promise.reject(
@@ -1802,9 +1802,7 @@ export class Bluetooth extends BluetoothCommon {
18021802
})
18031803
);
18041804
}
1805-
return Promise.resolve().then(() => {
1806-
connection.device.disconnect();
1807-
});
1805+
connection.device.disconnect();
18081806
}
18091807

18101808
private addToGattQueue(p: () => Promise<any>) {

src/bluetooth.ios.ts

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ export class Bluetooth extends BluetoothCommon {
957957
arguments: args,
958958
});
959959
} else {
960-
return new Promise<void>((resolve, reject) => {
960+
await new Promise<void>((resolve, reject) => {
961961
const subD = {
962962
centralManagerDidConnectPeripheral: (central: CBCentralManager, peripheral: CBPeripheral) => {
963963
const UUID = NSUUIDToString(peripheral.identifier);
@@ -989,39 +989,34 @@ export class Bluetooth extends BluetoothCommon {
989989
CLog(CLogTypes.info, methodName, '----about to connect:', connectingUUID, this._centralDelegate, this._centralManager);
990990
}
991991
this.centralManager.connectPeripheralOptions(peripheral, null);
992-
}).then(() => {
993-
// This disconnects the Promise chain so these tasks can run independent of the successful connection response.
994-
Promise.resolve()
995-
.then(() => {
996-
if (args.autoDiscoverAll !== false) {
997-
return this.discoverAll({ peripheralUUID: connectingUUID });
998-
}
999-
return undefined;
1000-
})
1001-
.then((result) => {
1002-
const adv = this._advData[connectingUUID];
1003-
const dataToSend = {
1004-
UUID: connectingUUID,
1005-
name: peripheral.name,
1006-
state: this._getState(peripheral.state),
1007-
services: result?.services,
1008-
localName: adv?.localName,
1009-
manufacturerId: adv?.manufacturerId,
1010-
advertismentData: adv,
1011-
mtu: FIXED_IOS_MTU,
1012-
};
1013-
// delete this._advData[connectingUUID];
1014-
const cb = this._connectCallbacks[connectingUUID];
1015-
if (cb) {
1016-
cb(dataToSend);
1017-
delete this._connectCallbacks[connectingUUID];
1018-
}
1019-
this.sendEvent(Bluetooth.device_connected_event, dataToSend);
1020-
return dataToSend;
1021-
});
1022-
1023-
return Promise.resolve();
1024992
});
993+
let services, mtu = FIXED_IOS_MTU;
994+
if (args.autoDiscoverAll !== false) {
995+
services = (await this.discoverAll({ peripheralUUID: connectingUUID }))?.services;
996+
}
997+
if (!!args.autoMaxMTU) {
998+
mtu = await this.requestMtu({ peripheralUUID: connectingUUID, value: FIXED_IOS_MTU }) ;
999+
}
1000+
const adv = this._advData[connectingUUID];
1001+
const dataToSend = {
1002+
UUID: connectingUUID,
1003+
name: peripheral.name,
1004+
state: this._getState(peripheral.state),
1005+
services,
1006+
nativeDevice: peripheral,
1007+
localName: adv?.localName,
1008+
manufacturerId: adv?.manufacturerId,
1009+
advertismentData: adv,
1010+
mtu,
1011+
};
1012+
// delete this._advData[connectingUUID];
1013+
const cb = this._connectCallbacks[connectingUUID];
1014+
if (cb) {
1015+
cb(dataToSend);
1016+
delete this._connectCallbacks[connectingUUID];
1017+
}
1018+
this.sendEvent(Bluetooth.device_connected_event, dataToSend);
1019+
return dataToSend;
10251020
}
10261021
} catch (ex) {
10271022
if (Trace.isEnabled()) {
@@ -1038,32 +1033,27 @@ export class Bluetooth extends BluetoothCommon {
10381033

10391034
@bluetoothEnabled
10401035
@prepareArgs
1041-
public disconnect(args) {
1036+
public async disconnect(args) {
10421037
const methodName = 'disconnect';
10431038
try {
10441039
if (!args.UUID) {
1045-
return Promise.reject(
1046-
new BluetoothError(BluetoothCommon.msg_missing_parameter, {
1047-
method: methodName,
1048-
type: BluetoothCommon.UUIDKey,
1049-
arguments: args,
1050-
})
1051-
);
1040+
throw new BluetoothError(BluetoothCommon.msg_missing_parameter, {
1041+
method: methodName,
1042+
type: BluetoothCommon.UUIDKey,
1043+
arguments: args,
1044+
});
10521045
}
10531046
const pUUID = args.UUID;
10541047
const peripheral = this.findPeripheral(pUUID);
10551048
if (!peripheral) {
1056-
return Promise.reject(
1057-
new BluetoothError(BluetoothCommon.msg_no_peripheral, {
1058-
method: methodName,
1059-
arguments: args,
1060-
})
1061-
);
1049+
throw new BluetoothError(BluetoothCommon.msg_no_peripheral, {
1050+
method: methodName,
1051+
arguments: args,
1052+
});
10621053
} else {
10631054
// no need to send an error when already disconnected, but it's wise to check it
10641055
if (peripheral.state !== CBPeripheralState.Disconnected) {
1065-
// This disconnects the Promise chain so these tasks can run independent of the successful connection response.
1066-
(new Promise<void>((resolve, reject) => {
1056+
return new Promise<void>((resolve, reject) => {
10671057
const subD = {
10681058
centralManagerDidDisconnectPeripheralError: (central: CBCentralManager, peripheral: CBPeripheral, error?: NSError) => {
10691059
const UUID = NSUUIDToString(peripheral.identifier);
@@ -1087,26 +1077,19 @@ export class Bluetooth extends BluetoothCommon {
10871077
CLog(CLogTypes.info, methodName, '---- Disconnecting peripheral with UUID', pUUID);
10881078
}
10891079
this.centralManager.cancelPeripheralConnection(peripheral);
1090-
})).catch((ex) => {
1091-
if (Trace.isEnabled()) {
1092-
CLog(CLogTypes.error, methodName, '---- error:', ex);
1093-
}
10941080
});
10951081
}
1096-
return Promise.resolve();
10971082
}
10981083
} catch (ex) {
10991084
if (Trace.isEnabled()) {
11001085
CLog(CLogTypes.error, methodName, '---- error:', ex);
11011086
}
1102-
return Promise.reject(
1103-
new BluetoothError(ex.message, {
1104-
stack: ex.stack,
1105-
nativeException: ex.nativeException,
1106-
method: methodName,
1107-
arguments: args,
1108-
})
1109-
);
1087+
throw new BluetoothError(ex.message, {
1088+
stack: ex.stack,
1089+
nativeException: ex.nativeException,
1090+
method: methodName,
1091+
arguments: args,
1092+
});
11101093
}
11111094
}
11121095

@@ -1646,7 +1629,7 @@ export class Bluetooth extends BluetoothCommon {
16461629
}
16471630

16481631
private _findService(UUID: CBUUID, peripheral: CBPeripheral) {
1649-
for (let i = 0; i < peripheral.services.count; i++) {
1632+
for (let i = 0; i < peripheral.services?.count; i++) {
16501633
const service = peripheral.services.objectAtIndex(i);
16511634
// TODO this may need a different compare, see Cordova plugin's findServiceFromUUID function
16521635
if (UUID.isEqual(service.UUID)) {

0 commit comments

Comments
 (0)