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