@@ -21,12 +21,24 @@ export class WinPeripheral extends Peripheral {
2121 public async connect ( ) : Promise < GattRemote > {
2222 this . _state = 'connecting' ;
2323
24- this . adapter . noble . connect ( this . uuid ) ;
24+ await new Promise < void > ( ( res , rej ) => {
25+ const cleanup = ( ) => {
26+ clearTimeout ( timer ) ;
27+ this . adapter . noble . off ( 'connect' , connHandler ) ;
28+ } ;
29+ const resolve = ( ) => {
30+ cleanup ( ) ;
31+ res ( ) ;
32+ } ;
33+ const reject = ( err : Error ) => {
34+ cleanup ( ) ;
35+ rej ( err ) ;
36+ } ;
37+
38+ const timer = setTimeout ( ( ) => reject ( new Error ( 'Connecting timed out' ) ) , 10000 ) ;
2539
26- await new Promise < void > ( ( resolve , reject ) => {
2740 const connHandler = ( uuid : string , err : Error ) => {
2841 if ( uuid === this . uuid ) {
29- this . adapter . noble . off ( 'connect' , connHandler ) ;
3042 if ( err ) {
3143 reject ( err ) ;
3244 } else {
@@ -35,6 +47,8 @@ export class WinPeripheral extends Peripheral {
3547 }
3648 } ;
3749 this . adapter . noble . on ( 'connect' , connHandler ) ;
50+
51+ this . adapter . noble . connect ( this . uuid ) ;
3852 } ) ;
3953
4054 this . _gatt = new WinGatt ( this ) ;
@@ -45,16 +59,23 @@ export class WinPeripheral extends Peripheral {
4559 public async disconnect ( ) : Promise < void > {
4660 this . _state = 'disconnecting' ;
4761
48- this . adapter . noble . disconnect ( this . uuid ) ;
62+ await new Promise < void > ( ( res ) => {
63+ const resolve = ( ) => {
64+ clearTimeout ( timer ) ;
65+ this . adapter . noble . off ( 'connect' , disconnHandler ) ;
66+ res ( ) ;
67+ } ;
68+
69+ const timer = setTimeout ( resolve , 10000 ) ;
4970
50- await new Promise < void > ( ( resolve ) => {
5171 const disconnHandler = ( uuid : string ) => {
5272 if ( uuid === this . uuid ) {
53- this . adapter . noble . off ( 'disconnect' , disconnHandler ) ;
5473 resolve ( ) ;
5574 }
5675 } ;
5776 this . adapter . noble . on ( 'disconnect' , disconnHandler ) ;
77+
78+ this . adapter . noble . disconnect ( this . uuid ) ;
5879 } ) ;
5980
6081 this . _state = 'disconnected' ;
0 commit comments