Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

MainActivity.java turnOffBtn.

setOnClickListener(new
package com.example.myapplication; View.OnClickListener() {
@SuppressLint("MissingPermission")
import android.annotation.SuppressLint; @Override
import android.bluetooth.BluetoothAdapter; public void onClick(View v) {
import android.content.Intent; if (bluetoothAdapter.isEnabled()) {
import android.os.Bundle; bluetoothAdapter.disable();
import android.view.View; Toast.makeText(MainActivity.this, "Bluetooth turned
import android.widget.Button; off", To
import android.widget.Toast; ast.LENGTH_SHORT).show();
} else {
import androidx.appcompat.app.AppCompatActivity; Toast.makeText(MainActivity.this, "Bluetooth
is
public class MainActivity extends AppCompatActivity { already off", Toast.LENGTH_SHORT).show();
}
private BluetoothAdapter bluetoothAdapter; }
});
@Override
protected void onCreate(Bundle savedInstanceState) discoverableBtn.setOnClickListener(new
{ View.OnClickListener() {
super.onCreate(savedInstanceState); @SuppressLint("MissingPermission")
setContentView(R.layout.activity_main); @Override
public void onClick(View v) {
bluetoothAdapter = Intent discoverableIntent = new Intent(Bluetoot
BluetoothAdapter.getDefaultAdapter(); hAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(Bluetooth
Button turnOnBtn = findViewById(R.id.turnOnBtn); Adapter.EXTRA_DISCOVERABLE_DURATION, 300
Button turnOffBtn = findViewById(R.id.turnOffBtn); startActivity(discoverableIntent);
Button discoverableBtn = }
findViewById(R.id.discoverableBtn); });
Button showPairedDevicesBtn =
findViewById(R.id.showPairedDevicesBtn); showPairedDevicesBtn.setOnClickListener(new
View.
turnOnBtn.setOnClickListener(new OnClickListener() {
View.OnClickListener() { @Override
@SuppressLint("MissingPermission") public void onClick(View v) {
@Override
public void onClick(View v) { Toast.makeText(getApplicationContext(),"Device N
if (!bluetoothAdapter.isEnabled()) { ot found",Toast.LENGTH_LONG).show();
Intent enableBtIntent = new }
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); });
startActivity(enableBtIntent); }
} else { }
Toast.makeText(MainActivity.this, "Bluetooth
is already on", Toast.LENGTH_SHORT).show();
}
}
});
turnOffBtn.setOnClickListener(new
View.OnClickListener() {
Activity_main.xml
<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="16dp">

<Button
android:id="@+id/turnOnBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn On Bluetooth" />

<Button
android:id="@+id/turnOffBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn Off Bluetooth" />

<Button
android:id="@+id/discoverableBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make Discoverable" />

<Button
android:id="@+id/showPairedDevicesBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Paired Devices" />

</LinearLayout>

You might also like