Skip to content

Commit 6b1a8f8

Browse files
committed
fix(ios): correctly query mtu on connection
1 parent dda3876 commit 6b1a8f8

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

src/bluetooth.ios.ts

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ export class Bluetooth extends BluetoothCommon {
896896

897897
@bluetoothEnabled
898898
@prepareArgs
899-
public connect(args: ConnectOptions) {
899+
public async connect(args: ConnectOptions) {
900900
const methodName = 'connect';
901901
try {
902902
if (!args.UUID) {
@@ -919,14 +919,12 @@ export class Bluetooth extends BluetoothCommon {
919919
}
920920

921921
if (!peripheral) {
922-
return Promise.reject(
923-
new BluetoothError(BluetoothCommon.msg_no_peripheral, {
924-
method: methodName,
925-
arguments: args,
926-
})
927-
);
922+
throw new BluetoothError(BluetoothCommon.msg_no_peripheral, {
923+
method: methodName,
924+
arguments: args,
925+
});
928926
} else {
929-
return new Promise<void>((resolve, reject) => {
927+
await new Promise<void>((resolve, reject) => {
930928
const subD = {
931929
centralManagerDidConnectPeripheral: (central: CBCentralManager, peripheral: CBPeripheral) => {
932930
const UUID = NSUUIDToString(peripheral.identifier);
@@ -958,47 +956,44 @@ export class Bluetooth extends BluetoothCommon {
958956
CLog(CLogTypes.info, methodName, '----about to connect:', connectingUUID, this._centralDelegate, this._centralManager);
959957
}
960958
this.centralManager.connectPeripheralOptions(peripheral, null);
961-
})
962-
.then(() => {
963-
if (args.autoDiscoverAll !== false) {
964-
return this.discoverAll({ peripheralUUID: connectingUUID });
965-
}
966-
return undefined;
967-
})
968-
.then((result) => {
969-
const adv = this._advData[connectingUUID];
970-
const dataToSend = {
971-
UUID: connectingUUID,
972-
name: peripheral.name,
973-
state: this._getState(peripheral.state),
974-
services: result?.services,
975-
localName: adv?.localName,
976-
manufacturerId: adv?.manufacturerId,
977-
advertismentData: adv,
978-
mtu: FIXED_IOS_MTU,
979-
};
980-
// delete this._advData[connectingUUID];
981-
const cb = this._connectCallbacks[connectingUUID];
982-
if (cb) {
983-
cb(dataToSend);
984-
delete this._connectCallbacks[connectingUUID];
985-
}
986-
this.sendEvent(Bluetooth.device_connected_event, dataToSend);
987-
return dataToSend;
988-
});
959+
});
960+
let services, mtu = FIXED_IOS_MTU;
961+
if (args.autoDiscoverAll !== false) {
962+
services = (await this.discoverAll({ peripheralUUID: connectingUUID }))?.services;
963+
}
964+
if (!!args.autoMaxMTU) {
965+
mtu = await this.requestMtu({ peripheralUUID: connectingUUID, value: FIXED_IOS_MTU }) ;
966+
}
967+
const adv = this._advData[connectingUUID];
968+
const dataToSend = {
969+
UUID: connectingUUID,
970+
name: peripheral.name,
971+
state: this._getState(peripheral.state),
972+
services,
973+
localName: adv?.localName,
974+
manufacturerId: adv?.manufacturerId,
975+
advertismentData: adv,
976+
mtu,
977+
};
978+
// delete this._advData[connectingUUID];
979+
const cb = this._connectCallbacks[connectingUUID];
980+
if (cb) {
981+
cb(dataToSend);
982+
delete this._connectCallbacks[connectingUUID];
983+
}
984+
this.sendEvent(Bluetooth.device_connected_event, dataToSend);
985+
return dataToSend;
989986
}
990987
} catch (ex) {
991988
if (Trace.isEnabled()) {
992989
CLog(CLogTypes.error, methodName, '---- error:', ex);
993990
}
994-
return Promise.reject(
995-
new BluetoothError(ex.message, {
996-
stack: ex.stack,
997-
nativeException: ex.nativeException,
998-
method: methodName,
999-
arguments: args,
1000-
})
1001-
);
991+
throw new BluetoothError(ex.message, {
992+
stack: ex.stack,
993+
nativeException: ex.nativeException,
994+
method: methodName,
995+
arguments: args,
996+
});
1002997
}
1003998
}
1004999

@@ -1228,11 +1223,9 @@ export class Bluetooth extends BluetoothCommon {
12281223
if (!peripheral) {
12291224
return Promise.reject(new BluetoothError(BluetoothCommon.msg_no_peripheral, { method: methodName, arguments: args }));
12301225
}
1231-
// return this._getWrapper(args, CBCharacteristicProperties.PropertyWrite).then(wrapper => {
12321226
return Promise.resolve(
12331227
Math.min(peripheral.maximumWriteValueLengthForType(CBCharacteristicWriteType.WithoutResponse), peripheral.maximumWriteValueLengthForType(CBCharacteristicWriteType.WithResponse))
12341228
);
1235-
// });
12361229
}
12371230
@prepareArgs
12381231
public write(args: WriteOptions) {

0 commit comments

Comments
 (0)