initial tool
This commit is contained in:
108
src/main.rs
Normal file
108
src/main.rs
Normal file
@ -0,0 +1,108 @@
|
||||
// See the "macOS permissions note" in README.md before running this on macOS
|
||||
// Big Sur or later.
|
||||
|
||||
use std::time::Duration;
|
||||
use tokio::time;
|
||||
|
||||
use btleplug::api::{BDAddr, Central, CharPropFlags, Manager as _, Peripheral, ScanFilter};
|
||||
use btleplug::platform::Manager;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let manager = Manager::new().await?;
|
||||
let adapter_list = manager.adapters().await?;
|
||||
if adapter_list.is_empty() {
|
||||
eprintln!("No Bluetooth adapters found");
|
||||
}
|
||||
let command = std::env::args().nth(1).expect("help");
|
||||
let param = std::env::args().nth(2).unwrap_or(String::from("aa:bb:cc:dd:ee:ff"));
|
||||
let mut help = false;
|
||||
let mut scan = false;
|
||||
let mut device = false;
|
||||
|
||||
match command.as_str() {
|
||||
"help" => {help = true},
|
||||
"scan" => {scan = true },
|
||||
"device" => {device = true},
|
||||
_ => { help = true },
|
||||
|
||||
}
|
||||
if help {
|
||||
print!("Help!!!");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for adapter in adapter_list.iter() {
|
||||
println!("Starting scan on {}...", adapter.adapter_info().await?);
|
||||
adapter
|
||||
.start_scan(ScanFilter::default())
|
||||
.await
|
||||
.expect("Can't scan BLE adapter for connected devices...");
|
||||
time::sleep(Duration::from_secs(10)).await;
|
||||
let peripherals = adapter.peripherals().await?;
|
||||
if peripherals.is_empty() {
|
||||
eprintln!("->>> BLE peripheral devices were not found, sorry. Exiting...");
|
||||
} else {
|
||||
// All peripheral devices in range
|
||||
for peripheral in peripherals.iter() {
|
||||
let properties = peripheral.properties().await?.unwrap();
|
||||
let is_connected = peripheral.is_connected().await?;
|
||||
let mac_addr = properties
|
||||
.address;
|
||||
let local_name = properties
|
||||
.local_name
|
||||
.unwrap_or(String::from("No Advertised Name"));
|
||||
if scan {
|
||||
println!(
|
||||
"Peripheral {:?} ({:?})",
|
||||
local_name, mac_addr
|
||||
);
|
||||
}
|
||||
if device && mac_addr.eq(&BDAddr::from_str_delim(¶m).unwrap()) {
|
||||
println!("Advertised Service Data from peripheral {:?}", &local_name);
|
||||
for (svc_uuid, data) in properties.service_data.iter() {
|
||||
println!("Service UUID {} has data {:?}",svc_uuid,data);
|
||||
}
|
||||
if !is_connected {
|
||||
println!("Connecting to peripheral {:?}...", &local_name);
|
||||
if let Err(err) = peripheral.connect().await {
|
||||
eprintln!("Error connecting to peripheral, skipping: {}", err);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let is_connected = peripheral.is_connected().await?;
|
||||
println!(
|
||||
"Now connected ({:?}) to peripheral {:?}...",
|
||||
is_connected, &local_name
|
||||
);
|
||||
|
||||
peripheral.discover_services().await?;
|
||||
println!("Discover peripheral {:?} services...", &local_name);
|
||||
for service in peripheral.services() {
|
||||
println!(
|
||||
"Service UUID {}, primary: {}",
|
||||
service.uuid, service.primary
|
||||
);
|
||||
for characteristic in service.characteristics {
|
||||
println!(" Characteristic {:?} has {:?}", characteristic.uuid,characteristic.properties);
|
||||
if characteristic.properties.contains(CharPropFlags::READ) {
|
||||
let data = peripheral.read(&characteristic).await?;
|
||||
println!(" and data: {:?}",data)
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_connected {
|
||||
println!("Disconnecting from peripheral {:?}...", &local_name);
|
||||
peripheral
|
||||
.disconnect()
|
||||
.await
|
||||
.expect("Error disconnecting from BLE peripheral");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user