0% found this document useful (0 votes)
13 views6 pages

Activit 1

The document describes an Android app that demonstrates Bluetooth functionality. The app contains XML layout files and Java code to turn Bluetooth on/off, get visible, list paired devices, and more using buttons. It also includes the necessary permissions and app components in the Android manifest.

Uploaded by

aryanbattu04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Activit 1

The document describes an Android app that demonstrates Bluetooth functionality. The app contains XML layout files and Java code to turn Bluetooth on/off, get visible, list paired devices, and more using buttons. It also includes the necessary permissions and app components in the Android manifest.

Uploaded by

aryanbattu04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name:Aryan battu Roll no:49

Practical no:24

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/t ools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:transitionGroup="true"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="Bluetooth"
android:textSize="35dp" />
<Space
android:layout_width="match_parent"
android:layout_height="23dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:clickable="true"
android:onClick="on"
android:text="Turn On" />
<Button
Name:Aryan battu Roll no:49
Practical no:24

android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:onClick="visible"
android:text="Get visible" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:onClick="list"
android:text="List devices" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:onClick="off"
android:text="turn off" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="Paired devices:"
android:textColor="@color/black"
android:textSize="25dp" />
<ListView
Name:Aryan battu Roll no:49
Practical no:24

android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java
package com.example.ex2401;
import androidx.appcompat.app.AppCompatActiv ity;
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 java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4;

private BluetoothAdapter BA;


private
Set<BluetoothDevice>pairedDevices;
ListView lv;
public void off(View v){
BA.disable();
Toast.makeText(getApplicationContext(),
Name:Aryan battu Roll no:49
Practical no:24

}
"Turned off"
,Toast.LENGTH_LONG).show();
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
BA=BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}
public void on(View v){ if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQU EST_ENABLE);
startActivityForResult(turnOn, 0);

Toast.makeText(getApplicationContext(),
"Turned on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Already on",
Toast.LENGTH_LONG).show();
}
}
public void visible(View v){
Intent getVisible =new
Intent(BluetoothAdapter.ACTION_REQU EST_DISCOVERABLE);
Name:Aryan battu Roll no:49
Practical no:24

startActivityForResult(getVisible, 0);
}
public void list(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_SHORT).show(
);
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple
_list_item_1, list);
lv.setAdapter(adapter);
}
}

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.co m/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
package="com.example.ex2401">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUE TOOTH_CONNECT" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Name:Aryan battu Roll no:49
Practical no:24

android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.EX2401">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>

OUTPUT:

You might also like