[go: up one dir, main page]

0% found this document useful (0 votes)
8 views4 pages

Practical No 24

The document contains XML and Java code for an Android application that manages Bluetooth functionality, including buttons to turn Bluetooth on/off, list paired devices, and make the device discoverable. It includes layout definitions for buttons and a ListView to display paired devices, as well as methods to handle Bluetooth actions and permissions. The application uses AndroidX libraries and requires specific Bluetooth permissions in the manifest file.

Uploaded by

gavalimanasi1
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)
8 views4 pages

Practical No 24

The document contains XML and Java code for an Android application that manages Bluetooth functionality, including buttons to turn Bluetooth on/off, list paired devices, and make the device discoverable. It includes layout definitions for buttons and a ListView to display paired devices, as well as methods to handle Bluetooth actions and permissions. The application uses AndroidX libraries and requires specific Bluetooth permissions in the manifest file.

Uploaded by

gavalimanasi1
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/ 4

Practical no 24 android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Turn On" />

<!-- List Paired Devices Button -->


<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices" />

<!-- Turn Off Bluetooth Button -->


<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off" />

<!-- Make Device Discoverable Button ->


<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
XML android:text="Make Visible" />
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.Constra <!-- Paired Devices Label -->
intLayout <TextView
android:id="@+id/pairedDevicesText"
xmlns:android="http://schemas.android.c android:layout_width="wrap_content"
om/apk/res/android" android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/ android:layout_margin="10dp"
apk/res-auto" android:text="@string/paired_devices"
xmlns:tools="http://schemas.android.com />
/tools"
android:id="@+id/main" <!-- ListView for Displaying Paired
android:layout_width="match_parent" Devices -->
android:layout_height="match_parent" <ListView
tools:context=".MainActivity" android:id="@+id/listView"
tools:ignore="VisualLintBounds"> android:layout_width="253dp"
android:layout_height="238dp"
<!-- Turn On Bluetooth Button --> android:layout_margin="10dp"
<Button android:layout_marginTop="8dp"
android:id="@+id/b1" />
</androidx.constraintlayout.widget.Constr savedInstanceState) {
aintLayout> super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
Java
package com.example.pr24; setContentView(R.layout.activity_main);
import android.annotation.SuppressLint; ViewCompat.setOnApplyWindowInsetsList
import ener(findViewById(R.id.main), (v, insets) ->
android.bluetooth.BluetoothAdapter; {
import Insets systemBars =
android.bluetooth.BluetoothDevice; insets.getInsets(WindowInsetsCompat.Typ
import android.content.Intent; e.systemBars());
import v.setPadding(systemBars.left,
android.content.pm.PackageManager; systemBars.top, systemBars.right,
import android.os.Build; systemBars.bottom);
import android.os.Bundle; return insets;
import android.widget.ArrayAdapter; });
import android.widget.Button; btOn = findViewById(R.id.b1);
import android.widget.ListView; btList = findViewById(R.id.b2);
import android.widget.TextView; btOff = findViewById(R.id.b3);
import android.widget.Toast; btVisible = findViewById(R.id.b4);
import androidx.activity.EdgeToEdge; listView = findViewById(R.id.listView);
import androidx.annotation.RequiresApi; pairedDevicesText =
import findViewById(R.id.pairedDevicesText);
androidx.appcompat.app.AppCompatActiv BA =
ity; BluetoothAdapter.getDefaultAdapter();
import androidx.core.app.ActivityCompat; if (BA == null) {
import androidx.core.graphics.Insets; Toast.makeText(this, "Bluetooth not
import androidx.core.view.ViewCompat; supported on this device",
import Toast.LENGTH_LONG).show();
androidx.core.view.WindowInsetsCompat; return;
import java.util.ArrayList; }
import java.util.Set; btOn.setOnClickListener(view ->
public class MainActivity extends turnOnBluetooth());
AppCompatActivity { btOff.setOnClickListener(view ->
Button btOn, btOff, btList, btVisible; turnOffBluetooth());
private BluetoothAdapter BA; btList.setOnClickListener(view ->
ListView listView; listPairedDevices());
TextView pairedDevicesText; btVisible.setOnClickListener(view ->
makeDeviceDiscoverable());
@RequiresApi(api = }
Build.VERSION_CODES.S) @SuppressLint("MissingPermission")
@SuppressLint("MissingInflatedId") public void turnOnBluetooth() {
@Override if (!BA.isEnabled()) {
protected void onCreate(Bundle @SuppressLint("MissingPermission")
Intent turnOn = new
Intent(BluetoothAdapter.ACTION_REQUES Toast.makeText(getApplicationContext(),
T_ENABLE); "Bluetooth is already Off",
startActivityForResult(turnOn, 1); Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), }
"Turning Bluetooth On", }
Toast.LENGTH_LONG).show(); public void makeDeviceDiscoverable() {
} else { Intent discoverableIntent = new
Toast.makeText(getApplicationContext(), Intent(BluetoothAdapter.ACTION_REQUES
"Bluetooth is already On", T_DISCOVERABLE);
Toast.LENGTH_LONG).show();
} discoverableIntent.putExtra(BluetoothAda
} pter.EXTRA_DISCOVERABLE_DURATION,
public void turnOffBluetooth() { 300);
if (BA.isEnabled()) { if
if (ActivityCompat.checkSelfPermission(this,
(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH
android.Manifest.permission.BLUETOOTH _ADVERTISE) !=
_CONNECT) != PackageManager.PERMISSION_GRANTED)
PackageManager.PERMISSION_GRANTED) {
{ // TODO: Consider calling
// TODO: Consider calling //
// ActivityCompat#requestPermissions
ActivityCompat#requestPermissions // here to request the missing
// here to request the missing permissions, and then overriding
permissions, and then overriding // public void
// public void onRequestPermissionsResult(int
onRequestPermissionsResult(int requestCode, String[] permissions,
requestCode, String[] permissions, // int[]
// int[] grantResults)
grantResults) // to handle the case where the
// to handle the case where the user grants the permission. See the
user grants the permission. See the documentation
documentation // for
// for ActivityCompat#requestPermissions for
ActivityCompat#requestPermissions for more details.
more details. return;
return; }
} startActivity(discoverableIntent);
BA.disable(); }
Toast.makeText(getApplicationContext(),
"Bluetooth Turned Off", @SuppressLint("SetTextI18n")
Toast.LENGTH_LONG).show(); @RequiresApi(api =
} else { Build.VERSION_CODES.S)
public void listPairedDevices() { "Bluetooth is now enabled",
if Toast.LENGTH_LONG).show();
(ActivityCompat.checkSelfPermission(this, } else {
android.Manifest.permission.BLUETOOTH
_CONNECT) != Toast.makeText(getApplicationContext(),
PackageManager.PERMISSION_GRANTED) "Failed to enable Bluetooth",
{ Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(this, }
new }
String[]{android.Manifest.permission.BLUE }
TOOTH_CONNECT}, 1); }
return; Manifest File
} <uses-permission
Set<BluetoothDevice> pairedDevices android:name="android.permission.BLUET
= BA.getBondedDevices(); OOTH"/>
ArrayList<String> list = new <uses-permission
ArrayList<>(); android:name="android.permission.BLUET
if (!pairedDevices.isEmpty()) { OOTH_ADMIN"/>
pairedDevicesText.setText("Paired <uses-permission
Devices:"); android:name="android.permission.BLUET
for (BluetoothDevice device : OOTH_ADVERTISE" />
pairedDevices) { <uses-permission
list.add(device.getName() + " (" + android:name="android.permission.BLUET
device.getAddress() + ")"); OOTH_CONNECT" />
}
ArrayAdapter<String> adapter =
new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(),
"No Paired Devices Found",
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int
requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,
resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {

Toast.makeText(getApplicationContext(),

You might also like