Exp24 MAD
Exp24 MAD
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Turn On" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Get visible to other" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="List all devices" />
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Turn Off" />
<ListView
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="382dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java :-
package com.example.exp24_st1_08;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
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.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.btn1);
b2 = findViewById(R.id.btn2);
b3 = findViewById(R.id.btn3);
b4 = findViewById(R.id.btn4);
l1 = findViewById(R.id.l1);
t1 = findViewById(R.id.textView);
ba = BluetoothAdapter.getDefaultAdapter();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!ba.isEnabled()) {
Intent enableBluetoothIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetoothIntent,
REQUEST_ENABLE_BLUETOOTH);
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already on",
Toast.LENGTH_SHORT).show();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(discoverableIntent, REQUEST_ENABLE_BLUETOOTH);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pair = ba.getBondedDevices();
ArrayList<String> pairedDevicesList = new ArrayList<>();
for (BluetoothDevice device : pair) {
pairedDevicesList.add(device.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, pairedDevicesList);
l1.setAdapter(adapter);
Toast.makeText(MainActivity.this, "Showing paired devices",
Toast.LENGTH_SHORT).show();
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ba.isEnabled()) {
ba.disable();
Toast.makeText(MainActivity.this, "Bluetooth is disabled",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bluetooth is already off",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
if (resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, "Bluetooth is enabled",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Failed to enable Bluetooth",
Toast.LENGTH_SHORT).show();
}
}
}
}
list_text.xml :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
• Output :-