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

24 Aayush

The document describes an Android program that uses a GUI to control Bluetooth functions like turning Bluetooth on and off, making the device visible, listing paired devices, and turning Bluetooth off. The program uses XML layouts and Java code to build the GUI and handle the Bluetooth functionality when buttons are clicked.

Uploaded by

HARSH MAGHNANI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

24 Aayush

The document describes an Android program that uses a GUI to control Bluetooth functions like turning Bluetooth on and off, making the device visible, listing paired devices, and turning Bluetooth off. The program uses XML layouts and Java code to build the GUI and handle the Bluetooth functionality when buttons are clicked.

Uploaded by

HARSH MAGHNANI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exp24: Write a program to turn on, get visible, list devices and turnoff Bluetooth with the help of

following GUI.

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Exp24:Bluetooth Devices"
android:textSize="18dp"
android:textColor="#0000FF"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:textSize="30dp"
android:layout_marginTop="60dp"
/>
<Button
android:id="@+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:textSize="20dp"
/>
<Button
android:id="@+id/visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Visible"
android:textSize="20dp"
/>
<Button
android:id="@+id/listdevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices"
android:textSize="20dp"
/>
<Button
android:id="@+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"
android:textSize="20dp"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/l1"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PROGRAMMED BY AAYUSH BALIP"
android:textColor="#0000FF"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="35dp"
android:layout_marginEnd="35dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifestxml:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

MainActivity.java:
package com.example.exp24;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter BA;

ListView lv;

private Set<BluetoothDevice>pairedDevices;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BA=BluetoothAdapter.getDefaultAdapter();
Button turnOnBtn = findViewById(R.id.on);
Button getVisibleBtn = findViewById(R.id.visible);
Button listDevicesBtn = findViewById(R.id.listdevices);
Button turnOffBtn = findViewById(R.id.off);
lv=(ListView)findViewById(R.id.l1);
turnOnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!BA.isEnabled()){
Intent turnOn=new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn,0);
Toast.makeText(getApplicationContext(),"Turn
On",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(),"Already
On",Toast.LENGTH_LONG).show();;
}
}
});

getVisibleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent getv=new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getv,0);
}
});

listDevicesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bt : pairedDevices) {
list.add(bt.getName());
}
Toast.makeText(getApplicationContext(),"Showing Paired
Devices",Toast.LENGTH_LONG).show();
ArrayAdapter<String> adpt = new
ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1,
list);
lv.setAdapter(adpt);
}
});

turnOffBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BA.disable();
Toast.makeText(getApplicationContext(),"Turn
Off",Toast.LENGTH_LONG).show(); }
});
}
}
Output:-

You might also like