Practical 24.
1: Write a program to turn on, get visible, list devices and turn off
Bluetooth with the help of following GUI:
 JAVA FILE:
package com.example.prac24;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
  Button b1, b2, b3, b4; // Buttons for Bluetooth control
  private BluetoothAdapter BA; // Bluetooth Adapter
  private Set<BluetoothDevice> pairedDevices; // Set of paired devices
  ListView lv; // ListView to display paired devices
  private static final int REQUEST_ENABLE_BT = 1; // Request code for enabling Bluetooth
  private static final int REQUEST_BLUETOOTH_PERMISSIONS = 2; // Request code for Bluetooth
permissions
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Initialize UI components
      b1 = findViewById(R.id.TurnOn);
      b2 = findViewById(R.id.GetVisible);
      b3 = findViewById(R.id.ListDevices);
      b4 = findViewById(R.id.TurnOff);
      lv = findViewById(R.id.pairedDevice1);
      // Get the default Bluetooth adapter
      BA = BluetoothAdapter.getDefaultAdapter();
    // Check if the device supports Bluetooth
    if (BA == null) {
       Toast.makeText(getApplicationContext(), "Bluetooth not supported on this device",
Toast.LENGTH_LONG).show();
       finish();
    }
      // Request runtime permissions for Android 12+
      requestBluetoothPermissions();
      // Set button actions
      b1.setOnClickListener(this::on);
      b2.setOnClickListener(this::visible);
      b3.setOnClickListener(this::list);
      b4.setOnClickListener(this::off);
  }
 private void requestBluetoothPermissions() {
   if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)
        != PackageManager.PERMISSION_GRANTED ||
        ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN)
             != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this,
          new String[]{Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN},
          REQUEST_BLUETOOTH_PERMISSIONS);
   }
 }
  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_BLUETOOTH_PERMISSIONS) {
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
            Toast.makeText(this, "Bluetooth permissions granted", Toast.LENGTH_SHORT).show();
         } else {
            Toast.makeText(this, "Bluetooth permissions denied", Toast.LENGTH_SHORT).show();
            finish();
         }
     }
 }
 @SuppressLint("MissingPermission")
 public void on(View v) {
   if (BA.isEnabled()) {
      Toast.makeText(this, "Bluetooth is already on", Toast.LENGTH_SHORT).show();
   } else {
      Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
      Toast.makeText(this, "Bluetooth is now turned on", Toast.LENGTH_SHORT).show();
   }
 }
 @SuppressLint("MissingPermission")
 public void off(View v) {
   if (BA.isEnabled()) {
      BA.disable();
      Toast.makeText(this, "Bluetooth is now turned off", Toast.LENGTH_SHORT).show();
   } else {
      Toast.makeText(this, "Bluetooth is already off", Toast.LENGTH_SHORT).show();
   }
 }
  @SuppressLint("MissingPermission")
  public void visible(View v) {
    Intent visibleIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    visibleIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
    startActivity(visibleIntent);
    Toast.makeText(this, "Device is now discoverable for 5 minutes",
Toast.LENGTH_SHORT).show();
  }
 @SuppressLint("MissingPermission")
 public void list(View v) {
   pairedDevices = BA.getBondedDevices();
     ArrayList<String> deviceList = new ArrayList<>();
     for (BluetoothDevice device : pairedDevices) {
       deviceList.add(device.getName() + " - " + device.getAddress());
     }
    if (!deviceList.isEmpty()) {
       ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, deviceList);
       lv.setAdapter(adapter);
    } else {
       Toast.makeText(this, "No paired devices found", Toast.LENGTH_SHORT).show();
    }
  }
}
activity file:
<?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"
  android:padding="16dp"
  android:background="#FFFFFF">
  <!-- Title TextView -->
  <TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Bluetooth Control Panel"
    android:textColor="#0D0B0B"
    android:textSize="24sp"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
  <!-- Turn On Button -->
  <Button
    android:id="@+id/TurnOn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textview1"
    android:layout_marginTop="16dp"
    android:layout_centerHorizontal="true"
    android:backgroundTint="#809CC7"
    android:text="Turn On"
    android:textStyle="bold" />
<!-- Get Visible Button -->
<Button
  android:id="@+id/GetVisible"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/TurnOn"
  android:layout_marginTop="16dp"
  android:layout_centerHorizontal="true"
  android:backgroundTint="#626DC5"
  android:text="Get Visible"
  android:textStyle="bold" />
<!-- List Devices Button -->
<Button
  android:id="@+id/ListDevices"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/GetVisible"
  android:layout_marginTop="16dp"
  android:layout_centerHorizontal="true"
  android:backgroundTint="#24338A"
  android:text="List Devices"
  android:textStyle="bold" />
<!-- Turn Off Button -->
<Button
  android:id="@+id/TurnOff"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/ListDevices"
  android:layout_marginTop="16dp"
  android:layout_centerHorizontal="true"
  android:backgroundTint="#111157"
  android:text="Turn Off"
  android:textStyle="bold" />
<!-- Paired Devices Label -->
<TextView
  android:id="@+id/paired"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/TurnOff"
  android:layout_marginTop="24dp"
  android:layout_marginBottom="8dp"
    android:layout_centerHorizontal="true"
    android:text="Paired Devices:"
    android:textColor="#02040D"
    android:textSize="18sp"
    android:textStyle="bold" />
  <!-- Paired Devices List -->
  <ListView
    android:id="@+id/pairedDevice1"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@id/paired"
    android:layout_marginTop="8dp"
    android:layout_marginHorizontal="16dp"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp" />
</RelativeLayout>
Manifest File:
        <manifest
         xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.example.prac24">
          <!-- Bluetooth Permissions -->
          <uses-permission android:name="android.permission.BLUETOOTH" />
          <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
          <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
          <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
          <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
          <!-- Location Permissions for Bluetooth Scanning -->
          <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
          <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
          <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.Prac24">
            <activity android:name=".MainActivity"
              android:exported="true">
              <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
          </activity>
       </application>
     </manifest>
Output: