@@ -821,7 +821,7 @@ function initBluetoothGattCallback() {
821821 if ( device == null ) {
822822 // happens some time, why ... ?
823823 } else {
824- pUUID = device . getAddress ( ) ;
824+ pUUID = device . getAddress ( ) ;
825825 }
826826 if ( Trace . isEnabled ( ) ) {
827827 CLog ( CLogTypes . info , `TNS_BluetoothGattCallback.onCharacteristicChanged ---- gatt: ${ gatt } , characteristic: ${ characteristic } , device: ${ pUUID } ` ) ;
@@ -949,23 +949,23 @@ function initBluetoothGattCallback() {
949949 * @param status - GATT_SUCCESS if the PHY has been changed successfully.
950950 */
951951 onPhyUpdate ( gatt : android . bluetooth . BluetoothGatt , txPhy : number , rxPhy : number , status : number ) {
952- if ( Trace . isEnabled ( ) ) {
953- CLog ( CLogTypes . info , " TNS_BluetoothGattCallback.onPhyUpdate ---- gatt: " + gatt + " txPhy: " + txPhy + " rxPhy: " + rxPhy + " , status: " + status ) ;
954- }
955- var owner = this . owner . get ( ) ;
956- if ( owner ) {
957- owner . notify ( {
958- eventName : 'phy' ,
959- object : owner ,
960- data : { txPhy, rxPhy} ,
961- } ) ;
962- }
963- this . subDelegates . forEach ( function ( d ) {
964- if ( d . onPhyUpdate ) {
965- d . onPhyUpdate ( gatt , txPhy , rxPhy , status ) ;
966- }
967- } ) ;
968- } ;
952+ if ( Trace . isEnabled ( ) ) {
953+ CLog ( CLogTypes . info , ' TNS_BluetoothGattCallback.onPhyUpdate ---- gatt: ' + gatt + ' txPhy: ' + txPhy + ' rxPhy: ' + rxPhy + ' , status: ' + status ) ;
954+ }
955+ const owner = this . owner . get ( ) ;
956+ if ( owner ) {
957+ owner . notify ( {
958+ eventName : 'phy' ,
959+ object : owner ,
960+ data : { txPhy, rxPhy} ,
961+ } ) ;
962+ }
963+ this . subDelegates . forEach ( function ( d ) {
964+ if ( d . onPhyUpdate ) {
965+ d . onPhyUpdate ( gatt , txPhy , rxPhy , status ) ;
966+ }
967+ } ) ;
968+ } ;
969969 }
970970 BluetoothGattCallback = BluetoothGattCallbackImpl ;
971971}
@@ -1125,7 +1125,7 @@ export class Bluetooth extends BluetoothCommon {
11251125 public connections : {
11261126 [ k : string ] : {
11271127 state : ConnectionState ;
1128- onConnected ?: ( e : { UUID : string ; name : string ; state : string ; services ?: Service [ ] ; advertismentData : AdvertismentData , mtu ?: number } ) => void ;
1128+ onConnected ?: ( e : { UUID : string ; name : string ; state : string ; services ?: Service [ ] ; advertismentData : AdvertismentData ; mtu ?: number } ) => void ;
11291129 onDisconnected ?: ( e : { UUID : string ; name : string } ) => void ;
11301130 device ?: android . bluetooth . BluetoothGatt ;
11311131 onNotifyCallbacks ?: {
@@ -1605,7 +1605,7 @@ export class Bluetooth extends BluetoothCommon {
16051605
16061606 @bluetoothEnabled
16071607 @prepareArgs
1608- public connect ( args : ConnectOptions ) {
1608+ public async connect ( args : ConnectOptions ) {
16091609 // or macaddress..
16101610 const methodName = 'connect' ;
16111611 if ( ! args . UUID ) {
@@ -1643,7 +1643,7 @@ export class Bluetooth extends BluetoothCommon {
16431643 onDisconnected : args . onDisconnected ,
16441644 // device: gatt // TODO rename device to gatt?
16451645 } ) ;
1646- return new Promise < void > ( ( resolve , reject ) => {
1646+ await new Promise < void > ( ( resolve , reject ) => {
16471647 const clearListeners = ( ) => {
16481648 this . bluetoothGattCallback . removeSubDelegate ( subD ) ;
16491649 this . removeDisconnectListener ( onDisconnect ) ;
@@ -1709,40 +1709,45 @@ export class Bluetooth extends BluetoothCommon {
17091709 // onDisconnected: args.onDisconnected,
17101710 device : gatt , // TODO rename device to gatt?
17111711 } ) ;
1712- } )
1713- . then ( ( ) => ! ! args . autoDiscoverAll ? this . discoverAll ( { peripheralUUID : pUUID } ) . then ( ( result ) => result ?. services ) : undefined )
1714- . then ( ( services ) => ( ! ! args . auto2MegPhy ? this . select2MegPhy ( { peripheralUUID : pUUID } ) : Promise . resolve ( ) ) . then ( ( ) => services ) )
1715- . then ( ( services ) => ( ! ! args . autoMaxMTU ? this . requestMtu ( { peripheralUUID : pUUID , value : MAX_MTU } ) : Promise . resolve ( undefined ) )
1716- . then ( ( mtu ?: number ) => ( { services, mtu} ) ) )
1717- . then ( ( { services, mtu} ) => {
1718- const stateObject = this . connections [ pUUID ] ;
1719- if ( ! stateObject ) {
1720- return Promise . reject (
1721- new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
1722- method : methodName ,
1723- arguments : args ,
1724- } )
1725- ) as any ;
1726- }
1727- stateObject . state = 'connected' ;
1728- const adv = stateObject . advertismentData ;
1729- const dataToSend = {
1730- UUID : pUUID , // TODO consider renaming to id (and iOS as well)
1731- name : bluetoothDevice && bluetoothDevice . getName ( ) ,
1732- state : stateObject . state ,
1733- services,
1734- mtu,
1735- localName : adv ?. localName ,
1736- manufacturerId : adv ?. manufacturerId ,
1737- advertismentData : adv ,
1738- } ;
1739- if ( stateObject . onConnected ) {
1740- stateObject . onConnected ( dataToSend ) ;
1741- delete stateObject . onConnected ;
1742- }
1743- this . sendEvent ( Bluetooth . device_connected_event , dataToSend ) ;
1744- return dataToSend ;
1745- } ) ;
1712+ } ) ;
1713+ let services , mtu ;
1714+ if ( args . autoDiscoverAll !== false ) {
1715+ services = ( await this . discoverAll ( { peripheralUUID : pUUID } ) ) ?. services ;
1716+ }
1717+ if ( ! ! args . auto2MegPhy ) {
1718+ await this . select2MegPhy ( { peripheralUUID : pUUID } ) ;
1719+ }
1720+ if ( ! ! args . autoMaxMTU ) {
1721+ mtu = await this . requestMtu ( { peripheralUUID : pUUID , value : MAX_MTU } ) ;
1722+ }
1723+ // get the stateObject again to see if we got disconnected
1724+ stateObject = this . connections [ pUUID ] ;
1725+ if ( ! stateObject ) {
1726+ return Promise . reject (
1727+ new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
1728+ method : methodName ,
1729+ arguments : args ,
1730+ } )
1731+ ) as any ;
1732+ }
1733+ stateObject . state = 'connected' ;
1734+ const adv = stateObject . advertismentData ;
1735+ const dataToSend = {
1736+ UUID : pUUID , // TODO consider renaming to id (and iOS as well)
1737+ name : bluetoothDevice && bluetoothDevice . getName ( ) ,
1738+ state : stateObject . state ,
1739+ services,
1740+ mtu,
1741+ localName : adv ?. localName ,
1742+ manufacturerId : adv ?. manufacturerId ,
1743+ advertismentData : adv ,
1744+ } ;
1745+ if ( stateObject . onConnected ) {
1746+ stateObject . onConnected ( dataToSend ) ;
1747+ delete stateObject . onConnected ;
1748+ }
1749+ this . sendEvent ( Bluetooth . device_connected_event , dataToSend ) ;
1750+ return dataToSend ;
17461751 }
17471752 }
17481753
@@ -2618,7 +2623,7 @@ export class Bluetooth extends BluetoothCommon {
26182623 }
26192624
26202625 private attachSubDelegate (
2621- ctx : { methodName : string , args : any , resolve , reject } ,
2626+ ctx : { methodName : string ; args : any ; resolve ; reject } ,
26222627 createSubDelegate : ( clearListeners : ( ) => void , onError : ( err ) => void ) => SubBluetoothGattCallback ,
26232628 executeTask : ( onError : ( err ) => void ) => void ,
26242629 ) : void {
0 commit comments