107 lines
3.6 KiB
Kotlin
107 lines
3.6 KiB
Kotlin
package io.gitlab.jfronny.hc05ac.bt
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.app.ProgressDialog
|
|
import android.bluetooth.BluetoothSocket
|
|
import android.os.AsyncTask
|
|
import android.os.Bundle
|
|
import android.widget.Button
|
|
import android.widget.TextView
|
|
import android.widget.Toast
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import io.gitlab.jfronny.hc05ac.R
|
|
import io.gitlab.jfronny.hc05ac.btAdapter
|
|
import java.io.IOException
|
|
import java.util.*
|
|
|
|
class LedControl : AppCompatActivity() {
|
|
var address: String? = null
|
|
var lumn: TextView? = null
|
|
private var progress: ProgressDialog? = null
|
|
var btSocket: BluetoothSocket? = null
|
|
private var isBtConnected = false
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
val newint = intent
|
|
address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS)
|
|
setContentView(R.layout.activity_led_control)
|
|
val btn1: Button = findViewById(R.id.button2)
|
|
val btn2: Button = findViewById(R.id.button3)
|
|
val btn3: Button = findViewById(R.id.button5)
|
|
val btn4: Button = findViewById(R.id.button6)
|
|
val btn5: Button = findViewById(R.id.button7)
|
|
val btnDis: Button = findViewById(R.id.button4)
|
|
lumn = findViewById(R.id.textView2)
|
|
ConnectBT().execute()
|
|
btn1.setOnClickListener { sendSignal("1") }
|
|
btn2.setOnClickListener { sendSignal("2") }
|
|
btn3.setOnClickListener { sendSignal("3") }
|
|
btn4.setOnClickListener { sendSignal("4") }
|
|
btn5.setOnClickListener { sendSignal("5") }
|
|
btnDis.setOnClickListener { disconnect() }
|
|
}
|
|
|
|
private fun sendSignal(number: String) {
|
|
if (btSocket != null) {
|
|
try {
|
|
btSocket!!.outputStream.write(number.toByteArray())
|
|
} catch (e: IOException) {
|
|
msg("Error")
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun disconnect() {
|
|
if (btSocket != null) {
|
|
try {
|
|
btSocket!!.close()
|
|
} catch (e: IOException) {
|
|
msg("Error")
|
|
}
|
|
}
|
|
finish()
|
|
}
|
|
|
|
private fun msg(s: String) {
|
|
Toast.makeText(applicationContext, s, Toast.LENGTH_LONG).show()
|
|
}
|
|
|
|
private inner class ConnectBT : AsyncTask<Void?, Void?, Void?>() {
|
|
private var connectSuccess = true
|
|
override fun onPreExecute() {
|
|
progress = ProgressDialog.show(this@LedControl, "Connecting...", "Please Wait!!!")
|
|
}
|
|
|
|
@SuppressLint("MissingPermission")
|
|
override fun doInBackground(vararg devices: Void?): Void? {
|
|
try {
|
|
if (btSocket == null || !isBtConnected) {
|
|
val bluetoothAdapter = btAdapter
|
|
val dispositivo = bluetoothAdapter.getRemoteDevice(address)
|
|
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID)
|
|
bluetoothAdapter.cancelDiscovery()
|
|
btSocket!!.connect()
|
|
}
|
|
} catch (e: IOException) {
|
|
connectSuccess = false
|
|
}
|
|
return null
|
|
}
|
|
|
|
override fun onPostExecute(result: Void?) {
|
|
super.onPostExecute(result)
|
|
if (!connectSuccess) {
|
|
msg("Connection Failed. Is it a SPP Bluetooth? Try again.")
|
|
finish()
|
|
} else {
|
|
msg("Connected")
|
|
isBtConnected = true
|
|
}
|
|
progress!!.dismiss()
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
val myUUID: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
|
|
}
|
|
} |