@@ -21,12 +21,24 @@ export class WinPeripheral extends Peripheral {
21
21
public async connect ( ) : Promise < GattRemote > {
22
22
this . _state = 'connecting' ;
23
23
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 ) ;
25
39
26
- await new Promise < void > ( ( resolve , reject ) => {
27
40
const connHandler = ( uuid : string , err : Error ) => {
28
41
if ( uuid === this . uuid ) {
29
- this . adapter . noble . off ( 'connect' , connHandler ) ;
30
42
if ( err ) {
31
43
reject ( err ) ;
32
44
} else {
@@ -35,6 +47,8 @@ export class WinPeripheral extends Peripheral {
35
47
}
36
48
} ;
37
49
this . adapter . noble . on ( 'connect' , connHandler ) ;
50
+
51
+ this . adapter . noble . connect ( this . uuid ) ;
38
52
} ) ;
39
53
40
54
this . _gatt = new WinGatt ( this ) ;
@@ -45,16 +59,23 @@ export class WinPeripheral extends Peripheral {
45
59
public async disconnect ( ) : Promise < void > {
46
60
this . _state = 'disconnecting' ;
47
61
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 ) ;
49
70
50
- await new Promise < void > ( ( resolve ) => {
51
71
const disconnHandler = ( uuid : string ) => {
52
72
if ( uuid === this . uuid ) {
53
- this . adapter . noble . off ( 'disconnect' , disconnHandler ) ;
54
73
resolve ( ) ;
55
74
}
56
75
} ;
57
76
this . adapter . noble . on ( 'disconnect' , disconnHandler ) ;
77
+
78
+ this . adapter . noble . disconnect ( this . uuid ) ;
58
79
} ) ;
59
80
60
81
this . _state = 'disconnected' ;
0 commit comments