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 async fn is_scanning(&self) -> bool

Returns true if the adapter is scanning

Source

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

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

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

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

Disconnects from the connected device This triggers a disconnect and then waits for the actual disconnect event from the adapter

§Errors

Returns an error if no device is connected or if the disconnect fails

§Panics

panics if there is an error with handling the internal disconnect event

Source

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

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

Source

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

Stops scanning for devices

§Errors

Returns an error if stopping the scan fails

Source

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

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

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

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

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