Membuat program android untuk menghubungkan tauri dengan perangkat bluetooth classic #1

Merged
rizky_denianto merged 3 commits from pengembangan into main 2026-04-14 19:05:15 +07:00
6 changed files with 81 additions and 55 deletions
Showing only changes of commit 9e30bf0a16 - Show all commits
+1 -1
View File
@@ -1,4 +1,4 @@
package com.plugin.bluetooth_classic package com.plugin.bluclas
import android.Manifest import android.Manifest
import android.app.Activity import android.app.Activity
+13 -6
View File
@@ -1,13 +1,20 @@
use tauri::{AppHandle, command, Runtime}; use tauri::{command, AppHandle, Runtime};
use crate::models::*; use crate::models::*;
use crate::Result;
use crate::BluclasExt; use crate::BluclasExt;
use crate::Result;
#[command] #[command]
pub(crate) async fn ping<R: Runtime>( pub(crate) async fn daftar_perangkat<R: Runtime>(
app: AppHandle<R>, app: AppHandle<R>,
payload: PingRequest, ) -> Result<Vec<SBluetoothClassicResponPerangkat>> {
) -> Result<PingResponse> { return app.bluclas().daftar_perangkat();
app.bluclas().ping(payload) }
#[command]
pub(crate) async fn kirim<R: Runtime>(
app: AppHandle<R>,
payload: SBluetoothClassicMuatanKirim,
) -> Result<()> {
return app.bluclas().kirim(payload);
} }
+15 -8
View File
@@ -4,19 +4,26 @@ use tauri::{plugin::PluginApi, AppHandle, Runtime};
use crate::models::*; use crate::models::*;
pub fn init<R: Runtime, C: DeserializeOwned>( pub fn init<R: Runtime, C: DeserializeOwned>(
app: &AppHandle<R>, app: &AppHandle<R>,
_api: PluginApi<R, C>, _api: PluginApi<R, C>,
) -> crate::Result<Bluclas<R>> { ) -> crate::Result<Bluclas<R>> {
Ok(Bluclas(app.clone())) Ok(Bluclas(app.clone()))
} }
/// Access to the bluclas APIs. /// Access to the bluclas APIs.
pub struct Bluclas<R: Runtime>(AppHandle<R>); pub struct Bluclas<R: Runtime>(AppHandle<R>);
impl<R: Runtime> Bluclas<R> { impl<R: Runtime> Bluclas<R> {
pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> { pub fn daftar_perangkat(&self) -> crate::Result<Vec<SBluetoothClassicResponPerangkat>> {
Ok(PingResponse { let daftar_res: Vec<SBluetoothClassicResponPerangkat> =
value: payload.value, vec![SBluetoothClassicResponPerangkat {
}) nama: String::new(),
} alamat: String::new(),
}];
return Ok(daftar_res);
}
pub fn kirim(&self, _: SBluetoothClassicMuatanKirim) -> crate::Result<()> {
return Ok(());
}
} }
+20 -17
View File
@@ -1,6 +1,6 @@
use tauri::{ use tauri::{
plugin::{Builder, TauriPlugin}, plugin::{Builder, TauriPlugin},
Manager, Runtime, Manager, Runtime,
}; };
pub use models::*; pub use models::*;
@@ -23,26 +23,29 @@ use mobile::Bluclas;
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the bluclas APIs. /// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the bluclas APIs.
pub trait BluclasExt<R: Runtime> { pub trait BluclasExt<R: Runtime> {
fn bluclas(&self) -> &Bluclas<R>; fn bluclas(&self) -> &Bluclas<R>;
} }
impl<R: Runtime, T: Manager<R>> crate::BluclasExt<R> for T { impl<R: Runtime, T: Manager<R>> crate::BluclasExt<R> for T {
fn bluclas(&self) -> &Bluclas<R> { fn bluclas(&self) -> &Bluclas<R> {
self.state::<Bluclas<R>>().inner() self.state::<Bluclas<R>>().inner()
} }
} }
/// Initializes the plugin. /// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> { pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("bluclas") Builder::new("bluclas")
.invoke_handler(tauri::generate_handler![commands::ping]) .invoke_handler(tauri::generate_handler![
.setup(|app, api| { commands::daftar_perangkat,
#[cfg(mobile)] commands::kirim
let bluclas = mobile::init(app, api)?; ])
#[cfg(desktop)] .setup(|app, api| {
let bluclas = desktop::init(app, api)?; #[cfg(mobile)]
app.manage(bluclas); let bluclas = mobile::init(app, api)?;
Ok(()) #[cfg(desktop)]
}) let bluclas = desktop::init(app, api)?;
.build() app.manage(bluclas);
Ok(())
})
.build()
} }
+22 -15
View File
@@ -1,7 +1,7 @@
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use tauri::{ use tauri::{
plugin::{PluginApi, PluginHandle}, plugin::{PluginApi, PluginHandle},
AppHandle, Runtime, AppHandle, Runtime,
}; };
use crate::models::*; use crate::models::*;
@@ -11,24 +11,31 @@ tauri::ios_plugin_binding!(init_plugin_bluclas);
// initializes the Kotlin or Swift plugin classes // initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>( pub fn init<R: Runtime, C: DeserializeOwned>(
_app: &AppHandle<R>, _app: &AppHandle<R>,
api: PluginApi<R, C>, api: PluginApi<R, C>,
) -> crate::Result<Bluclas<R>> { ) -> crate::Result<Bluclas<R>> {
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
let handle = api.register_android_plugin("", "ExamplePlugin")?; let handle = api.register_android_plugin("com.plugin.bluclas", "BluclasPlugin")?;
#[cfg(target_os = "ios")] #[cfg(target_os = "ios")]
let handle = api.register_ios_plugin(init_plugin_bluclas)?; let handle = api.register_ios_plugin(init_plugin_bluclas)?;
Ok(Bluclas(handle)) Ok(Bluclas(handle))
} }
/// Access to the bluclas APIs. /// Access to the bluclas APIs.
pub struct Bluclas<R: Runtime>(PluginHandle<R>); pub struct Bluclas<R: Runtime>(PluginHandle<R>);
impl<R: Runtime> Bluclas<R> { impl<R: Runtime> Bluclas<R> {
pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> { pub fn daftar_perangkat(&self) -> crate::Result<Vec<SBluetoothClassicResponPerangkat>> {
self return self
.0 .0
.run_mobile_plugin("ping", payload) .run_mobile_plugin("daftarPerangkat", ())
.map_err(Into::into) .map_err(Into::into);
} }
pub fn kirim(&self, payload: SBluetoothClassicMuatanKirim) -> crate::Result<()> {
return self
.0
.run_mobile_plugin("kirim", payload)
.map_err(Into::into);
}
} }
+10 -8
View File
@@ -1,13 +1,15 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PingRequest {
pub value: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)] #[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct PingResponse { pub struct SBluetoothClassicResponPerangkat {
pub value: Option<String>, pub nama: String,
pub alamat: String,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SBluetoothClassicMuatanKirim {
pub alamat: String,
pub data: Vec<u8>,
} }