Skip to content

Commit af2c79c

Browse files
committed
Add --keepalive argument
to maintain BLE adapter connection after successfully launching Android Auto
1 parent 38a633c commit af2c79c

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ OPTIONS:
155155
-i, --iface <IFACE>
156156
WLAN / Wi-Fi Hotspot interface [default: wlan0]
157157
158+
-k, --keepalive
159+
Keep alive mode: BLE adapter doesn't turn off after successful connection, so that the
160+
phone can remain connected (used in special configurations)
161+
158162
-l, --legacy
159163
Enable legacy mode
160164

src/bluetooth.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct BluetoothState {
5555
handle_aa: ProfileHandle,
5656
handle_hsp: JoinHandle<Result<ProfileHandle>>,
5757
handle_agent: AgentHandle,
58+
keepalive: bool,
5859
}
5960

6061
pub async fn get_cpu_serial_number_suffix() -> Result<String> {
@@ -71,6 +72,7 @@ async fn power_up_and_wait_for_connection(
7172
advertise: bool,
7273
btalias: Option<String>,
7374
connect: Option<Address>,
75+
keepalive: bool,
7476
) -> Result<(BluetoothState, Stream)> {
7577
// setting BT alias for further use
7678
let alias = match btalias {
@@ -220,6 +222,7 @@ async fn power_up_and_wait_for_connection(
220222
handle_aa,
221223
handle_hsp: task_hsp,
222224
handle_agent,
225+
keepalive,
223226
};
224227

225228
Ok((state, stream))
@@ -328,8 +331,12 @@ pub async fn bluetooth_stop(state: BluetoothState) -> Result<()> {
328331
}
329332
}
330333

331-
state.adapter.set_powered(false).await?;
332-
info!("{} 💤 Bluetooth adapter powered off", NAME);
334+
if state.keepalive {
335+
info!("{} 💤 Bluetooth adapter stays on", NAME);
336+
} else {
337+
state.adapter.set_powered(false).await?;
338+
info!("{} 💤 Bluetooth adapter powered off", NAME);
339+
}
333340

334341
Ok(())
335342
}
@@ -340,13 +347,14 @@ pub async fn bluetooth_setup_connection(
340347
connect: Option<Address>,
341348
wifi_config: WifiConfig,
342349
tcp_start: Arc<Notify>,
350+
keepalive: bool,
343351
) -> Result<BluetoothState> {
344352
use WifiInfoResponse::WifiInfoResponse;
345353
use WifiStartRequest::WifiStartRequest;
346354
let mut stage = 1;
347355
let mut started;
348356

349-
let (state, mut stream) = power_up_and_wait_for_connection(advertise, btalias, connect).await?;
357+
let (state, mut stream) = power_up_and_wait_for_connection(advertise, btalias, connect, keepalive).await?;
350358

351359
info!("{} 📲 Sending parameters via bluetooth to phone...", NAME);
352360
let mut start_req = WifiStartRequest::new();

src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ struct Args {
7575
/// BLE device name
7676
#[clap(short, long)]
7777
btalias: Option<String>,
78+
79+
/**
80+
Keep alive mode: BLE adapter doesn't turn off after successful connection,
81+
so that the phone can remain connected (used in special configurations)
82+
*/
83+
#[clap(short, long)]
84+
keepalive: bool,
7885
}
7986

8087
#[derive(Clone)]
@@ -179,6 +186,7 @@ async fn tokio_main(
179186
hostapd_conf: PathBuf,
180187
connect: Option<Address>,
181188
udc: Option<String>,
189+
keepalive: bool,
182190
need_restart: Arc<Notify>,
183191
tcp_start: Arc<Notify>,
184192
) {
@@ -205,6 +213,7 @@ async fn tokio_main(
205213
connect,
206214
wifi_conf.clone(),
207215
tcp_start.clone(),
216+
keepalive,
208217
)
209218
.await
210219
{
@@ -289,6 +298,7 @@ fn main() {
289298
args.hostapd_conf,
290299
args.connect,
291300
args.udc,
301+
args.keepalive,
292302
need_restart,
293303
tcp_start,
294304
)

0 commit comments

Comments
 (0)