Skip to content

Commit 97a63bc

Browse files
committed
Add --btalias argument
Allow the user to rename the BLE advertised name of the host system
1 parent 34f3c09 commit 97a63bc

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ USAGE:
123123
124124
OPTIONS:
125125
-a, --advertise BLE advertising
126+
-b, --btalias Specify the BLE name of the device
126127
-c, --connect <CONNECT> Auto-connect to saved phone or specified phone MAC address if
127128
provided
128129
-d, --debug Enable debug info

src/bluetooth.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,23 @@ pub async fn get_cpu_serial_number_suffix() -> Result<String> {
7474

7575
async fn power_up_and_wait_for_connection(
7676
advertise: bool,
77+
btalias: Option<String>,
7778
connect: Option<Address>,
7879
) -> Result<(BluetoothState, Stream)> {
7980
// setting BT alias for further use
80-
let alias = match get_cpu_serial_number_suffix().await {
81-
Ok(suffix) => format!("{}-{}", BT_ALIAS, suffix),
82-
Err(_) => String::from(BT_ALIAS),
83-
};
81+
let alias: String;
82+
83+
match btalias {
84+
None => {
85+
alias = match get_cpu_serial_number_suffix().await {
86+
Ok(suffix) => format!("{}-{}", BT_ALIAS, suffix),
87+
Err(_) => String::from(BT_ALIAS),
88+
};
89+
}
90+
Some(btalias) => {
91+
alias = btalias;
92+
}
93+
}
8494
info!("{} 🥏 Bluetooth alias: <bold><green>{}</>", NAME, alias);
8595

8696
let session = bluer::Session::new().await?;
@@ -337,6 +347,7 @@ pub async fn bluetooth_stop(state: BluetoothState) -> Result<()> {
337347

338348
pub async fn bluetooth_setup_connection(
339349
advertise: bool,
350+
btalias: Option<String>,
340351
iface: &str,
341352
connect: Option<Address>,
342353
tcp_start: Arc<Notify>,
@@ -348,7 +359,7 @@ pub async fn bluetooth_setup_connection(
348359

349360
let mut wlan_ip_addr = String::from(DEFAULT_WLAN_ADDR);
350361

351-
let (state, mut stream) = power_up_and_wait_for_connection(advertise, connect).await?;
362+
let (state, mut stream) = power_up_and_wait_for_connection(advertise, btalias, connect).await?;
352363

353364

354365
// Get UP interface and IP

src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ struct Args {
7272
default_value = "wlan0"
7373
)]
7474
iface: String,
75+
76+
/// BLE device name
77+
#[clap(
78+
short,
79+
long
80+
)]
81+
btalias: Option<String>,
7582

7683
}
7784

@@ -120,6 +127,7 @@ fn logging_init(debug: bool, log_path: &PathBuf) {
120127

121128
async fn tokio_main(
122129
advertise: bool,
130+
btalias: Option<String>,
123131
legacy: bool,
124132
iface: String,
125133
connect: Option<Address>,
@@ -143,7 +151,7 @@ async fn tokio_main(
143151

144152
let bt_stop;
145153
loop {
146-
match bluetooth_setup_connection(advertise, &iface, connect, tcp_start.clone()).await {
154+
match bluetooth_setup_connection(advertise, btalias.clone(), &iface, connect, tcp_start.clone()).await {
147155
Ok(state) => {
148156
// we're ready, gracefully shutdown bluetooth in task
149157
bt_stop = tokio::spawn(async move { bluetooth_stop(state).await });
@@ -219,6 +227,7 @@ fn main() {
219227
runtime.spawn(async move {
220228
tokio_main(
221229
args.advertise,
230+
args.btalias,
222231
args.legacy,
223232
args.iface,
224233
args.connect,

0 commit comments

Comments
 (0)