Compare commits
2 Commits
df87a5e811
...
bc3ac09f0c
| Author | SHA1 | Date | |
|---|---|---|---|
| bc3ac09f0c | |||
| 1afbd8817c |
@@ -33,7 +33,7 @@ class KirimArgs {
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
class BluclasPlugin(private val activity: Activity) : Plugin(activity) {
|
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 val adapter: BluetoothAdapter? = this.activity.getSystemService(BluetoothManager::class.java).getAdapter()
|
||||||
private var soket: BluetoothSocket? = null
|
private var soket: BluetoothSocket? = null
|
||||||
@@ -59,55 +59,41 @@ class BluclasPlugin(private val activity: Activity) : Plugin(activity) {
|
|||||||
invoke.resolve(objekDaftarRes)
|
invoke.resolve(objekDaftarRes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun menghubungkanKoneksi(invoke: Invoke, alamat: String) {
|
fun menghubungkanKoneksi(alamat: String) {
|
||||||
try {
|
val perangkat: BluetoothDevice = adapter!!.getRemoteDevice(alamat)
|
||||||
val perangkat: BluetoothDevice = adapter!!.getRemoteDevice(alamat)
|
soket = perangkat.createInsecureRfcommSocketToServiceRecord(uuidSpp)
|
||||||
|
soket!!.connect()
|
||||||
soket = perangkat.createRfcommSocketToServiceRecord(SPP_UUID)
|
keluaran = soket!!.outputStream
|
||||||
soket!!.connect()
|
|
||||||
keluaran = soket!!.outputStream
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
invoke.reject("Koneksi dengan perangkat gagal: ${e.message}")
|
|
||||||
}
|
|
||||||
|
|
||||||
invoke.resolve()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun memutusKoneksi(invoke: Invoke) {
|
fun memutusKoneksi() {
|
||||||
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)
|
||||||
|
|
||||||
menghubungkanKoneksi(invoke, args.alamat ?: "")
|
|
||||||
if (keluaran == null) {
|
|
||||||
invoke.reject("Tidak ada perangkat yang terhubung")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
menghubungkanKoneksi(args.alamat ?: "")
|
||||||
|
|
||||||
|
if (keluaran == null) {
|
||||||
|
throw Exception("Tidak ada perangkat yang terhubung")
|
||||||
|
}
|
||||||
|
|
||||||
keluaran!!.write(args.data)
|
keluaran!!.write(args.data)
|
||||||
keluaran!!.flush()
|
keluaran!!.flush()
|
||||||
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
memutusKoneksi(invoke)
|
memutusKoneksi()
|
||||||
invoke.reject("Mengirim data ke perangkat gagal: ${e.message}")
|
invoke.reject("Mengirim data ke perangkat gagal: ${e.message}")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
memutusKoneksi(invoke)
|
memutusKoneksi()
|
||||||
invoke.resolve()
|
invoke.resolve()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user