0% found this document useful (0 votes)
7 views

Bluetooth_OnOff

The document contains an Android application code that manages Bluetooth functionality, including enabling, disabling, and discovering paired devices. It defines a MainActivity class with buttons to control Bluetooth settings and display a list of bonded devices. Additionally, it includes necessary permissions in the AndroidManifest.xml for Bluetooth operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Bluetooth_OnOff

The document contains an Android application code that manages Bluetooth functionality, including enabling, disabling, and discovering paired devices. It defines a MainActivity class with buttons to control Bluetooth settings and display a list of bonded devices. Additionally, it includes necessary permissions in the AndroidManifest.xml for Bluetooth operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package com.example.bluetooth.

myapplication;

import android.app.Activity;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity {

BluetoothAdapter bluetooth;
ListView list;

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

bluetooth = BluetoothAdapter.getDefaultAdapter();
list = findViewById(R.id.listView);

findViewById(R.id.button).setOnClickListener(v -> {
if (!bluetooth.isEnabled()) {
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
Toast.makeText(this, "Bluetooth ON", Toast.LENGTH_SHORT).show();
}
});

findViewById(R.id.button2).setOnClickListener(v -> {
startActivity(new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE));
});

findViewById(R.id.button3).setOnClickListener(v -> {
bluetooth.disable();
Toast.makeText(this, "Bluetooth OFF", Toast.LENGTH_SHORT).show();
});

findViewById(R.id.button4).setOnClickListener(v -> {
ArrayList<String> devices = new ArrayList<>();
for (BluetoothDevice d : bluetooth.getBondedDevices()) {
devices.add(d.getName());
}
list.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, devices));
});
}
}

AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"

You might also like