0% found this document useful (0 votes)
12 views2 pages

Bluetooth_PairedDevices

The document contains an Android application layout defined in XML and its corresponding Java activity. The layout includes a button to list paired Bluetooth devices, a text view, and a list view to display the device names. The Java code initializes the Bluetooth adapter, sets up a button click listener, and retrieves and displays the names of paired Bluetooth devices in the list view.
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)
12 views2 pages

Bluetooth_PairedDevices

The document contains an Android application layout defined in XML and its corresponding Java activity. The layout includes a button to list paired Bluetooth devices, a text view, and a list view to display the device names. The Java code initializes the Bluetooth adapter, sets up a button click listener, and retrieves and displays the names of paired Bluetooth devices in the list view.
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/ 2

activity_main.

xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Paired Devices"
android:onClick="list" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"/>

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

MainActivity.java

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

public class MainActivity extends Activity {

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();


ListView listView = findViewById(R.id.listView);

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

Button b1 = findViewById(R.id.button1);
b1.setOnClickListener(v -> listPairedDevices());
}

private void listPairedDevices() {


Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();
ArrayList<String> deviceNames = new ArrayList<>();

for (BluetoothDevice device : pairedDevices) {


deviceNames.add(device.getName());
}

listView.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, deviceNames));
Toast.makeText(this, "Showing Paired Devices", Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml

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


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

You might also like