Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1afbd8817c | |||
| d16127348c | |||
| bb1fee0c68 | |||
| 00c3ba9b4c | |||
| 38c39ab8d7 | |||
| 25ce630e0d | |||
| 79b4fa56f6 | |||
| 681087d338 | |||
| 9e30bf0a16 |
+3
-3
@@ -1,10 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tauri-plugin-bluclas"
|
name = "tauri-plugin-bluclas"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = [ "You" ]
|
authors = [ "Rizky Denianto" ]
|
||||||
description = ""
|
description = ""
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
rust-version = "1.77.2"
|
rust-version = "1.94.1"
|
||||||
exclude = ["/examples", "/dist-js", "/guest-js", "/node_modules"]
|
exclude = ["/examples", "/dist-js", "/guest-js", "/node_modules"]
|
||||||
links = "tauri-plugin-bluclas"
|
links = "tauri-plugin-bluclas"
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -11,6 +11,7 @@ import app.tauri.annotation.InvokeArg
|
|||||||
import app.tauri.annotation.Permission
|
import app.tauri.annotation.Permission
|
||||||
import app.tauri.annotation.TauriPlugin
|
import app.tauri.annotation.TauriPlugin
|
||||||
import app.tauri.plugin.Invoke
|
import app.tauri.plugin.Invoke
|
||||||
|
import app.tauri.plugin.JSArray
|
||||||
import app.tauri.plugin.JSObject
|
import app.tauri.plugin.JSObject
|
||||||
import app.tauri.plugin.Plugin
|
import app.tauri.plugin.Plugin
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
@@ -31,8 +32,8 @@ class KirimArgs {
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
class BluetoothClassicPlugin(private val activity: Activity) : Plugin(activity) {
|
class BluclasPlugin(private val activity: Activity) : Plugin(activity) {
|
||||||
private val SPP_UUID: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
|
private val uuidSpp: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
|
||||||
|
|
||||||
private val adapter: BluetoothAdapter? = this.activity.getSystemService(BluetoothManager::class.java).getAdapter()
|
private val adapter: BluetoothAdapter? = this.activity.getSystemService(BluetoothManager::class.java).getAdapter()
|
||||||
private var soket: BluetoothSocket? = null
|
private var soket: BluetoothSocket? = null
|
||||||
@@ -45,67 +46,54 @@ class BluetoothClassicPlugin(private val activity: Activity) : Plugin(activity)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val daftar_res = adapter.bondedDevices.map { i ->
|
val daftarRes = JSArray()
|
||||||
mapOf(
|
adapter.bondedDevices.map { i ->
|
||||||
"nama" to (i.name ?: "Unknown"),
|
val res = JSObject()
|
||||||
"alamat" to i.address
|
res.put("nama", i.name ?: "Unknown")
|
||||||
)
|
res.put("alamat", i.address)
|
||||||
|
daftarRes.put(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
val objek_daftar_res = JSObject()
|
val objekDaftarRes = JSObject()
|
||||||
objek_daftar_res.put("data", daftar_res)
|
objekDaftarRes.put("data", daftarRes)
|
||||||
invoke.resolve(objek_daftar_res)
|
invoke.resolve(objekDaftarRes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun menghubungkanKoneksi(invoke: Invoke, alamat: String) {
|
fun menghubungkanKoneksi(alamat: String) {
|
||||||
try {
|
val perangkat: BluetoothDevice = adapter!!.getRemoteDevice(alamat)
|
||||||
val perangkat: BluetoothDevice = adapter!!.getRemoteDevice(alamat)
|
soket = perangkat.createInsecureRfcommSocketToServiceRecord(uuidSpp)
|
||||||
|
soket!!.connect()
|
||||||
soket = perangkat.createRfcommSocketToServiceRecord(SPP_UUID)
|
keluaran = soket!!.outputStream
|
||||||
soket!!.connect()
|
|
||||||
keluaran = soket!!.outputStream
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
invoke.reject("Koneksi dengan perangkat gagal: ${e.message}")
|
|
||||||
}
|
|
||||||
|
|
||||||
invoke.resolve()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun memutusKoneksi(invoke: Invoke) {
|
fun memutusKoneksi() {
|
||||||
try {
|
keluaran?.close()
|
||||||
keluaran?.close()
|
keluaran = null
|
||||||
keluaran = null
|
soket?.close()
|
||||||
soket?.close()
|
soket = null
|
||||||
soket = null
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
invoke.reject("Memutus koneksi perangkat gagal")
|
|
||||||
}
|
|
||||||
|
|
||||||
invoke.resolve()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command
|
@Command
|
||||||
fun kirim(invoke: Invoke) {
|
fun kirim(invoke: Invoke) {
|
||||||
val args = invoke.parseArgs(KirimArgs::class.java)
|
val args = invoke.parseArgs(KirimArgs::class.java)
|
||||||
|
|
||||||
menghubungkanKoneksi(invoke, args.alamat ?: "")
|
|
||||||
if (keluaran == null) {
|
|
||||||
invoke.reject("Tidak ada perangkat yang terhubung")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
menghubungkanKoneksi(args.alamat ?: "")
|
||||||
|
|
||||||
|
if (keluaran == null) {
|
||||||
|
throw Exception("Tidak ada perangkat yang terhubung")
|
||||||
|
}
|
||||||
|
|
||||||
keluaran!!.write(args.data)
|
keluaran!!.write(args.data)
|
||||||
keluaran!!.flush()
|
keluaran!!.flush()
|
||||||
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
memutusKoneksi(invoke)
|
memutusKoneksi()
|
||||||
invoke.reject("Mengirim data ke perangkat gagal: ${e.message}")
|
invoke.reject("Mengirim data ke perangkat gagal: ${e.message}")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
memutusKoneksi(invoke)
|
memutusKoneksi()
|
||||||
invoke.resolve()
|
invoke.resolve()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "tauri-plugin-bluclas-api",
|
"name": "tauri-plugin-bluclas-api",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"author": "You",
|
"author": "Rizky Denianto",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"types": "./dist-js/index.d.ts",
|
"types": "./dist-js/index.d.ts",
|
||||||
|
|||||||
+14
-7
@@ -1,13 +1,20 @@
|
|||||||
use tauri::{AppHandle, command, Runtime};
|
use tauri::{AppHandle, Runtime, command};
|
||||||
|
|
||||||
use crate::models::*;
|
|
||||||
use crate::Result;
|
|
||||||
use crate::BluclasExt;
|
use crate::BluclasExt;
|
||||||
|
use crate::Result;
|
||||||
|
use crate::models::*;
|
||||||
|
|
||||||
#[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<SBlueclasResponDaftarPerangkat> {
|
||||||
) -> Result<PingResponse> {
|
return app.bluclas().daftar_perangkat();
|
||||||
app.bluclas().ping(payload)
|
}
|
||||||
|
|
||||||
|
#[command]
|
||||||
|
pub(crate) async fn kirim<R: Runtime>(
|
||||||
|
app: AppHandle<R>,
|
||||||
|
payload: SBlueclasMuatanKirim,
|
||||||
|
) -> Result<()> {
|
||||||
|
return app.bluclas().kirim(payload);
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-9
@@ -1,22 +1,24 @@
|
|||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use tauri::{plugin::PluginApi, AppHandle, Runtime};
|
use tauri::{AppHandle, Runtime, plugin::PluginApi};
|
||||||
|
|
||||||
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<SBlueclasResponDaftarPerangkat> {
|
||||||
Ok(PingResponse {
|
return Ok(SBlueclasResponDaftarPerangkat { data: vec![] });
|
||||||
value: payload.value,
|
}
|
||||||
})
|
|
||||||
}
|
pub fn kirim(&self, _: SBlueclasMuatanKirim) -> crate::Result<()> {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-17
@@ -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
@@ -1,7 +1,7 @@
|
|||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use tauri::{
|
use tauri::{
|
||||||
plugin::{PluginApi, PluginHandle},
|
AppHandle, Runtime,
|
||||||
AppHandle, Runtime,
|
plugin::{PluginApi, PluginHandle},
|
||||||
};
|
};
|
||||||
|
|
||||||
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<SBlueclasResponDaftarPerangkat> {
|
||||||
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: SBlueclasMuatanKirim) -> crate::Result<()> {
|
||||||
|
return self
|
||||||
|
.0
|
||||||
|
.run_mobile_plugin("kirim", payload)
|
||||||
|
.map_err(Into::into);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-5
@@ -1,13 +1,21 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PingRequest {
|
pub struct SDaftarPerangkatBlueclasResponDaftarPerangkat {
|
||||||
pub value: Option<String>,
|
pub nama: String,
|
||||||
|
pub alamat: 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 SBlueclasResponDaftarPerangkat {
|
||||||
pub value: Option<String>,
|
pub data: Vec<SDaftarPerangkatBlueclasResponDaftarPerangkat>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct SBlueclasMuatanKirim {
|
||||||
|
pub alamat: String,
|
||||||
|
pub data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user