tauri_plugin_blec

Struct Handler

Source
pub struct Handler { /* private fields */ }

Implementations§

Source§

impl Handler

Source

pub fn is_connected(&self) -> bool

Returns true if a device is connected

Source

pub fn is_scanning(&self) -> bool

Returns true if the adapter is scanning

Source

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}");
    }
});
Source

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}");
    }
});
Source

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();
});
Source

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

Source

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:?}");
    }
});
Source

pub async fn stop_scan(&mut self) -> Result<(), Error>

Stops scanning for devices

§Errors

Returns an error if stopping the scan fails

Source

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();
});
Source

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();
});
Source

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();
});
Source

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

Source

pub async fn connected_device(&self) -> Result<BleDevice, Error>

Returns the connected device

§Errors

Returns an error if no device is connected

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T