0% found this document useful (0 votes)
64 views

Exp 24

The document contains code for an Android app that uses Bluetooth functionality. It includes an Activity_main.xml layout file with buttons to turn Bluetooth on/off, get visible devices, and list paired devices. The MainActivity.java file implements these buttons by requesting Bluetooth permissions, enabling/disabling Bluetooth, discovering/pairing with devices, and displaying paired devices in a list. It handles runtime permission requests. The app allows users to control and interact with nearby Bluetooth devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Exp 24

The document contains code for an Android app that uses Bluetooth functionality. It includes an Activity_main.xml layout file with buttons to turn Bluetooth on/off, get visible devices, and list paired devices. The MainActivity.java file implements these buttons by requesting Bluetooth permissions, enabling/disabling Bluetooth, discovering/pairing with devices, and displaying paired devices in a list. It handles runtime permission requests. The app allows users to control and interact with nearby Bluetooth devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Activity_main.

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

<Button
android:id="@+id/turnOnButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/getVisibleDevicesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Visible Devices"
android:layout_below="@id/turnOnButton"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/listDevicesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices"
android:layout_below="@id/getVisibleDevicesButton"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/turnOffButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"
android:layout_below="@id/listDevicesButton"
android:layout_marginTop="16dp"/>

<ListView
android:id="@+id/deviceListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/turnOffButton"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="Developed by Shreya Shukla(60)"
android:textStyle="bold" />

</RelativeLayout>
MainActivity.java:

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_ENABLE_BT = 1;

private BluetoothAdapter bluetoothAdapter;


private ArrayAdapter<String> deviceListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exp24_1);

// Request Bluetooth permissions at runtime


requestBluetoothPermissions();

// Initialize BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Initialize UI elements
Button turnOnButton = findViewById(R.id.turnOnButton);
Button getVisibleDevicesButton =
findViewById(R.id.getVisibleDevicesButton);
Button listDevicesButton =
findViewById(R.id.listDevicesButton);
Button turnOffButton = findViewById(R.id.turnOffButton);

ListView deviceListView = findViewById(R.id.deviceListView);


deviceListAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, new ArrayList<>());
deviceListView.setAdapter(deviceListAdapter);

// Set onClickListeners for buttons


turnOnButton.setOnClickListener(v -> turnOnBluetooth());
getVisibleDevicesButton.setOnClickListener(v ->
getVisibleDevices());
listDevicesButton.setOnClickListener(v -> listDevices());
turnOffButton.setOnClickListener(v -> turnOffBluetooth());
}

private void requestBluetoothPermissions() {


if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_CONNECT}, 1);
}
}

private void turnOnBluetooth() {


if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}

if (!bluetoothAdapter.isEnabled()) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED)
{
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH}, 3);
return;
}
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
showToast("Bluetooth is already turned on");
}
}

private void getVisibleDevices() {


if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}

if (!bluetoothAdapter.isEnabled()) {
showToast("Bluetooth is not turned on");
return;
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 4);
return;
}
// Request discoverable mode for 300 seconds
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURAT
ION, 300);
startActivity(discoverableIntent);
}
private void listDevices() {
if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}
if (!bluetoothAdapter.isEnabled()) {
showToast("Bluetooth is not turned on");
return;
}
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 4);
return;
}
Set<android.bluetooth.BluetoothDevice> pairedDevices =
bluetoothAdapter.getBondedDevices();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 5);
return;
}
if (pairedDevices.size() > 0) {
deviceListAdapter.clear();

for (android.bluetooth.BluetoothDevice device :


pairedDevices) {
deviceListAdapter.add(device.getName() + "\n" +
device.getAddress());
}
} else {
showToast("No paired devices found");
}
}
private void turnOffBluetooth() {
if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}

if (bluetoothAdapter.isEnabled()) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 2);
return;
}
bluetoothAdapter.disable();
showToast("Bluetooth turned off");
} else {
showToast("Bluetooth is already turned off");
}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
// Handle runtime permissions
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);

if (requestCode == 1) {
// Check if the permissions were granted
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED
&& grantResults.length > 1 && grantResults[1] ==
PackageManager.PERMISSION_GRANTED) {
// Permissions granted, continue with your Bluetooth
operations
} else {

// Permissions denied, handle accordingly (e.g., show


a message to the user)
showToast("Bluetooth permissions are required for this
app to function.");
}
}
}
}

You might also like