Android Bluetooth Enable Disable Connectivity From APP

Android bluetooth enable disable from your app with button click

Many application come with Bluetooth feature all you need to setup a button for on off Bluetooth directly from your application with this easy tutorial. Know more from android official site about Bluetooth connectivity. Get complete video tutorial for beginner android Bluetooth on off. Below is the complete source code of android Bluetooth enable disable.

package attraction.blessme.begin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int REQ_ENABLE_CODE = 0;
    private static final int REQ_DISCOVER_CODE = 0;
    private BluetoothAdapter btAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Button enableBT = (Button) findViewById(R.id.enable_bt);
        final Button discoverBT = (Button) findViewById(R.id.discover_bt);
        final Button disableBT = (Button) findViewById(R.id.disable_bt);
        
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            //textView.append("device does not support BT");
            Toast.makeText(getApplicationContext(), "Device does not support BT", Toast.LENGTH_LONG);
        }

        enableBT.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                
                if (!btAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQ_ENABLE_CODE);
                    Toast.makeText(getApplicationContext(), "BLUETOOTH IS ENABLED", Toast.LENGTH_SHORT);
                }
            }
        });
        
        disableBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                btAdapter.disable();
                Toast.makeText(getApplicationContext(), "BLUETOOTH IS DISABLED", Toast.LENGTH_SHORT);
            }
        });

        discoverBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (!btAdapter.isDiscovering()) {

                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    startActivityForResult(enableBtIntent, REQ_DISCOVER_CODE);

                    Toast.makeText(getApplicationContext(), "SET YOUR BT DISCOVERABLE", Toast.LENGTH_LONG);
                }
            }
        });
    }

}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#3bffd8"
    tools:context=".MainActivity">

    <TextView
        android:text="BLUETOOTH / WiFi ON OFF Example"
        android:id="@+id/out"
        android:textStyle ="bold"
        android:textSize ="30sp"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_alignParentBottom ="true"
        android:layout_centerHorizontal ="true"
        android:layout_marginBottom ="18dp">
    </TextView>

    <Button
        android:id="@+id/enable_bt"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="05dp"
        android:textColor="#ffffff"
        android:background="#128dff"
        android:textStyle="bold"
        android:textSize="25sp"
        android:text="ENABLE BT"
        android:layout_above="@+id/disable_bt"
        android:layout_toStartOf="@+id/discover_bt" />

    <Button
        android:id="@+id/discover_bt"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:background="#ff0400"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_margin="10dp"
        android:text="DISCOVER BlueTooth"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:id="@+id/disable_bt"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:text="DISABLE BT"
        android:textColor="#ffffff"
        android:background="#0080ff"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="attraction.blessme.begin">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Visit ATTRACTION ANDROID page for more latest android code in single place.

Add a Comment

Your email address will not be published. Required fields are marked *