Practical 24
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;
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);
@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();
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:
Manifest File:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prac24">
<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: