Practical No 24
Q1.
<?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">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:textSize="24sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn on"
android:onClick="turn_on"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="get_visib"
android:text="Get visible" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List device"
android:onClick="list_d"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn off"
android:onClick="turn_off"/>
<ListView
android:id="@+id/listV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical24;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
BluetoothAdapter bluetooth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void turn_on(View v) {
bluetooth = BluetoothAdapter.getDefaultAdapter();
if (!bluetooth.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
public void get_visib(View v) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 6);
startActivity(intent);
}
public void list_d(View v) {
Set<BluetoothDevice> bluetoothDevices = bluetooth.getBondedDevices();
String [] a = {"Ath","But"};
String[] arr = new String[bluetoothDevices.size()];
int ind = 0;
if (bluetoothDevices.size() > 0) {
for (BluetoothDevice b : bluetoothDevices) {
arr[ind] = b.getName();
ind++;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,arr);
((ListView) findViewById(R.id.listV)).setAdapter(adapter);
}
public void turn_off(View v) {
bluetooth.disable();
}
}