65 lines
1.6 KiB
Kotlin
65 lines
1.6 KiB
Kotlin
package xyz.mtfos.btdemo
|
|
|
|
import android.app.Activity
|
|
import android.os.Bundle
|
|
import android.os.Handler
|
|
import android.widget.Button
|
|
import android.widget.EditText
|
|
import xyz.mtfos.btdemo.objectTool.bind
|
|
|
|
/**
|
|
* Created by jay on 2017/8/9.
|
|
*/
|
|
class MainActivity : Activity() {
|
|
val intxt: EditText by bind(R.id.intxt)
|
|
val btn: Button by bind(R.id.bt)
|
|
var ble: BLECls? = null
|
|
val uiHandler: Handler = Handler()
|
|
var th: Thread? = null
|
|
var thrun: Boolean = false
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_main)
|
|
|
|
btn.isEnabled = false
|
|
|
|
ble = BLECls(this@MainActivity)
|
|
|
|
if (ble?.init()!!) {
|
|
ble?.startScan()
|
|
}
|
|
|
|
th = Thread(Runnable {
|
|
thrun = true
|
|
while (thrun) {
|
|
System.out.println("state ===== " + ble?.mState)
|
|
if (ble?.mState == ble?.CONNECTED) {
|
|
uiHandler.post {
|
|
btn.isEnabled = true
|
|
}
|
|
thrun = false
|
|
break
|
|
}
|
|
Thread.sleep(200)
|
|
}
|
|
})
|
|
th?.start()
|
|
|
|
btn.setOnClickListener {
|
|
val txt: String = intxt.text.toString()
|
|
ble?.sendLongData(txt.toByteArray())
|
|
}
|
|
}
|
|
|
|
override fun onStop() {
|
|
super.onStop()
|
|
thrun = false
|
|
if (ble?.isScanning!!) {
|
|
ble?.stopScan()
|
|
}
|
|
if (ble?.mState == ble?.CONNECTED) {
|
|
ble?.disconnect()
|
|
}
|
|
}
|
|
} |