Update MainActivity.kt

master
Zhengyu Peng 3 years ago
parent ee5192138e
commit 23a154a9c3

@ -101,11 +101,11 @@ class MainActivity : AppCompatActivity() {
readSharedPref() readSharedPref()
if (tabLayout.selectedTabPosition == 0){ if (tabLayout.selectedTabPosition == 0) {
ipLayout.visibility = View.VISIBLE ipLayout.visibility = View.VISIBLE
portLayout.visibility = View.VISIBLE portLayout.visibility = View.VISIBLE
selectedDevice.visibility = View.GONE selectedDevice.visibility = View.GONE
} else if (tabLayout.selectedTabPosition == 1){ } else if (tabLayout.selectedTabPosition == 1) {
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE) checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
ipLayout.visibility = View.GONE ipLayout.visibility = View.GONE
portLayout.visibility = View.GONE portLayout.visibility = View.GONE
@ -116,11 +116,13 @@ class MainActivity : AppCompatActivity() {
// your code to perform when the user clicks on the button // your code to perform when the user clicks on the button
// Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show() // Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
if (tabLayout.selectedTabPosition == 0) {
if (isNumericAddress(ipInput.text.toString()) && portInput.text.toString() if (isNumericAddress(ipInput.text.toString()) && portInput.text.toString()
.toInt() >= 0 && portInput.text.toString().toInt() <= 65535 .toInt() >= 0 && portInput.text.toString().toInt() <= 65535
) { ) {
saveSharedPref() saveSharedPref()
val intent = Intent(this, ControlActivity::class.java).apply { val intent = Intent(this, ControlActivity::class.java).apply {
putExtra("interface", "WiFi")
putExtra("ip", ipInput.text.toString()) putExtra("ip", ipInput.text.toString())
putExtra("port", portInput.text.toString()) putExtra("port", portInput.text.toString())
} }
@ -130,6 +132,16 @@ class MainActivity : AppCompatActivity() {
} else { } else {
portLayout.error = getString(R.string.invalid_port) portLayout.error = getString(R.string.invalid_port)
} }
} else if (tabLayout.selectedTabPosition == 1) {
if(deviceAddress.text.isNotBlank()){
saveSharedPref()
val intent = Intent(this, ControlActivity::class.java).apply {
putExtra("interface", "Bluetooth")
putExtra("mac", deviceAddress.text.toString())
}
startActivity(intent)
}
}
} }
@ -191,7 +203,11 @@ class MainActivity : AppCompatActivity() {
super.onRequestPermissionsResult(requestCode, permissions, grantResults) super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == BLUETOOTH_PERMISSION_CODE) { if (requestCode == BLUETOOTH_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this@MainActivity, "Bluetooth Permission Granted", Toast.LENGTH_SHORT) Toast.makeText(
this@MainActivity,
"Bluetooth Permission Granted",
Toast.LENGTH_SHORT
)
.show() .show()
} else { } else {
Toast.makeText(this@MainActivity, "Bluetooth Permission Denied", Toast.LENGTH_SHORT) Toast.makeText(this@MainActivity, "Bluetooth Permission Denied", Toast.LENGTH_SHORT)
@ -216,7 +232,7 @@ class MainActivity : AppCompatActivity() {
portInput.setText(prefs.getString(SHAREDPREFSPORT, "1234")) portInput.setText(prefs.getString(SHAREDPREFSPORT, "1234"))
val selectedTab = prefs.getString(SHARED_PREFS_TAB, "WiFi") val selectedTab = prefs.getString(SHARED_PREFS_TAB, "WiFi")
if (selectedTab == "WiFi"){ if (selectedTab == "WiFi") {
val tab = tabLayout.getTabAt(0) val tab = tabLayout.getTabAt(0)
tab!!.select() tab!!.select()
} else if (selectedTab == "Bluetooth") { } else if (selectedTab == "Bluetooth") {
@ -236,9 +252,9 @@ class MainActivity : AppCompatActivity() {
val editor = prefs.edit() val editor = prefs.edit()
editor.putString(SHAREDPREFSIP, ipInput.text.toString()) editor.putString(SHAREDPREFSIP, ipInput.text.toString())
editor.putString(SHAREDPREFSPORT, portInput.text.toString()) editor.putString(SHAREDPREFSPORT, portInput.text.toString())
if (tabLayout.selectedTabPosition == 0){ if (tabLayout.selectedTabPosition == 0) {
editor.putString(SHARED_PREFS_TAB, "WiFi") editor.putString(SHARED_PREFS_TAB, "WiFi")
} else if (tabLayout.selectedTabPosition == 1){ } else if (tabLayout.selectedTabPosition == 1) {
editor.putString(SHARED_PREFS_TAB, "Bluetooth") editor.putString(SHARED_PREFS_TAB, "Bluetooth")
} }

Loading…
Cancel
Save