Wednesday, August 12, 2020

android kotlin - Enable disable bluetooth programmatically

MainActivity.kt

package com.example.jetpack

import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    private val REQUEST_ENABLE_BT = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get device default bluetooth adapter
        val adapter = BluetoothAdapter.getDefaultAdapter()

        // check device support bluetooth or not
        if (adapter != null){
            textView.text = "Device support Bluetooth."
        }else{
            textView.text = "Device does not support Bluetooth."
        }


        // get bluetooth enable disable status
        btnStatus.setOnClickListener {
            adapter?.apply {
                textView.text = if (isEnabled){
                    "Bluetooth is enable."
                }else{
                    "Bluetooth is disable."
                }
            }
        }


        // enable bluetooth programmatically
        btnEnable.setOnClickListener {
            adapter?.apply {
                if (!isEnabled){
                    val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                    startActivityForResult(intent, REQUEST_ENABLE_BT)
                }else{
                    textView.text = "Bluetooth is already enable."
                }
            }
        }


        // disable bluetooth programmatically
        btnDisable.setOnClickListener {
            adapter?.apply {
                if (isEnabled){
                    disable()
                }else{
                    textView.text = "Bluetooth is already disable."
                }
            }
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E5E4E2"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:backgroundTint="#36747D"
        android:text="Status"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnDisable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:backgroundTint="#C32148"
        android:text="Disable"
        app:layout_constraintBottom_toBottomOf="@+id/btnEnable"
        app:layout_constraintStart_toEndOf="@+id/btnEnable" />

    <Button
        android:id="@+id/btnEnable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:backgroundTint="#545AA7"
        android:text="Enable"
        app:layout_constraintBottom_toBottomOf="@+id/btnStatus"
        app:layout_constraintStart_toEndOf="@+id/btnStatus" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:fontFamily="sans-serif-condensed"
        android:gravity="center"
        android:textColor="#191970"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnStatus"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>
AndroidMenifest.xml [permissions]

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />