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