Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Task 14 Bluetooth

Name: Aniket Sarang Shekokar

• Code:-

1. Bluetooth.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"
tools:context=".BluetoothActivity"
android:orientation="vertical"
android:background="@drawable/background_shape">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:textSize="28dp"
android:textColor="@color/white"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:layout_marginTop="40dp"/>

<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:src="@drawable/logo"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_bluetooth_turn_on"
android:layout_weight="1"
android:text="Turn On"
android:textStyle="bold"
android:textColor="@color/yellow"
android:textSize="16sp"
android:backgroundTint="@color/white"
android:layout_margin="4dp"
android:fontFamily="sans-serif-condensed"
android:onClick="turnOn"/>

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_bluetooth_get_visible"
android:layout_weight="1"
android:text="Get Visible"
android:textColor="@color/yellow"
android:textSize="16sp"
android:textStyle="bold"
android:backgroundTint="@color/white"
android:layout_margin="4dp"
android:fontFamily="sans-serif-condensed"
android:onClick="getVisible"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_bluetooth_paired_devices"
android:layout_weight="1"
android:text="Paired Devices"
android:textColor="@color/yellow"
android:textStyle="bold"
android:textSize="16sp"
android:backgroundTint="@color/white"
android:layout_margin="4dp"
android:fontFamily="sans-serif-condensed"
android:onClick="pairedDevices"/>

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btn_bluetooth_turn_off"
android:layout_weight="1"
android:text="Turn Off"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/yellow"
android:backgroundTint="@color/white"
android:layout_margin="4dp"
android:fontFamily="sans-serif-condensed"
android:onClick="turnOff"/>

</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Paired Devices list"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:layout_margin="20dp"/>

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_bluetooth_paired_devices"/>

</LinearLayout>

2. Bluetooth activity.java:--

package com.example.aniketsapp;

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

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Set;

public class BluetoothActivity extends AppCompatActivity {

Button btn_turnOn,btn_getVisible,btn_pairedDevices,btn_turnOff;
ListView lv_paired_devices;
BluetoothAdapter bluetoothAdapter;
Set<BluetoothDevice> bluetoothDevices;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);

btn_turnOn = findViewById(R.id.btn_bluetooth_turn_on);
btn_getVisible = findViewById(R.id.btn_bluetooth_get_visible);
btn_pairedDevices = findViewById(R.id.btn_bluetooth_paired_devices);
btn_turnOff = findViewById(R.id.btn_bluetooth_turn_off);
lv_paired_devices = findViewById(R.id.lv_bluetooth_paired_devices);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

@SuppressLint("MissingPermission")
public void turnOn(View view) {
if (!bluetoothAdapter.isEnabled())
{
Intent intent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,456);
}
else
{
Toast.makeText(this, "Your Bluetooth is already on",
Toast.LENGTH_SHORT).show();
}
}

@SuppressLint("MissingPermission")
public void getVisible(View view) {
Intent intent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent,000);
}

@SuppressLint("MissingPermission")
public void pairedDevices(View view) {
bluetoothDevices = bluetoothAdapter.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bd : bluetoothDevices)
{
list.add(bd.getName());
ArrayAdapter adapter = new
ArrayAdapter(BluetoothActivity.this,android.R.layout.simple_dropdown_item_1li
ne,list);
lv_paired_devices.setAdapter(adapter);
}
}

@SuppressLint("MissingPermission")
public void turnOff(View view) {
if (bluetoothAdapter.isEnabled())
{
bluetoothAdapter.disable();
}
else
{
Toast.makeText(this, "Your Bluetooth is already off",
Toast.LENGTH_SHORT).show();
}
}
}

3. Android manifest:--
4. <?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-feature
android:name="android.hardware.telephony"
android:required="false" />

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

<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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.aniketsapp"
tools:targetApi="31">
<activity
android:name=".BluetoothActivity"
android:exported="false" />
<!--
TODO: Before you run your application, you need a Google
Maps API key.

To get one, follow the directions here:

https://developers.google.com/maps/documentation/android-sdk/get-api-
key

Once you have your API key (it starts with "AIza"), define
a new property in your
project's local.properties file (e.g.
MAPS_API_KEY=Aiza...), and replace the
"YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBvAj8Gpg9Q4y-E4JLDROcG9dhCmXOSHN0" />

<activity
android:name=".MyLocationActivity"
android:exported="false"
android:label="@string/title_activity_my_location" />
<activity
android:name=".ContactusActivity"
android:exported="false" />
<activity
android:name=".AboutusActivity"
android:exported="false" />
<activity
android:name=".SettingActivity"
android:exported="false" />
<activity
android:name=".MyprofileActivity"
android:exported="false" />
<activity
android:name=".HomeActivity"
android:exported="false" />
<activity
android:name=".RegistrationActivity"
android:exported="false" />
<activity
android:name=".LoginActivity"
android:exported="false" />
<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>

Output:---

You might also like