0% found this document useful (0 votes)
14 views3 pages

Bluetooth program

The document contains an XML layout for a Bluetooth application with buttons to enable Bluetooth, discover devices, and connect to a device. The MainActivity.java file implements the functionality for these buttons using the BluetoothAdapter and includes methods for enabling Bluetooth and discovering devices. Additionally, the manifest file specifies the necessary Bluetooth permissions for the application.

Uploaded by

solankesnehal96k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Bluetooth program

The document contains an XML layout for a Bluetooth application with buttons to enable Bluetooth, discover devices, and connect to a device. The MainActivity.java file implements the functionality for these buttons using the BluetoothAdapter and includes methods for enabling Bluetooth and discovering devices. Additionally, the manifest file specifies the necessary Bluetooth permissions for the application.

Uploaded by

solankesnehal96k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Bluetooth program

Xml code
<?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=".MainActivity"
android:orientation="vertical"
>

<Button
android:id="@+id/btnEnableBluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Bluetooth"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>

<Button
android:id="@+id/btnDiscoverDevices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discover Devices"
android:layout_below="@id/btnEnableBluetooth"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btnConnectToDevice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect to Device"
android:layout_below="@id/btnDiscoverDevices"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

</LinearLayout>

Mainactivity.java
package com.example.uefclass;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.UUID;

public class MainActivity extends AppCompatActivity {


private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket btSocket;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-
1000-8000-00805F9B34FB");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Button btnEnableBluetooth =
findViewById(R.id.btnEnableBluetooth);
Button btnDiscoverDevices =
findViewById(R.id.btnDiscoverDevices);
Button btnConnectToDevice =
findViewById(R.id.btnConnectToDevice);

btnEnableBluetooth.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
enableBluetooth();
}
});

btnDiscoverDevices.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
discoverDevices();
}
});

btnConnectToDevice.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
connectToDevice();
}
});
}

private void enableBluetooth() {


if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}

if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
} else {
showToast("Bluetooth is already enabled");
}
}

private void discoverDevices() {


if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}

if (!bluetoothAdapter.isEnabled()) {
showToast("Enable Bluetooth first");
return;
}

Intent discoverDevicesIntent = new Intent(MainActivity.this,


DeviceListActivity.class);
startActivity(discoverDevicesIntent);
}

private void connectToDevice() {


// Implement Bluetooth device connection logic here
// You may need to create a separate activity for device list and
connection
}

private void showToast(String message) {


Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}
}

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

You might also like