Write a Program to turn on, turn off, get visible, list visible and turn off Bluetooth with the help of GUI.

 Java Code:

package com.example.myapplication;


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.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.Set;


public class MainActivity extends AppCompatActivity {

private BluetoothAdapter BA;

ListView lv;

private Set<BluetoothDevice>pairedDevices;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        BA=BluetoothAdapter.getDefaultAdapter();

        lv=(ListView)findViewById(R.id.l1);

    }

    public void on(View v){

        if(!BA.isEnabled()){

            Intent turnOn=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

            startActivityForResult(turnOn,0);

            Toast.makeText(getApplicationContext(),"Turn 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(),"Turn Off",Toast.LENGTH_LONG).show();

    }

    public void getvisible(View v)  {

            Intent getv=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

            startActivityForResult(getv,0);

    }

    public void getlist(View v){

       pairedDevices=BA.getBondedDevices();

        ArrayList list=new ArrayList();

        for(BluetoothDevice bt:pairedDevices){

            list.add(bt.getName());

   }

        Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_LONG).show();

        ArrayAdapter adpt=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);

        lv.setAdapter(adpt);

    }

}


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:orientation="vertical"

    tools:context=".MainActivity">


    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">


    <Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="On"

    android:textSize="30dp"

    android:onClick="on"/>


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Off"

        android:textSize="30dp"

        android:onClick="off"/>


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Visible"

        android:textSize="30dp"

        android:onClick="getvisible"/>


    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="List"

        android:textSize="30dp"

        android:onClick="getlist"/>

   </LinearLayout>


    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Bluetooth Devices"

        android:textSize="50dp"/>


<ListView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:id="@+id/l1"/>

</LinearLayout>


Manifest.xml File:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.myapplication">

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

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

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>



Output:

Original View:                               After Clicking “ON” Button:

           


After Clicking “OFF” Button:     After Clicking “VISIBLE” Button:    After Clicking “List” Button:


         


Comments

Popular posts from this blog

Write a program to create a first display screen on any search engine using auto complete text View

Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Write a program to create a login form for social networking website.