fix btConnect

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

View File

@ -14,11 +14,11 @@ import kotlinx.android.synthetic.main.activity_main.*
// //
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private val REQUEST_CODE_ENABLE_BT:Int = 1 private val REQUEST_CODE_ENABLE_BT: Int = 1
private val REQUEST_CODE_DISCOVERBLE_BT:Int = 2 private val REQUEST_CODE_DISCOVERBLE_BT: Int = 2
// bluetooth adapter // bluetooth adapter
lateinit var bAdapter:BluetoothAdapter lateinit var bAdapter: BluetoothAdapter
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -28,28 +28,25 @@ class MainActivity : AppCompatActivity() {
// bluetooth adapter // bluetooth adapter
bAdapter = BluetoothAdapter.getDefaultAdapter() bAdapter = BluetoothAdapter.getDefaultAdapter()
// check is on/off // check is on/off
if(bAdapter == null){ if (bAdapter == null) {
bluetoothStatusTV.text = "Bluetooht is not available" bluetoothStatusTV.text = "Bluetooht is not available"
} } else {
else{
bluetoothStatusTV.text = "Bluetooht is available" bluetoothStatusTV.text = "Bluetooht is available"
} }
// Статус bluetooth // Статус bluetooth
if (bAdapter.isEnabled){ if (bAdapter.isEnabled) {
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
} } else {
else{
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off) bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
} }
// Включаем bluetooth // Включаем bluetooth
// TODO Bluetooth включается, но сообщение и иконка неправильные // TODO Bluetooth включается, но сообщение и иконка неправильные
turnOnBtn.setOnClickListener { turnOnBtn.setOnClickListener {
if (bAdapter.isEnabled){ if (bAdapter.isEnabled) {
// Уже включен // Уже включен
Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()
} } else {
else{
// Включаем // Включаем
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(intent, REQUEST_CODE_ENABLE_BT) startActivityForResult(intent, REQUEST_CODE_ENABLE_BT)
@ -57,10 +54,9 @@ class MainActivity : AppCompatActivity() {
} }
// Выключаем bluetooth // Выключаем bluetooth
turnOffBtn.setOnClickListener { turnOffBtn.setOnClickListener {
if (!bAdapter.isEnabled){ if (!bAdapter.isEnabled) {
Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()
} } else {
else{
bAdapter.disable() bAdapter.disable()
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off) bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show()
@ -69,38 +65,37 @@ class MainActivity : AppCompatActivity() {
} }
// Ищем bluetooth // Ищем bluetooth
discoverableBtn.setOnClickListener { discoverableBtn.setOnClickListener {
if (!bAdapter.isDiscovering){ if (!bAdapter.isDiscovering) {
Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show()
val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE))
startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT) startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT)
} }
} }
// Список сопряженных устроств // Список сопряженных устроств
pairedBtn.setOnClickListener{ pairedBtn.setOnClickListener {
if (bAdapter.isEnabled){ if (bAdapter.isEnabled) {
pairedTv.text = "Paired devices" pairedTv.text = "Paired devices"
// получаем список сопряженных устройств // получаем список сопряженных устройств
val devices = bAdapter.bondedDevices val devices = bAdapter.bondedDevices
for(device in devices){ for (device in devices) {
val deviceName = device.name val deviceName = device.name
val deviceAddress = device val deviceAddress = device
pairedTv.append("\nDevice: $deviceName , $device") pairedTv.append("\nDevice: $deviceName , $device")
} }
} } else {
else{
Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show()
} }
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when(requestCode){ when (requestCode) {
REQUEST_CODE_ENABLE_BT -> 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 bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) // 14:14
Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show()
} } else {
else{
Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_SHORT).show()
} }