fix btConnect

This commit is contained in:
skysamara 2022-09-02 19:55:23 +03:00
parent 41b849c86a
commit 353c34dbe3

View File

@ -30,15 +30,13 @@ class MainActivity : AppCompatActivity() {
// check is on/off
if (bAdapter == null) {
bluetoothStatusTV.text = "Bluetooht is not available"
}
else{
} else {
bluetoothStatusTV.text = "Bluetooht is available"
}
// Статус bluetooth
if (bAdapter.isEnabled) {
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
}
else{
} else {
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
}
@ -48,8 +46,7 @@ class MainActivity : AppCompatActivity() {
if (bAdapter.isEnabled) {
// Уже включен
Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()
}
else{
} else {
// Включаем
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(intent, REQUEST_CODE_ENABLE_BT)
@ -59,8 +56,7 @@ class MainActivity : AppCompatActivity() {
turnOffBtn.setOnClickListener {
if (!bAdapter.isEnabled) {
Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()
}
else{
} else {
bAdapter.disable()
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show()
@ -86,8 +82,7 @@ class MainActivity : AppCompatActivity() {
val deviceAddress = device
pairedTv.append("\nDevice: $deviceName , $device")
}
}
else{
} else {
Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show()
}
}
@ -96,11 +91,11 @@ class MainActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REQUEST_CODE_ENABLE_BT ->
if (requestCode == Activity.RESULT_OK){
// if (requestCode == Activity.RESULT_OK){
if (resultCode == Activity.RESULT_OK) {
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) // 14:14
Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show()
}
else{
} else {
Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_SHORT).show()
}