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

Practical No 24

The document contains code for an Android app that allows the user to control Bluetooth functionality on their device. It includes layout XML with buttons to turn Bluetooth on/off, get the device visible to others, and list paired devices. The MainActivity code handles the button clicks by accessing the BluetoothAdapter to enable/disable Bluetooth, make the device discoverable, get a list of paired devices, and display that list in a ListView.

Uploaded by

atharvabutte03
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)
571 views2 pages

Practical No 24

The document contains code for an Android app that allows the user to control Bluetooth functionality on their device. It includes layout XML with buttons to turn Bluetooth on/off, get the device visible to others, and list paired devices. The MainActivity code handles the button clicks by accessing the BluetoothAdapter to enable/disable Bluetooth, make the device discoverable, get a list of paired devices, and display that list in a ListView.

Uploaded by

atharvabutte03
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

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();
}
}

You might also like