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 fn is_scanning(&self) -> bool
pub fn is_scanning(&self) -> bool
Returns true if the adapter is scanning
Sourcepub fn set_scanning_update_channel(&mut self, tx: Sender<bool>)
pub fn set_scanning_update_channel(&mut 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.lock().await.set_scanning_update_channel(tx);
while let Some(scanning) = rx.recv().await {
println!("Scanning: {scanning}");
}
});
Sourcepub fn set_connection_update_channel(&mut self, tx: Sender<bool>)
pub fn set_connection_update_channel(&mut 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.lock().await.set_connection_update_channel(tx);
while let Some(connected) = rx.recv().await {
println!("Connected: {connected}");
}
});
Sourcepub async fn connect(
&mut self,
address: String,
on_disconnect: Option<impl Fn() + Send + 'static>,
) -> Result<(), Error>
pub async fn connect( &mut self, address: String, on_disconnect: Option<impl Fn() + Send + 'static>, ) -> Result<(), Error>
Connects to the given address If a callback is provided, it will be called when the device is disconnected
§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.lock().await.connect("00:00:00:00:00:00".to_string(),Some(|| println!("disconnected"))).await.unwrap();
});
Sourcepub async fn disconnect(&mut self) -> Result<(), Error>
pub async fn disconnect(&mut self) -> Result<(), Error>
Disconnects from the connected device
§Errors
Returns an error if no device is connected or if the disconnect operation fails
Sourcepub async fn discover(
&mut self,
tx: Option<Sender<Vec<BleDevice>>>,
timeout: u64,
) -> Result<(), Error>
pub async fn discover( &mut self, tx: Option<Sender<Vec<BleDevice>>>, timeout: u64, ) -> 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.
§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;
async_runtime::block_on(async {
let handler = tauri_plugin_blec::get_handler().unwrap();
let (tx, mut rx) = mpsc::channel(1);
handler.lock().await.discover(Some(tx),1000).await.unwrap();
while let Some(devices) = rx.recv().await {
println!("Discovered {devices:?}");
}
});
Sourcepub async fn send_data(&mut self, c: Uuid, data: &[u8]) -> Result<(), Error>
pub async fn send_data(&mut self, c: Uuid, data: &[u8]) -> 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};
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.lock().await.send_data(CHARACTERISTIC_UUID,&data).await.unwrap();
});
Sourcepub async fn recv_data(&mut self, c: Uuid) -> Result<Vec<u8>, Error>
pub async fn recv_data(&mut 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.lock().await.recv_data(CHARACTERISTIC_UUID).await.unwrap();
});
Sourcepub async fn subscribe(
&mut self,
c: Uuid,
callback: impl Fn(&[u8]) + Send + Sync + 'static,
) -> Result<(), Error>
pub async fn subscribe( &mut 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.lock().await.subscribe(CHARACTERISTIC_UUID,|data| println!("received {data:?}")).await.unwrap();
});
Sourcepub async fn unsubscribe(&mut self, c: Uuid) -> Result<(), Error>
pub async fn unsubscribe(&mut 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