@@ -5,7 +5,7 @@ use futures::StreamExt;
55use std:: time:: Duration ;
66use uuid:: Uuid ;
77
8- use bluest:: { Adapter , DeviceId } ;
8+ use bluest:: { Adapter , ConnectionEvent , DeviceId } ;
99
1010use tauri:: { command, AppHandle , State } ;
1111
@@ -46,22 +46,38 @@ pub async fn gatt_connect(
4646
4747 if let Some ( c) = char {
4848 let c2 = c. clone ( ) ;
49- tauri:: async_runtime:: spawn ( async move {
49+ let ah1 = app_handle. clone ( ) ;
50+ let notify_handle = tauri:: async_runtime:: spawn ( async move {
5051 if let Ok ( mut n) = c2. notify ( ) . await {
51- // Need to keep adapter from being dropped while active/connected
52- let a = adapter;
53-
5452 use tauri:: Manager ;
5553
5654 while let Some ( Ok ( vn) ) = n. next ( ) . await {
57- app_handle . emit ( "connection_data" , vn. clone ( ) ) ;
55+ ah1 . emit ( "connection_data" , vn. clone ( ) ) ;
5856 }
57+ }
58+ } ) ;
59+
60+ let ah2 = app_handle. clone ( ) ;
61+ tauri:: async_runtime:: spawn ( async move {
62+ // Need to keep adapter from being dropped while active/connected
63+ let a = adapter;
5964
60- let state = app_handle. state :: < super :: commands:: ActiveConnection > ( ) ;
61- * state. conn . lock ( ) . await = None ;
65+ use tauri:: Manager ;
6266
63- app_handle. emit ( "connection_disconnected" , ( ) ) ;
64- }
67+ if let Ok ( mut events) = a. device_connection_events ( & d) . await {
68+ while let Some ( ev) = events. next ( ) . await {
69+ if ev == ConnectionEvent :: Disconnected {
70+ let state = ah2. state :: < super :: commands:: ActiveConnection > ( ) ;
71+ * state. conn . lock ( ) . await = None ;
72+
73+ if let Err ( e) = ah2. emit ( "connection_disconnected" , ( ) ) {
74+ println ! ( "ERROR RAISING! {:?}" , e) ;
75+ }
76+
77+ notify_handle. abort ( ) ;
78+ }
79+ }
80+ } ;
6581 } ) ;
6682
6783 let ( send, mut recv) = channel ( 5 ) ;
0 commit comments