Exp 24
Exp 24
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/turnOnButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/getVisibleDevicesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Visible Devices"
android:layout_below="@id/turnOnButton"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/listDevicesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices"
android:layout_below="@id/getVisibleDevicesButton"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/turnOffButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"
android:layout_below="@id/listDevicesButton"
android:layout_marginTop="16dp"/>
<ListView
android:id="@+id/deviceListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/turnOffButton"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:padding="8dp"
android:text="Developed by Shreya Shukla(60)"
android:textStyle="bold" />
</RelativeLayout>
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exp24_1);
// Initialize BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Initialize UI elements
Button turnOnButton = findViewById(R.id.turnOnButton);
Button getVisibleDevicesButton =
findViewById(R.id.getVisibleDevicesButton);
Button listDevicesButton =
findViewById(R.id.listDevicesButton);
Button turnOffButton = findViewById(R.id.turnOffButton);
if (!bluetoothAdapter.isEnabled()) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED)
{
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH}, 3);
return;
}
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
showToast("Bluetooth is already turned on");
}
}
if (!bluetoothAdapter.isEnabled()) {
showToast("Bluetooth is not turned on");
return;
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 4);
return;
}
// Request discoverable mode for 300 seconds
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURAT
ION, 300);
startActivity(discoverableIntent);
}
private void listDevices() {
if (bluetoothAdapter == null) {
showToast("Bluetooth not supported on this device");
return;
}
if (!bluetoothAdapter.isEnabled()) {
showToast("Bluetooth is not turned on");
return;
}
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 4);
return;
}
Set<android.bluetooth.BluetoothDevice> pairedDevices =
bluetoothAdapter.getBondedDevices();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 5);
return;
}
if (pairedDevices.size() > 0) {
deviceListAdapter.clear();
if (bluetoothAdapter.isEnabled()) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {
// Request the missing permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.BLUETOOTH_CONNECT}, 2);
return;
}
bluetoothAdapter.disable();
showToast("Bluetooth turned off");
} else {
showToast("Bluetooth is already turned off");
}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
// Handle runtime permissions
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == 1) {
// Check if the permissions were granted
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED
&& grantResults.length > 1 && grantResults[1] ==
PackageManager.PERMISSION_GRANTED) {
// Permissions granted, continue with your Bluetooth
operations
} else {