Mobile Application Development Practical 24

Question 1
Write a program to turn on, get visible, list devices and turnoff Bluetooth with the help of following GUI.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:layout_marginLeft="20dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/heading"
        android:text="Bluetooth"
        android:textSize="40dp"
        android:textStyle="bold"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Turn On"
        android:id="@+id/btnon"
        android:background="#A19A9A"/>

    <Button
        android:id="@+id/btnvisible"
        android:layout_width="118dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#A19A9A"
        android:text="Get Visible" />

    <Button
        android:id="@+id/btnlist"
        android:layout_width="118dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#A19A9A"
        android:text="List Devices" />

    <Button
        android:id="@+id/btnoff"
        android:layout_width="105dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#A19A9A"
        android:text="Turn Off" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:id="@+id/list"
        android:text="List of devices"
        android:textSize="40dp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/devlist"/>

</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Practical24"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
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.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    Button b1,b2,b3,b4;
    TextView tvlist;
    BluetoothAdapter ba;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=findViewById(R.id.btnon);
        b2=findViewById(R.id.btnvisible);
        b3=findViewById(R.id.btnlist);
        b4=findViewById(R.id.btnoff);
        tvlist=findViewById(R.id.devlist);
        ba=BluetoothAdapter.getDefaultAdapter();

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ba.isEnabled())
                    Toast.makeText(getApplicationContext(),"Bluetooth already on",Toast.LENGTH_LONG).show();
                else
                {
                    Intent ion=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(ion,22);
                    Toast.makeText(getApplicationContext(),"Bluetooth turned on",Toast.LENGTH_LONG).show();
                }
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent vi=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(vi,22);
            }
        });

        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ba.disable();
                Toast.makeText(getApplicationContext(),"Bluetooth turned off",Toast.LENGTH_LONG).show();
            }
        });

        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String list = "";
                Set<BluetoothDevice> devices = ba.getBondedDevices();
                if (devices.size() > 0) {
                    BluetoothDevice bd[]=(BluetoothDevice[]) devices.toArray();
                    //for (BluetoothDevice bd : devices)
                    for(int i=0;i< bd.length;i++){
                        list=list+"\n"+bd[i].getName();
                    }
                    tvlist.setText(list);
                }
                else
                    tvlist.setText("No device found");
            }
        });
    }
}
Output
Screenshot_20230325_113558
Screenshot_20230325_113621
Screenshot_20230325_113635
Screenshot_20230325-112104

Leave a Comment

Your email address will not be published. Required fields are marked *