Show a progress bar during scan
This commit is contained in:
parent
729dab35df
commit
d3db6a0885
59
Cargo.lock
generated
59
Cargo.lock
generated
|
@ -190,6 +190,19 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.15.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3d79fbe8970a77e3e34151cc13d3b3e248aa0faaecb9f6091fa07ebefe5ad60"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"unicode-width",
|
||||
"windows-sys 0.42.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation"
|
||||
version = "0.9.3"
|
||||
|
@ -274,6 +287,12 @@ version = "1.8.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.2.8"
|
||||
|
@ -426,6 +445,19 @@ version = "0.3.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.17.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cef509aa9bc73864d6756f0d34d35504af3cf0844373afe9b8669a5b8005a729"
|
||||
dependencies = [
|
||||
"console",
|
||||
"number_prefix",
|
||||
"portable-atomic",
|
||||
"tokio",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "io-lifetimes"
|
||||
version = "1.0.5"
|
||||
|
@ -492,6 +524,12 @@ dependencies = [
|
|||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.139"
|
||||
|
@ -538,6 +576,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"btleplug",
|
||||
"clap",
|
||||
"indicatif",
|
||||
"tokio",
|
||||
"uuid",
|
||||
]
|
||||
|
@ -579,6 +618,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "number_prefix"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
||||
|
||||
[[package]]
|
||||
name = "objc"
|
||||
version = "0.2.7"
|
||||
|
@ -631,6 +676,12 @@ version = "0.3.26"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "0.3.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26f6a7b87c2e435a3241addceeeff740ff8b7e76b74c13bf9acb17fa454ea00b"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
|
@ -827,7 +878,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"bytes",
|
||||
"libc",
|
||||
"memchr",
|
||||
"mio",
|
||||
"num_cpus",
|
||||
"pin-project-lite",
|
||||
|
@ -878,6 +931,12 @@ version = "1.0.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.3.0"
|
||||
|
|
|
@ -9,5 +9,6 @@ edition = "2021"
|
|||
[dependencies]
|
||||
btleplug = "0.10.4"
|
||||
clap = { version = "4.1.6", features = ["derive"] }
|
||||
indicatif = { version = "0.17.3", features = ["tokio"] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
|
||||
uuid = "1.3.0"
|
||||
|
|
11
src/ble.rs
11
src/ble.rs
|
@ -1,14 +1,17 @@
|
|||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
use tokio::time;
|
||||
use uuid::Uuid;
|
||||
|
||||
use btleplug::api::{Central, Manager as _, Peripheral, ScanFilter};
|
||||
use btleplug::platform::{Adapter, Manager};
|
||||
use tokio::time;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::utils::progress_bar;
|
||||
|
||||
const NORDIC_UART_SERVICE_UUID: Uuid = Uuid::from_u128(0x6e400001_b5a3_f393_e0a9_e50e24dcca9e);
|
||||
|
||||
pub async fn scan(adapter_name: String, scan_time: u64) -> Result<(), Box<dyn Error>> {
|
||||
let scan_time = Duration::from_secs(scan_time);
|
||||
let manager = Manager::new().await?;
|
||||
let adapter = get_adapter_by_name(&manager, adapter_name).await?;
|
||||
adapter
|
||||
|
@ -16,7 +19,8 @@ pub async fn scan(adapter_name: String, scan_time: u64) -> Result<(), Box<dyn Er
|
|||
.await
|
||||
.expect("An error occurred while scanning for devices");
|
||||
|
||||
time::sleep(Duration::from_secs(scan_time)).await;
|
||||
progress_bar(scan_time);
|
||||
time::sleep(scan_time).await;
|
||||
|
||||
let peripherals = adapter.peripherals().await?;
|
||||
if peripherals.is_empty() {
|
||||
|
@ -31,6 +35,7 @@ pub async fn scan(adapter_name: String, scan_time: u64) -> Result<(), Box<dyn Er
|
|||
if !services.contains(&NORDIC_UART_SERVICE_UUID) {
|
||||
continue;
|
||||
}
|
||||
// TODO: put in a vec and sort by rssi.
|
||||
let address = properties
|
||||
.as_ref()
|
||||
.ok_or_else(|| "Error reading device address".to_string())?
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod ble;
|
||||
mod utils;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::error::Error;
|
||||
|
|
35
src/utils.rs
Normal file
35
src/utils.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use tokio::runtime;
|
||||
use tokio::time::interval;
|
||||
|
||||
// Show a progress bar that will be full in `scan_time` seconds.
|
||||
pub fn progress_bar(scan_time: Duration) {
|
||||
let steps = (scan_time.as_millis() / 5) as u64;
|
||||
let pb = ProgressBar::new(steps);
|
||||
let spinner_style = ProgressStyle::with_template("{spinner} [{wide_bar}]")
|
||||
.unwrap()
|
||||
.progress_chars("#>-");
|
||||
pb.set_style(spinner_style.clone());
|
||||
|
||||
let rt = runtime::Builder::new_multi_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.expect("failed to create runtime");
|
||||
|
||||
let future = async move {
|
||||
pb.set_message("Scanning...");
|
||||
let mut intv = interval(Duration::from_millis(5));
|
||||
|
||||
for _ in 0..steps {
|
||||
intv.tick().await;
|
||||
pb.inc(1);
|
||||
}
|
||||
pb.finish_with_message("Done");
|
||||
};
|
||||
thread::spawn(move || {
|
||||
rt.block_on(future);
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue