diff --git a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt index da4d9a8..3f26e53 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt +++ b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt @@ -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 } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c8763f4..4621ea2 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,6 +4,24 @@ android:layout_height="match_parent" android:orientation="vertical"> + +