@@ -24,6 +24,7 @@ import {
2424 WriteOptions ,
2525 bluetoothEnabled ,
2626 prepareArgs ,
27+ BluetoothOptions ,
2728} from './bluetooth.common' ;
2829import PQueue from 'p-queue' ;
2930import { Trace } from '@nativescript/core' ;
@@ -1100,7 +1101,7 @@ export class Bluetooth extends BluetoothCommon {
11001101 private LeScanCallback : LeScanCallback ;
11011102
11021103 // with gatt all operations must be queued. Parallel operations will fail
1103- gattQueue : PQueue ;
1104+ gattQueue : PQueue | undefined ;
11041105
11051106 static readonly android = {
11061107 ScanMode,
@@ -1135,7 +1136,7 @@ export class Bluetooth extends BluetoothCommon {
11351136 advertismentData ?: AdvertismentData ;
11361137 } ;
11371138 } = { } ;
1138- constructor ( ) {
1139+ constructor ( restoreIdentifierOrOptions ?: string | Partial < BluetoothOptions > ) {
11391140 super ( ) ;
11401141 if ( Trace . isEnabled ( ) ) {
11411142 CLog ( CLogTypes . info , '*** Android Bluetooth Constructor ***' ) ;
@@ -1149,11 +1150,17 @@ export class Bluetooth extends BluetoothCommon {
11491150 initLeScanCallback ( ) ;
11501151 this . LeScanCallback = new LeScanCallbackVar ( new WeakRef ( this ) ) ;
11511152 }
1152- this . gattQueue = new PQueue ( { concurrency : 1 } ) ;
1153+ if ( typeof restoreIdentifierOrOptions === 'object' && ! ! restoreIdentifierOrOptions . disableAndroidQueue ) {
1154+ this . gattQueue = undefined ;
1155+ } else {
1156+ this . gattQueue = new PQueue ( { concurrency : 1 } ) ;
1157+ }
11531158 }
11541159
11551160 clear ( ) {
1156- this . gattQueue . clear ( ) ;
1161+ if ( this . gattQueue ) {
1162+ this . gattQueue . clear ( ) ;
1163+ }
11571164 }
11581165 broadcastRegistered = false ;
11591166 registerBroadcast ( ) {
@@ -1661,7 +1668,7 @@ export class Bluetooth extends BluetoothCommon {
16611668 onDisconnected : args . onDisconnected ,
16621669 // device: gatt // TODO rename device to gatt?
16631670 } ) ;
1664- await new Promise < void > ( ( resolve , reject ) => {
1671+ return new Promise < void > ( ( resolve , reject ) => {
16651672 const clearListeners = ( ) => {
16661673 this . bluetoothGattCallback . removeSubDelegate ( subD ) ;
16671674 this . removeDisconnectListener ( onDisconnect ) ;
@@ -1727,46 +1734,45 @@ export class Bluetooth extends BluetoothCommon {
17271734 // onDisconnected: args.onDisconnected,
17281735 device : gatt , // TODO rename device to gatt?
17291736 } ) ;
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 ( ) ;
17301775 } ) ;
1731- let services , mtu ;
1732- if ( args . autoDiscoverAll !== false ) {
1733- services = ( await this . discoverAll ( { peripheralUUID : pUUID } ) ) ?. services ;
1734- }
1735- if ( ! ! args . auto2MegPhy ) {
1736- await this . select2MegPhy ( { peripheralUUID : pUUID } ) ;
1737- }
1738- if ( ! ! args . autoMaxMTU ) {
1739- mtu = await this . requestMtu ( { peripheralUUID : pUUID , value : MAX_MTU } ) ;
1740- }
1741- // get the stateObject again to see if we got disconnected
1742- stateObject = this . connections [ pUUID ] ;
1743- if ( ! stateObject ) {
1744- return Promise . reject (
1745- new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
1746- method : methodName ,
1747- arguments : args ,
1748- } )
1749- ) as any ;
1750- }
1751- stateObject . state = 'connected' ;
1752- const adv = stateObject . advertismentData ;
1753- const dataToSend = {
1754- UUID : pUUID , // TODO consider renaming to id (and iOS as well)
1755- name : bluetoothDevice && bluetoothDevice . getName ( ) ,
1756- state : stateObject . state ,
1757- services,
1758- mtu,
1759- nativeDevice : bluetoothDevice ,
1760- localName : adv ?. localName ,
1761- manufacturerId : adv ?. manufacturerId ,
1762- advertismentData : adv ,
1763- } ;
1764- if ( stateObject . onConnected ) {
1765- stateObject . onConnected ( dataToSend ) ;
1766- delete stateObject . onConnected ;
1767- }
1768- this . sendEvent ( Bluetooth . device_connected_event , dataToSend ) ;
1769- return dataToSend ;
17701776 }
17711777 }
17721778
@@ -1800,12 +1806,15 @@ export class Bluetooth extends BluetoothCommon {
18001806 } ) ;
18011807 }
18021808
1803- private addToGatQueue ( p : ( ) => Promise < any > ) {
1804- return this . gattQueue . add ( p ) ;
1809+ private addToGattQueue ( p : ( ) => Promise < any > ) {
1810+ if ( this . gattQueue ) {
1811+ return this . gattQueue . add ( p ) ;
1812+ }
1813+ return p ( ) ;
18051814 }
18061815
18071816 private addToQueue ( args : WrapperOptions , runner : ( wrapper : WrapperResult ) => any ) {
1808- return this . addToGatQueue ( ( ) => this . _getWrapper ( args ) . then ( ( wrapper ) => runner ( wrapper ) ) ) ;
1817+ return this . addToGattQueue ( ( ) => this . _getWrapper ( args ) . then ( ( wrapper ) => runner ( wrapper ) ) ) ;
18091818 }
18101819
18111820 @prepareArgs
@@ -1951,7 +1960,7 @@ export class Bluetooth extends BluetoothCommon {
19511960 if ( Trace . isEnabled ( ) ) {
19521961 CLog ( CLogTypes . info , methodName , args ) ;
19531962 }
1954- return this . addToGatQueue (
1963+ return this . addToGattQueue (
19551964 ( ) =>
19561965 new Promise ( ( resolve , reject ) => {
19571966 if ( Trace . isEnabled ( ) ) {
@@ -2452,7 +2461,7 @@ export class Bluetooth extends BluetoothCommon {
24522461 if ( Trace . isEnabled ( ) ) {
24532462 CLog ( CLogTypes . info , methodName , pUUID , stateObject ) ;
24542463 }
2455- return this . addToGatQueue (
2464+ return this . addToGattQueue (
24562465 ( ) =>
24572466 new Promise ( ( resolve , reject ) => {
24582467 if ( Trace . isEnabled ( ) ) {
@@ -2653,7 +2662,7 @@ export class Bluetooth extends BluetoothCommon {
26532662 if ( Trace . isEnabled ( ) ) {
26542663 CLog ( CLogTypes . info , methodName , pUUID , stateObject ) ;
26552664 }
2656- return this . addToGatQueue (
2665+ return this . addToGattQueue (
26572666 ( ) =>
26582667 new Promise < void > ( ( resolve , reject ) => {
26592668 if ( Trace . isEnabled ( ) ) {
0 commit comments