From fff4094dffad28076b79e546708dbb8534db0b9d Mon Sep 17 00:00:00 2001 From: George G Date: Sat, 28 Dec 2024 02:38:51 +0200 Subject: [PATCH] Add --keepalive argument to maintain BLE adapter connection after successfully launching Android Auto --- README.md | 4 ++++ src/bluetooth.rs | 15 ++++++++++++--- src/main.rs | 8 ++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a9112df..81fd47a 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,10 @@ OPTIONS: -i, --iface WLAN / Wi-Fi Hotspot interface [default: wlan0] + -k, --keepalive + Keep alive mode: BLE adapter doesn't turn off after successful connection, so that the + phone can remain connected (used in special configurations) + -l, --legacy Enable legacy mode diff --git a/src/bluetooth.rs b/src/bluetooth.rs index c411c03..2a15515 100644 --- a/src/bluetooth.rs +++ b/src/bluetooth.rs @@ -55,6 +55,7 @@ pub struct BluetoothState { handle_aa: ProfileHandle, handle_hsp: JoinHandle>, handle_agent: AgentHandle, + keepalive: bool, } pub async fn get_cpu_serial_number_suffix() -> Result { @@ -71,6 +72,7 @@ async fn power_up_and_wait_for_connection( advertise: bool, btalias: Option, connect: Option
, + keepalive: bool, ) -> Result<(BluetoothState, Stream)> { // setting BT alias for further use let alias = match btalias { @@ -220,6 +222,7 @@ async fn power_up_and_wait_for_connection( handle_aa, handle_hsp: task_hsp, handle_agent, + keepalive, }; Ok((state, stream)) @@ -328,8 +331,12 @@ pub async fn bluetooth_stop(state: BluetoothState) -> Result<()> { } } - state.adapter.set_powered(false).await?; - info!("{} 💤 Bluetooth adapter powered off", NAME); + if state.keepalive { + info!("{} 💤 Bluetooth adapter stays on", NAME); + } else { + state.adapter.set_powered(false).await?; + info!("{} 💤 Bluetooth adapter powered off", NAME); + } Ok(()) } @@ -340,13 +347,15 @@ pub async fn bluetooth_setup_connection( connect: Option
, wifi_config: WifiConfig, tcp_start: Arc, + keepalive: bool, ) -> Result { use WifiInfoResponse::WifiInfoResponse; use WifiStartRequest::WifiStartRequest; let mut stage = 1; let mut started; - let (state, mut stream) = power_up_and_wait_for_connection(advertise, btalias, connect).await?; + let (state, mut stream) = + power_up_and_wait_for_connection(advertise, btalias, connect, keepalive).await?; info!("{} 📲 Sending parameters via bluetooth to phone...", NAME); let mut start_req = WifiStartRequest::new(); diff --git a/src/main.rs b/src/main.rs index 00c25d4..bd4a37f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,11 @@ struct Args { /// BLE device name #[clap(short, long)] btalias: Option, + + /// Keep alive mode: BLE adapter doesn't turn off after successful connection, + /// so that the phone can remain connected (used in special configurations) + #[clap(short, long)] + keepalive: bool, } #[derive(Clone)] @@ -179,6 +184,7 @@ async fn tokio_main( hostapd_conf: PathBuf, connect: Option
, udc: Option, + keepalive: bool, need_restart: Arc, tcp_start: Arc, ) { @@ -205,6 +211,7 @@ async fn tokio_main( connect, wifi_conf.clone(), tcp_start.clone(), + keepalive, ) .await { @@ -289,6 +296,7 @@ fn main() { args.hostapd_conf, args.connect, args.udc, + args.keepalive, need_restart, tcp_start, )