0% found this document useful (0 votes)
12 views3 pages

Prac 24

The document contains an Android application that demonstrates Bluetooth functionality, including turning Bluetooth on and off, and listing paired devices. The layout is defined in XML with buttons for user interaction and a ListView to display devices. The Java code implements the logic for Bluetooth operations and user interface interactions in the MainActivity class.

Uploaded by

ankitasurya663
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)
12 views3 pages

Prac 24

The document contains an Android application that demonstrates Bluetooth functionality, including turning Bluetooth on and off, and listing paired devices. The layout is defined in XML with buttons for user interaction and a ListView to display devices. The Java code implements the logic for Bluetooth operations and user interface interactions in the MainActivity class.

Uploaded by

ankitasurya663
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/ 3

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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth example" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:onClick="on" />

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

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

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

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp24;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
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 {


Button b1, b2, b3;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
ListView lv;

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

b1 = findViewById(R.id.button);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
lv = findViewById(R.id.listView);
BA = BluetoothAdapter.getDefaultAdapter();
}

public void on(View v) {


if (!BA.isEnabled()) {
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
0);
Toast.makeText(getApplicationContext(), "Turned On",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already On",
Toast.LENGTH_LONG).show();
}
}

public void off(View v) {


BA.disable();
Toast.makeText(getApplicationContext(), "Turned Off", Toast.LENGTH_LONG).show();
}

public void list(View v) {


pairedDevices = BA.getBondedDevices();
ArrayList<String> list = new ArrayList<>();
for (BluetoothDevice bt : pairedDevices) {
list.add(bt.getName());
}
Toast.makeText(getApplicationContext(), "Show Paired Devices",
Toast.LENGTH_LONG).show();
ArrayAdapter adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp24">

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

<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/Theme.Exp24">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

You might also like