Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1afbd8817c |
@@ -33,7 +33,7 @@ class KirimArgs {
|
||||
]
|
||||
)
|
||||
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 var soket: BluetoothSocket? = null
|
||||
@@ -59,55 +59,41 @@ class BluclasPlugin(private val activity: Activity) : Plugin(activity) {
|
||||
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 menghubungkanKoneksi(alamat: String) {
|
||||
val perangkat: BluetoothDevice = adapter!!.getRemoteDevice(alamat)
|
||||
soket = perangkat.createInsecureRfcommSocketToServiceRecord(uuidSpp)
|
||||
soket!!.connect()
|
||||
keluaran = soket!!.outputStream
|
||||
}
|
||||
|
||||
fun memutusKoneksi(invoke: Invoke) {
|
||||
try {
|
||||
keluaran?.close()
|
||||
keluaran = null
|
||||
soket?.close()
|
||||
soket = null
|
||||
|
||||
} catch (e: Exception) {
|
||||
invoke.reject("Memutus koneksi perangkat gagal")
|
||||
}
|
||||
|
||||
invoke.resolve()
|
||||
fun memutusKoneksi() {
|
||||
keluaran?.close()
|
||||
keluaran = null
|
||||
soket?.close()
|
||||
soket = null
|
||||
}
|
||||
|
||||
@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 {
|
||||
menghubungkanKoneksi(args.alamat ?: "")
|
||||
|
||||
if (keluaran == null) {
|
||||
throw Exception("Tidak ada perangkat yang terhubung")
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user