45 lines
1.4 KiB
Kotlin
45 lines
1.4 KiB
Kotlin
package io.gitlab.jfronny.hc05ac.bt
|
|
|
|
import android.bluetooth.BluetoothSocket
|
|
import android.os.Bundle
|
|
import io.gitlab.jfronny.hc05ac.InputActivity
|
|
import io.gitlab.jfronny.hc05ac.util.logE
|
|
import io.gitlab.jfronny.hc05ac.util.toast
|
|
import java.io.IOException
|
|
|
|
class BtInputActivity : InputActivity(), ConnectBtTask.Ctx {
|
|
override var address: String? = null
|
|
override fun close() = finish()
|
|
override val context: BtInputActivity = this
|
|
override var btSocket: BluetoothSocket? = null
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
address = intent.getStringExtra(DeviceListActivity.EXTRA_ADDRESS)
|
|
ConnectBtTask(this).execute()
|
|
}
|
|
|
|
override fun send(left: Byte, right: Byte) {
|
|
if (btSocket != null) {
|
|
try {
|
|
val pl = left.toUByte() and 0xF0u
|
|
val pr = ((right.toUByte() and 0xF0u).div(16u)).toUByte()
|
|
btSocket!!.outputStream.write(byteArrayOf((pl or pr).toByte()))
|
|
} catch (e: IOException) {
|
|
logE("Could not send signal", e)
|
|
toast("Error")
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
super.onDestroy()
|
|
if (btSocket != null) {
|
|
try {
|
|
btSocket!!.close()
|
|
} catch (e: IOException) {
|
|
logE("Could not disconnect", e)
|
|
}
|
|
}
|
|
}
|
|
} |