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

Practical No 24

The document outlines an Android layout and Java code for a Bluetooth management application. It includes XML layout elements such as buttons for turning Bluetooth on/off, making it visible, and listing paired devices, along with corresponding Java methods to handle these actions. The code also incorporates permission checks for Bluetooth operations and displays paired devices in a TextView.

Uploaded by

Roshan Kumar
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)
19 views4 pages

Practical No 24

The document outlines an Android layout and Java code for a Bluetooth management application. It includes XML layout elements such as buttons for turning Bluetooth on/off, making it visible, and listing paired devices, along with corresponding Java methods to handle these actions. The code also incorporates permission checks for Bluetooth operations and displays paired devices in a TextView.

Uploaded by

Roshan Kumar
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_height="wrap_content"

droid:text="Turn Off"
Name:-Roshan kumar
droid:layout_below="@id/btn_list_devices"
<?xml version="1.0" encoding="utf-8"?> android:layout_centerHorizontal="true"
<RelativeLayout android:layout_marginTop="20dp" />
xmlns:android="http://schemas.android.com/apk/res/
android" <!-- Output TextView to show List of Devices -->
android:layout_width="match_parent" <TextView
android:layout_height="match_parent"> android:id="@+id/tv_output"
android:layout_width="wrap_content"
<!--Bluetooth Image --> android:layout_height="wrap_content"
<ImageView android:text=""
android:id="@+id/bluetooth_image" android:layout_below="@id/btn_turn_off"
android:layout_width="150dp" android:layout_marginTop="20dp"
android:layout_height="150dp" android:layout_centerHorizontal="true" />
android:src="@drawable/blutooth" </RelativeLayout>
android:layout_centerHorizontal="true"
Mainactivity.java file:
android:layout_marginTop="50dp" />
package com.example.hello;
<!-- Turn On Button -->
<Button import android.bluetooth.BluetoothAdapter;
android:id="@+id/btn_turn_on" import android.bluetooth.BluetoothDevice;
android:layout_width="wrap_content" import android.content.Intent;
android:layout_height="wrap_content" import android.content.pm.PackageManager;
android:text="Turn On" import android.os.Bundle;
android:layout_below="@id/bluetooth_image" import android.widget.Button;
android:layout_centerHorizontal="true" import android.widget.TextView;
android:layout_marginTop="20dp" /> import android.widget.Toast;

<!-- Get Visible Button --> import androidx.appcompat.app.AppCompatActivity;


<Button import androidx.core.app.ActivityCompat;
android:id="@+id/btn_get_visible"
android:layout_width="wrap_content" import java.util.Set;
android:layout_height="wrap_content"
android:text="Get Visible" public class practical_no_24 extends
android:layout_below="@id/btn_turn_on" AppCompatActivity {
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" /> BluetoothAdapter bluetoothAdapter;
TextView tvOutput;
<!-- List Devices Button --> Button btnTurnOn, btnGetVisible, btnListDevices,
<Button btnTurnOff;
android:id="@+id/btn_list_devices"
android:layout_width="wrap_content" @Override
android:layout_height="wrap_content" protected void onCreate(Bundle
android:text="List Devices" savedInstanceState) {
android:layout_below="@id/btn_get_visible" super.onCreate(savedInstanceState);
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" /> setContentView(R.layout.activity_practical_no24);

<!-- Turn Off Button --> bluetoothAdapter =


<Button BluetoothAdapter.getDefaultAdapter();
android:id="@+id/btn_turn_off" tvOutput = findViewById(R.id.tv_output);
android:layout_width="wrap_content" btnTurnOn = findViewById(R.id.btn_turn_on);
btnGetVisible = discoverableIntent.putExtra(BluetoothAdapter.EXTRA_
findViewById(R.id.btn_get_visible); DISCOVERABLE_DURATION, 300);
btnListDevices = startActivity(discoverableIntent);
findViewById(R.id.btn_list_devices); } else {
btnTurnOff = findViewById(R.id.btn_turn_off); Toast.makeText(this, "Please turn on Bluetooth
first", Toast.LENGTH_SHORT).show();
// Button click listeners }
btnTurnOn.setOnClickListener(v -> }
turnOnBluetooth());
btnGetVisible.setOnClickListener(v -> // List paired devices
makeBluetoothVisible()); private void listPairedDevices() {
btnListDevices.setOnClickListener(v -> if (bluetoothAdapter.isEnabled()) {
listPairedDevices()); Set<BluetoothDevice> pairedDevices =
btnTurnOff.setOnClickListener(v -> bluetoothAdapter.getBondedDevices();
turnOffBluetooth()); if (pairedDevices.size() > 0) {
} StringBuilder stringBuilder = new
StringBuilder();
// Turn Bluetooth ON for (BluetoothDevice device : pairedDevices) {
private void turnOnBluetooth() { stringBuilder.append(device.getName())
if (!bluetoothAdapter.isEnabled()) { .append(" - ")
Intent enableBtIntent = new .append(device.getAddress())
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); .append("\n");
if (ActivityCompat.checkSelfPermission(this, }
android.Manifest.permission.BLUETOOTH_CONNECT) tvOutput.setText(stringBuilder.toString());
!= PackageManager.PERMISSION_GRANTED) { } else {
// TODO: Consider calling tvOutput.setText("No paired devices
// ActivityCompat#requestPermissions found.");
// here to request the missing permissions, }
and then overriding } else {
// public void Toast.makeText(this, "Please turn on Bluetooth
onRequestPermissionsResult(int requestCode, String[] first", Toast.LENGTH_SHORT).show();
permissions, }
// int[] grantResults) }
// to handle the case where the user grants
the permission. See the documentation // Turn Bluetooth OFF
// for ActivityCompat#requestPermissions for private void turnOffBluetooth() {
more details. if (bluetoothAdapter.isEnabled()) {
return; if (ActivityCompat.checkSelfPermission(this,
} android.Manifest.permission.BLUETOOTH_CONNECT)
startActivityForResult(enableBtIntent, 1); != PackageManager.PERMISSION_GRANTED) {
} else { // TODO: Consider calling
Toast.makeText(this, "Bluetooth is already ON", // ActivityCompat#requestPermissions
Toast.LENGTH_SHORT).show(); // here to request the missing permissions,
} and then overriding
} // public void
onRequestPermissionsResult(int requestCode, String[]
permissions,
private void makeBluetoothVisible() { // int[] grantResults)
if (bluetoothAdapter.isEnabled()) { // to handle the case where the user grants
Intent discoverableIntent = new the permission. See the documentation
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVER // for ActivityCompat#requestPermissions for
ABLE); more details.
return;
}
bluetoothAdapter.disable();
Toast.makeText(this, "Bluetooth turned off",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Bluetooth is already OFF",
Toast.LENGTH_SHORT).show();
}
}
}

output :
get visible button :

You might also like