pub struct Handler { /* private fields */ }
Implementations§
Source§impl Handler
impl Handler
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true if a device is connected
Sourcepub async fn is_scanning(&self) -> bool
pub async fn is_scanning(&self) -> bool
Returns true if the adapter is scanning
Sourcepub async fn set_scanning_update_channel(&self, tx: Sender<bool>)
pub async fn set_scanning_update_channel(&self, tx: Sender<bool>)
Takes a sender that will be used to send changes in the scanning status
§Example
use tauri::async_runtime;
use tokio::sync::mpsc;
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let (tx, mut rx) = mpsc::channel(1);
handler.set_scanning_update_channel(tx).await;
while let Some(scanning) = rx.recv().await {
println!("Scanning: {scanning}");
}
});
Sourcepub async fn set_connection_update_channel(&self, tx: Sender<bool>)
pub async fn set_connection_update_channel(&self, tx: Sender<bool>)
Takes a sender that will be used to send changes in the connection status
§Example
use tauri::async_runtime;
use tokio::sync::mpsc;
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let (tx, mut rx) = mpsc::channel(1);
handler.set_connection_update_channel(tx).await;
while let Some(connected) = rx.recv().await {
println!("Connected: {connected}");
}
});
Sourcepub async fn connect(
&self,
address: &str,
on_disconnect: Option<Box<dyn Fn() + Send>>,
) -> Result<(), Error>
pub async fn connect( &self, address: &str, on_disconnect: Option<Box<dyn Fn() + Send>>, ) -> Result<(), Error>
Connects to the given address If a callback is provided, it will be called when the device is disconnected. Because connecting sometimes fails especially on android, this method tries up to 3 times before returning an error
§Errors
Returns an error if no devices are found, if the device is already connected, if the connection fails, or if the service/characteristics discovery fails
§Example
use tauri::async_runtime;
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
handler.connect("00:00:00:00:00:00", Some(Box::new(|| println!("disconnected")))).await.unwrap();
});
Sourcepub async fn disconnect(&self) -> Result<(), Error>
pub async fn disconnect(&self) -> Result<(), Error>
Sourcepub async fn discover(
&self,
tx: Option<Sender<Vec<BleDevice>>>,
timeout: u64,
filter: ScanFilter,
) -> Result<(), Error>
pub async fn discover( &self, tx: Option<Sender<Vec<BleDevice>>>, timeout: u64, filter: ScanFilter, ) -> Result<(), Error>
Scans for timeout
milliseconds and periodically sends discovered devices
to the given channel.
A task is spawned to handle the scan and send the devices, so the function
returns immediately.
A Variant of ScanFilter
can be provided to filter the discovered devices
§Errors
Returns an error if starting the scan fails
§Panics
Panics if there is an error getting devices from the adapter
§Example
use tauri::async_runtime;
use tokio::sync::mpsc;
use tauri_plugin_blec::models::ScanFilter;
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let (tx, mut rx) = mpsc::channel(1);
handler.discover(Some(tx),1000, ScanFilter::None).await.unwrap();
while let Some(devices) = rx.recv().await {
println!("Discovered {devices:?}");
}
});
Sourcepub async fn discover_services(
&self,
address: &str,
) -> Result<Vec<Service>, Error>
pub async fn discover_services( &self, address: &str, ) -> Result<Vec<Service>, Error>
Discover provided services and charecteristics If the device is not connected, a connection is made in order to discover the services and characteristics After the discovery is done, the device is disconnected If the devices was already connected, it will stay connected
§Errors
Returns an error if the device is not found, if the connection fails, or if the discovery fails
§Panics
Panics if there is an error with the internal disconnect event
Sourcepub async fn send_data(
&self,
c: Uuid,
data: &[u8],
write_type: WriteType,
) -> Result<(), Error>
pub async fn send_data( &self, c: Uuid, data: &[u8], write_type: WriteType, ) -> Result<(), Error>
Sends data to the given characteristic of the connected device
§Errors
Returns an error if no device is connected or the characteristic is not available or if the write operation fails
§Example
use tauri::async_runtime;
use uuid::{Uuid,uuid};
use tauri_plugin_blec::models::WriteType;
const CHARACTERISTIC_UUID: Uuid = uuid!("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B");
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let data = [1,2,3,4,5];
let response = handler.send_data(CHARACTERISTIC_UUID,&data, WriteType::WithResponse).await.unwrap();
});
Sourcepub async fn recv_data(&self, c: Uuid) -> Result<Vec<u8>, Error>
pub async fn recv_data(&self, c: Uuid) -> Result<Vec<u8>, Error>
Receives data from the given characteristic of the connected device Returns the data as a vector of bytes
§Errors
Returns an error if no device is connected or the characteristic is not available or if the read operation fails
§Example
use tauri::async_runtime;
use uuid::{Uuid,uuid};
const CHARACTERISTIC_UUID: Uuid = uuid!("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B");
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let response = handler.recv_data(CHARACTERISTIC_UUID).await.unwrap();
});
Sourcepub async fn subscribe(
&self,
c: Uuid,
callback: impl Fn(&[u8]) + Send + Sync + 'static,
) -> Result<(), Error>
pub async fn subscribe( &self, c: Uuid, callback: impl Fn(&[u8]) + Send + Sync + 'static, ) -> Result<(), Error>
Subscribe to notifications from the given characteristic The callback will be called whenever a notification is received
§Errors
Returns an error if no device is connected or the characteristic is not available or if the subscribe operation fails
§Example
use tauri::async_runtime;
use uuid::{Uuid,uuid};
const CHARACTERISTIC_UUID: Uuid = uuid!("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B");
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let response = handler.subscribe(CHARACTERISTIC_UUID,|data| println!("received {data:?}")).await.unwrap();
});
Sourcepub async fn unsubscribe(&self, c: Uuid) -> Result<(), Error>
pub async fn unsubscribe(&self, c: Uuid) -> Result<(), Error>
Unsubscribe from notifications for the given characteristic This will also remove the callback from the list of listeners
§Errors
Returns an error if no device is connected or the characteristic is not available or if the unsubscribe operation fails