tauri_plugin_bluetooth_manager/
lib.rs

1use tauri::{
2    async_runtime,
3    plugin::{Builder, TauriPlugin},
4    Manager, Runtime,
5};
6
7pub use models::*;
8
9mod commands;
10mod desktop;
11mod error;
12mod models;
13
14pub use error::{Error, Result};
15
16use desktop::BluetoothManager;
17
18pub trait BluetoothManagerExt<R: Runtime> {
19    fn bluetooth_manager(&self) -> &BluetoothManager;
20}
21
22impl<R: Runtime, T: Manager<R>> BluetoothManagerExt<R> for T {
23    fn bluetooth_manager(&self) -> &BluetoothManager {
24        self.state::<BluetoothManager>().inner()
25    }
26}
27
28/// Initializes the plugin.
29pub fn init<R: Runtime>() -> TauriPlugin<R> {
30    Builder::<R>::new("bluetooth-manager")
31        .invoke_handler(tauri::generate_handler![
32            commands::list_adapters,
33            commands::set_adapter_powered,
34            commands::get_adapter_state,
35            commands::start_scan,
36            commands::stop_scan,
37            commands::list_devices,
38            commands::list_paired_devices,
39            commands::connect_device,
40            commands::disconnect_device,
41            commands::get_device_info,
42            commands::bluetooth_plugin_status,
43        ])
44        .setup(|app_handle, api| {
45            let result = async_runtime::block_on(desktop::init(app_handle.clone(), api));
46            let initialized = result.is_ok();
47            if let Some(manager) = app_handle.try_state::<desktop::BluetoothManager>() {
48                let mut guard = manager.inner().initialized.lock().unwrap();
49                *guard = initialized;
50            }
51            if let Err(e) = result {
52                eprintln!("Bluetooth service not available: {e}");
53            }
54            Ok(())
55        })
56        .build()
57}