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

Bluetooth

The document outlines a program for managing Bluetooth connectivity in an Android application. It includes a user interface with buttons to turn Bluetooth on, make the device discoverable, and turn Bluetooth off, along with the corresponding Java code to implement these functionalities. The program uses the BluetoothAdapter class to handle Bluetooth operations and provides feedback to the user via Toast messages.

Uploaded by

Sakshi Kale
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)
12 views2 pages

Bluetooth

The document outlines a program for managing Bluetooth connectivity in an Android application. It includes a user interface with buttons to turn Bluetooth on, make the device discoverable, and turn Bluetooth off, along with the corresponding Java code to implement these functionalities. The program uses the BluetoothAdapter class to handle Bluetooth operations and provides feedback to the user via Toast messages.

Uploaded by

Sakshi Kale
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

Unit-5

Develop a program for providing bluetooth connectivity.


ON, OFF, Discoverable (V.V.IMP)

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth On"
android:id="@+id/turnOn">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discoverable"
android:id="@+id/discover">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth Off"
android:id="@+id/turnOff">
</Button>
</LinearLayout>

package com.example.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

BY VIKAS PATIL
Button turnOn, turnOff, discover ;
int REQUEST_ENABLE_BT = 0;
int REQUEST_DISCOVER_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
turnOff=findViewById(R.id.turnOff);
turnOn=findViewById(R.id.turnOn);
discover=findViewById(R.id.discover);
BluetoothAdapter mBluetooth=BluetoothAdapter.getDefaultAdapter();

turnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mBluetooth.isEnabled()) {
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT);
Toast.makeText(MainActivity.this, "Bluetooth Turned On",
Toast.LENGTH_SHORT).show();
}
}
});
discover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mBluetooth.isDiscovering()) {
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE),
REQUEST_DISCOVER_BT);
Toast.makeText(MainActivity.this, "Device Discoverable",
Toast.LENGTH_SHORT).show();
}
}
});
turnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mBluetooth.isEnabled()) {
mBluetooth.disable();
Toast.makeText(MainActivity.this, "Bluetooth Turned Off",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

BY VIKAS PATIL

You might also like