Skip to content

Commit fb17c69

Browse files
committed
fix: ble connection
1 parent ef27983 commit fb17c69

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/bluetooth.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{state::GlobalState, structs::BleDisplayDevice};
22
use alloc::string::ToString;
33
use core::cell::RefCell;
4-
use embassy_futures::select::select3;
4+
use embassy_futures::select::{Either, select, select3, select4};
55
use embassy_sync::{
66
blocking_mutex::raw::NoopRawMutex,
77
channel::{Channel, Sender},
@@ -70,10 +70,15 @@ pub async fn bluetooth_timer_task(
7070
.clear();
7171
}
7272

73-
let config = ScanConfig::default();
74-
let mut _session = scanner.scan(&config).await.unwrap();
75-
let _ = select3(
73+
let _ = select4(
7674
runner.run_with_handler(&printer),
75+
async {
76+
let config = ScanConfig::default();
77+
let mut _session = scanner.scan(&config).await.unwrap();
78+
loop {
79+
Timer::after_millis(10000).await;
80+
}
81+
},
7782
async {
7883
loop {
7984
let recv = discovery_channel.receive().await;
@@ -94,23 +99,22 @@ pub async fn bluetooth_timer_task(
9499
loop {
95100
Timer::after_millis(200).await;
96101
if state.ble_connect_sig.signaled() {
97-
return;
102+
break;
98103
}
99104
}
100105
},
101106
)
102107
.await;
103108

104-
drop(_session);
105109
scanner.into_inner()
106110
} else {
107111
central
108112
};
109113

110-
let display_addr = bond_info
111-
.clone()
112-
.map(|x| x.identity.bd_addr.into_inner())
113-
.unwrap_or(state.ble_connect_sig.wait().await.addr);
114+
let display_addr = match bond_info {
115+
Some(ref bond_info) => bond_info.identity.bd_addr.into_inner(),
116+
None => state.ble_connect_sig.wait().await.addr,
117+
};
114118
let target: Address = Address::random(display_addr);
115119

116120
let config = ConnectConfig {
@@ -159,12 +163,17 @@ pub async fn bluetooth_timer_task(
159163
bond,
160164
} => {
161165
log::info!("Pairing complete: {:?}", security_level);
166+
162167
if let Some(bond) = bond {
163168
store_bonding_info(&state.nvs, &bond).await;
164169
has_bond_info = true;
165-
} else {
170+
}
171+
172+
if !security_level.encrypted() {
173+
_ = state.nvs.invalidate_key(b"BONDING_KEY").await;
166174
break 'outer;
167175
}
176+
168177
break;
169178
}
170179
ConnectionEvent::PairingFailed(err) => {
@@ -196,8 +205,6 @@ pub async fn bluetooth_timer_task(
196205
.await
197206
.unwrap();
198207

199-
use embassy_futures::select::{Either, select};
200-
201208
let conn_fut = async {
202209
loop {
203210
if let ConnectionEvent::Disconnected { reason } = conn.next().await {
@@ -260,7 +267,6 @@ pub async fn bluetooth_timer_task(
260267
};
261268

262269
let gatt_and_conn_events = select(conn_fut, write_fut);
263-
264270
match select(client.task(), gatt_and_conn_events).await {
265271
Either::Second(Either::First(_)) => {
266272
log::info!("Connection event loop finished (disconnected)");

0 commit comments

Comments
 (0)