@@ -1668,7 +1668,7 @@ export class Bluetooth extends BluetoothCommon {
16681668 onDisconnected : args . onDisconnected ,
16691669 // device: gatt // TODO rename device to gatt?
16701670 } ) ;
1671- return new Promise < void > ( ( resolve , reject ) => {
1671+ await new Promise < void > ( ( resolve , reject ) => {
16721672 const clearListeners = ( ) => {
16731673 this . bluetoothGattCallback . removeSubDelegate ( subD ) ;
16741674 this . removeDisconnectListener ( onDisconnect ) ;
@@ -1734,45 +1734,46 @@ export class Bluetooth extends BluetoothCommon {
17341734 // onDisconnected: args.onDisconnected,
17351735 device : gatt , // TODO rename device to gatt?
17361736 } ) ;
1737- } ) . then ( ( ) => {
1738- // This disconnects the Promise chain so these tasks can run independent of the successful connection response.
1739- Promise . resolve ( )
1740- . then ( ( ) => ! ! args . autoDiscoverAll ? this . discoverAll ( { peripheralUUID : pUUID } ) . then ( ( result ) => result ?. services ) : undefined )
1741- . then ( ( services ) => ( ! ! args . auto2MegPhy ? this . select2MegPhy ( { peripheralUUID : pUUID } ) : Promise . resolve ( ) ) . then ( ( ) => services ) )
1742- . then ( ( services ) => ( ! ! args . autoMaxMTU ? this . requestMtu ( { peripheralUUID : pUUID , value : MAX_MTU } ) : Promise . resolve ( undefined ) )
1743- . then ( ( mtu ?: number ) => ( { services, mtu} ) ) )
1744- . then ( ( { services, mtu} ) => {
1745- const stateObject = this . connections [ pUUID ] ;
1746- if ( ! stateObject ) {
1747- return Promise . reject (
1748- new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
1749- method : methodName ,
1750- arguments : args ,
1751- } )
1752- ) as any ;
1753- }
1754- stateObject . state = 'connected' ;
1755- const adv = stateObject . advertismentData ;
1756- const dataToSend = {
1757- UUID : pUUID , // TODO consider renaming to id (and iOS as well)
1758- name : bluetoothDevice && bluetoothDevice . getName ( ) ,
1759- state : stateObject . state ,
1760- services,
1761- mtu,
1762- localName : adv ?. localName ,
1763- manufacturerId : adv ?. manufacturerId ,
1764- advertismentData : adv ,
1765- } ;
1766- if ( stateObject . onConnected ) {
1767- stateObject . onConnected ( dataToSend ) ;
1768- delete stateObject . onConnected ;
1769- }
1770- this . sendEvent ( Bluetooth . device_connected_event , dataToSend ) ;
1771- return dataToSend ;
1772- } ) ;
1773-
1774- return Promise . resolve ( ) ;
17751737 } ) ;
1738+ let services , mtu ;
1739+ if ( args . autoDiscoverAll !== false ) {
1740+ services = ( await this . discoverAll ( { peripheralUUID : pUUID } ) ) ?. services ;
1741+ }
1742+ if ( ! ! args . auto2MegPhy ) {
1743+ await this . select2MegPhy ( { peripheralUUID : pUUID } ) ;
1744+ }
1745+ if ( ! ! args . autoMaxMTU ) {
1746+ mtu = await this . requestMtu ( { peripheralUUID : pUUID , value : MAX_MTU } ) ;
1747+ }
1748+ // get the stateObject again to see if we got disconnected
1749+ stateObject = this . connections [ pUUID ] ;
1750+ if ( ! stateObject ) {
1751+ return Promise . reject (
1752+ new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
1753+ method : methodName ,
1754+ arguments : args ,
1755+ } )
1756+ ) as any ;
1757+ }
1758+ stateObject . state = 'connected' ;
1759+ const adv = stateObject . advertismentData ;
1760+ const dataToSend = {
1761+ UUID : pUUID , // TODO consider renaming to id (and iOS as well)
1762+ name : bluetoothDevice && bluetoothDevice . getName ( ) ,
1763+ state : stateObject . state ,
1764+ services,
1765+ mtu,
1766+ nativeDevice : bluetoothDevice ,
1767+ localName : adv ?. localName ,
1768+ manufacturerId : adv ?. manufacturerId ,
1769+ advertismentData : adv ,
1770+ } ;
1771+ if ( stateObject . onConnected ) {
1772+ stateObject . onConnected ( dataToSend ) ;
1773+ delete stateObject . onConnected ;
1774+ }
1775+ this . sendEvent ( Bluetooth . device_connected_event , dataToSend ) ;
1776+ return dataToSend ;
17761777 }
17771778 }
17781779
0 commit comments