Menghapus berkas contoh dan mengunggah program bluclas
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package com.plugin.bluclas
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("com.plugin.bluclas", appContext.packageName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.plugin.bluetooth_classic
|
||||
|
||||
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.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 BluetoothClassicPlugin(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 daftar_res = adapter.bondedDevices.map { i ->
|
||||
mapOf(
|
||||
"nama" to (i.name ?: "Unknown"),
|
||||
"alamat" to i.address
|
||||
)
|
||||
}
|
||||
|
||||
val objek_daftar_res = JSObject()
|
||||
objek_daftar_res.put("data", daftar_res)
|
||||
invoke.resolve(objek_daftar_res)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.plugin.bluclas
|
||||
|
||||
import android.util.Log
|
||||
|
||||
class Example {
|
||||
fun pong(value: String): String {
|
||||
Log.i("Pong", value)
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.plugin.bluclas
|
||||
|
||||
import android.app.Activity
|
||||
import app.tauri.annotation.Command
|
||||
import app.tauri.annotation.InvokeArg
|
||||
import app.tauri.annotation.TauriPlugin
|
||||
import app.tauri.plugin.JSObject
|
||||
import app.tauri.plugin.Plugin
|
||||
import app.tauri.plugin.Invoke
|
||||
|
||||
@InvokeArg
|
||||
class PingArgs {
|
||||
var value: String? = null
|
||||
}
|
||||
|
||||
@TauriPlugin
|
||||
class ExamplePlugin(private val activity: Activity): Plugin(activity) {
|
||||
private val implementation = Example()
|
||||
|
||||
@Command
|
||||
fun ping(invoke: Invoke) {
|
||||
val args = invoke.parseArgs(PingArgs::class.java)
|
||||
|
||||
val ret = JSObject()
|
||||
ret.put("value", implementation.pong(args.value ?: "default value :("))
|
||||
invoke.resolve(ret)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.plugin.bluclas
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user