114 lines
3.0 KiB
Kotlin
114 lines
3.0 KiB
Kotlin
package com.plugin.bluclas
|
|
|
|
import android.Manifest
|
|
import android.app.Activity
|
|
import android.bluetooth.BluetoothAdapter
|
|
import android.bluetooth.BluetoothDevice
|
|
import android.bluetooth.BluetoothManager
|
|
import android.bluetooth.BluetoothSocket
|
|
import app.tauri.annotation.Command
|
|
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
|
|
import java.util.*
|
|
|
|
|
|
@InvokeArg
|
|
class KirimArgs {
|
|
var alamat: String? = null
|
|
var data: ByteArray? = null
|
|
}
|
|
|
|
@TauriPlugin(
|
|
permissions = [
|
|
Permission(
|
|
strings = [Manifest.permission.BLUETOOTH_CONNECT],
|
|
alias = "bluetoothConnect"
|
|
)
|
|
]
|
|
)
|
|
class BluclasPlugin(private val activity: Activity) : Plugin(activity) {
|
|
private val SPP_UUID: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
|
|
|
|
private val adapter: BluetoothAdapter? = this.activity.getSystemService(BluetoothManager::class.java).getAdapter()
|
|
private var soket: BluetoothSocket? = null
|
|
private var keluaran: OutputStream? = null
|
|
|
|
@Command
|
|
fun daftarPerangkat(invoke: Invoke) {
|
|
if (adapter == null) {
|
|
invoke.reject("Bluetooth tidak didukung")
|
|
return
|
|
}
|
|
|
|
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 objekDaftarRes = JSObject()
|
|
objekDaftarRes.put("data", daftarRes)
|
|
invoke.resolve(objekDaftarRes)
|
|
}
|
|
|
|
fun menghubungkanKoneksi(invoke: Invoke, alamat: String) {
|
|
try {
|
|
val perangkat: BluetoothDevice = adapter!!.getRemoteDevice(alamat)
|
|
|
|
soket = perangkat.createRfcommSocketToServiceRecord(SPP_UUID)
|
|
soket!!.connect()
|
|
keluaran = soket!!.outputStream
|
|
|
|
} catch (e: Exception) {
|
|
invoke.reject("Koneksi dengan perangkat gagal: ${e.message}")
|
|
}
|
|
|
|
invoke.resolve()
|
|
}
|
|
|
|
fun memutusKoneksi(invoke: Invoke) {
|
|
try {
|
|
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 ?: "")
|
|
if (keluaran == null) {
|
|
invoke.reject("Tidak ada perangkat yang terhubung")
|
|
return
|
|
}
|
|
|
|
try {
|
|
keluaran!!.write(args.data)
|
|
keluaran!!.flush()
|
|
|
|
} catch (e: Exception) {
|
|
memutusKoneksi(invoke)
|
|
invoke.reject("Mengirim data ke perangkat gagal: ${e.message}")
|
|
}
|
|
|
|
memutusKoneksi(invoke)
|
|
invoke.resolve()
|
|
}
|
|
}
|