0% found this document useful (0 votes)
3 views2 pages

Prac 24

The document contains an Android application code that demonstrates Bluetooth functionality. It includes an XML layout file for the main activity with a TextView and a Button to turn Bluetooth on. The Java code handles Bluetooth operations, allowing users to enable or disable Bluetooth and provides feedback through Toast messages.

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)
3 views2 pages

Prac 24

The document contains an Android application code that demonstrates Bluetooth functionality. It includes an XML layout file for the main activity with a TextView and a Button to turn Bluetooth on. The Java code handles Bluetooth operations, allowing users to enable or disable Bluetooth and provides feedback through Toast messages.

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/ 2

prac24

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:layout_marginStart="56dp"
android:layout_marginTop="72dp"
android:text="Bluetooth example"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:text="Turn On"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView"
android:clickable="true"
android:onClick="on" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp24;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
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, b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
ListView lv;

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);
b4 = findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = findViewById(R.id.listView);
}

public void on(View v) {


if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_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 off(View v) {


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

You might also like