From 353c34dbe3652719bb58f8c456025b350d2923c9 Mon Sep 17 00:00:00 2001 From: skysamara Date: Fri, 2 Sep 2022 19:55:23 +0300 Subject: [PATCH] fix btConnect --- .../java/ru/panorobot/airshot/MainActivity.kt | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/ru/panorobot/airshot/MainActivity.kt b/app/src/main/java/ru/panorobot/airshot/MainActivity.kt index 1753f95..7bf7511 100644 --- a/app/src/main/java/ru/panorobot/airshot/MainActivity.kt +++ b/app/src/main/java/ru/panorobot/airshot/MainActivity.kt @@ -14,11 +14,11 @@ import kotlinx.android.synthetic.main.activity_main.* // class MainActivity : AppCompatActivity() { - private val REQUEST_CODE_ENABLE_BT:Int = 1 - private val REQUEST_CODE_DISCOVERBLE_BT:Int = 2 + private val REQUEST_CODE_ENABLE_BT: Int = 1 + private val REQUEST_CODE_DISCOVERBLE_BT: Int = 2 // bluetooth adapter - lateinit var bAdapter:BluetoothAdapter + lateinit var bAdapter: BluetoothAdapter @SuppressLint("MissingPermission") override fun onCreate(savedInstanceState: Bundle?) { @@ -28,28 +28,25 @@ class MainActivity : AppCompatActivity() { // bluetooth adapter bAdapter = BluetoothAdapter.getDefaultAdapter() // check is on/off - if(bAdapter == null){ + if (bAdapter == null) { bluetoothStatusTV.text = "Bluetooht is not available" - } - else{ + } else { bluetoothStatusTV.text = "Bluetooht is available" } // Статус bluetooth - if (bAdapter.isEnabled){ + if (bAdapter.isEnabled) { bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) - } - else{ + } else { bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off) } // Включаем bluetooth // TODO Bluetooth включается, но сообщение и иконка неправильные turnOnBtn.setOnClickListener { - if (bAdapter.isEnabled){ + 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) @@ -57,10 +54,9 @@ class MainActivity : AppCompatActivity() { } // Выключаем bluetooth turnOffBtn.setOnClickListener { - if (!bAdapter.isEnabled){ + 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() @@ -69,38 +65,37 @@ class MainActivity : AppCompatActivity() { } // Ищем bluetooth discoverableBtn.setOnClickListener { - if (!bAdapter.isDiscovering){ + if (!bAdapter.isDiscovering) { Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show() val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT) } } // Список сопряженных устроств - pairedBtn.setOnClickListener{ - if (bAdapter.isEnabled){ + pairedBtn.setOnClickListener { + if (bAdapter.isEnabled) { pairedTv.text = "Paired devices" // получаем список сопряженных устройств val devices = bAdapter.bondedDevices - for(device in devices){ + for (device in devices) { val deviceName = device.name val deviceAddress = device pairedTv.append("\nDevice: $deviceName , $device") } - } - else{ + } else { Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show() } } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - when(requestCode){ + 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() }