This commit is contained in:
Jay 2017-08-17 17:48:38 +08:00
parent 95c4bdaa9d
commit 18ae807a5b
2 changed files with 48 additions and 7 deletions

View File

@ -13,20 +13,45 @@ import xyz.mtfos.btdemo.objectTool.bind
class MainActivity : Activity() {
val intxt: EditText by bind(R.id.intxt)
val btn: Button by bind(R.id.bt)
val connectBtn: Button by bind(R.id.connect)
val disconnectBtn: Button by bind(R.id.disconnect)
var ble: BLECls? = null
val uiHandler: Handler = Handler()
var th: Thread? = null
var thrun: Boolean = false
var isInit: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn.isEnabled = false
connectBtn.isEnabled = true
disconnectBtn.isEnabled = false
ble = BLECls(this@MainActivity)
btn.setOnClickListener {
val txt: String = intxt.text.toString()
ble?.sendLongData(txt.toByteArray())
}
connectBtn.setOnClickListener {
scanConnect()
}
disconnectBtn.setOnClickListener {
disconnect()
}
}
override fun onStop() {
super.onStop()
disconnect()
}
fun scanConnect(){
if (ble?.init()!!) {
isInit = true
ble?.startScan()
}
@ -37,6 +62,8 @@ class MainActivity : Activity() {
if (ble?.mState == ble?.CONNECTED) {
uiHandler.post {
btn.isEnabled = true
disconnectBtn.isEnabled = true
connectBtn.isEnabled = false
}
thrun = false
break
@ -45,15 +72,9 @@ class MainActivity : Activity() {
}
})
th?.start()
btn.setOnClickListener {
val txt: String = intxt.text.toString()
ble?.sendLongData(txt.toByteArray())
}
}
override fun onStop() {
super.onStop()
fun disconnect() {
thrun = false
if (ble?.isScanning!!) {
ble?.stopScan()
@ -61,5 +82,7 @@ class MainActivity : Activity() {
if (ble?.mState == ble?.CONNECTED) {
ble?.disconnect()
}
disconnectBtn.isEnabled = false
connectBtn.isEnabled = true
}
}

View File

@ -4,6 +4,24 @@
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/connect"
android:text="Connect"
android:textAllCaps="false"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/disconnect"
android:text="Disconnect"
android:textAllCaps="false"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"