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

Practical 9.1

The document contains an Android application code that manages Bluetooth functionality. It includes an XML layout file for the user interface with a TextView and a ToggleButton, and a Java class that initializes the Bluetooth adapter and updates the UI based on the Bluetooth status. Permissions for Bluetooth access are also specified in the AndroidManifest.xml file.

Uploaded by

Pruthesh Upadhye
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)
8 views3 pages

Practical 9.1

The document contains an Android application code that manages Bluetooth functionality. It includes an XML layout file for the user interface with a TextView and a ToggleButton, and a Java class that initializes the Bluetooth adapter and updates the UI based on the Bluetooth status. Permissions for Bluetooth access are also specified in the AndroidManifest.xml file.

Uploaded by

Pruthesh Upadhye
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/ 3

9.1 activity_main.

xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">

<TextView
android:id="@+id/tvStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth Status: Unknown"
android:textSize="20sp"
android:textStyle="bold"
android:paddingBottom="20dp"/>

<ToggleButton
android:id="@+id/toggleBluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Bluetooth ON"
android:textOff="Bluetooth OFF"/>
</LinearLayout>

9.1 AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
9.1 MainActivity.java

package com.example.practical7;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ToggleButton toggleBluetooth;


private TextView tvStatus;
private BluetoothAdapter bluetoothAdapter;

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

// Initialize views
toggleBluetooth = findViewById(R.id.toggleBluetooth);
tvStatus = findViewById(R.id.tvStatus);

// Get Bluetooth adapter


bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Check if device supports Bluetooth


if (bluetoothAdapter == null) {
tvStatus.setText("Bluetooth is not supported on this device.");
toggleBluetooth.setEnabled(false);
return;
}

// Set initial Bluetooth status


if (bluetoothAdapter.isEnabled()) {
toggleBluetooth.setChecked(true);
tvStatus.setText("Bluetooth Status: ON");
} else {
toggleBluetooth.setChecked(false);
tvStatus.setText("Bluetooth Status: OFF");
}

// Toggle Button Click Listener


toggleBluetooth.setOnCheckedChangeListener((buttonView, isChecked) ->
{
if (isChecked) {
// Turn ON Bluetooth
bluetoothAdapter.enable();
tvStatus.setText("Bluetooth Status: ON");
} else {
// Turn OFF Bluetooth
bluetoothAdapter.disable();
tvStatus.setText("Bluetooth Status: OFF");
}
});
}
}

9.1 Output

You might also like