update
This commit is contained in:
parent
95c4bdaa9d
commit
18ae807a5b
@ -13,20 +13,45 @@ import xyz.mtfos.btdemo.objectTool.bind
|
|||||||
class MainActivity : Activity() {
|
class MainActivity : Activity() {
|
||||||
val intxt: EditText by bind(R.id.intxt)
|
val intxt: EditText by bind(R.id.intxt)
|
||||||
val btn: Button by bind(R.id.bt)
|
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
|
var ble: BLECls? = null
|
||||||
val uiHandler: Handler = Handler()
|
val uiHandler: Handler = Handler()
|
||||||
var th: Thread? = null
|
var th: Thread? = null
|
||||||
var thrun: Boolean = false
|
var thrun: Boolean = false
|
||||||
|
var isInit: Boolean = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
btn.isEnabled = false
|
btn.isEnabled = false
|
||||||
|
connectBtn.isEnabled = true
|
||||||
|
disconnectBtn.isEnabled = false
|
||||||
|
|
||||||
ble = BLECls(this@MainActivity)
|
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()!!) {
|
if (ble?.init()!!) {
|
||||||
|
isInit = true
|
||||||
ble?.startScan()
|
ble?.startScan()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +62,8 @@ class MainActivity : Activity() {
|
|||||||
if (ble?.mState == ble?.CONNECTED) {
|
if (ble?.mState == ble?.CONNECTED) {
|
||||||
uiHandler.post {
|
uiHandler.post {
|
||||||
btn.isEnabled = true
|
btn.isEnabled = true
|
||||||
|
disconnectBtn.isEnabled = true
|
||||||
|
connectBtn.isEnabled = false
|
||||||
}
|
}
|
||||||
thrun = false
|
thrun = false
|
||||||
break
|
break
|
||||||
@ -45,15 +72,9 @@ class MainActivity : Activity() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
th?.start()
|
th?.start()
|
||||||
|
|
||||||
btn.setOnClickListener {
|
|
||||||
val txt: String = intxt.text.toString()
|
|
||||||
ble?.sendLongData(txt.toByteArray())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
fun disconnect() {
|
||||||
super.onStop()
|
|
||||||
thrun = false
|
thrun = false
|
||||||
if (ble?.isScanning!!) {
|
if (ble?.isScanning!!) {
|
||||||
ble?.stopScan()
|
ble?.stopScan()
|
||||||
@ -61,5 +82,7 @@ class MainActivity : Activity() {
|
|||||||
if (ble?.mState == ble?.CONNECTED) {
|
if (ble?.mState == ble?.CONNECTED) {
|
||||||
ble?.disconnect()
|
ble?.disconnect()
|
||||||
}
|
}
|
||||||
|
disconnectBtn.isEnabled = false
|
||||||
|
connectBtn.isEnabled = true
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,24 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
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
|
<EditText
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
Loading…
Reference in New Issue
Block a user