0% found this document useful (0 votes)
19 views5 pages

Practical No - 12

The document describes an Android program that implements Bluetooth connectivity. It contains XML layout and Java code to turn Bluetooth on/off, make the device discoverable, list paired devices, and display them in a 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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Practical No - 12

The document describes an Android program that implements Bluetooth connectivity. It contains XML layout and Java code to turn Bluetooth on/off, make the device discoverable, list paired devices, and display them in a 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 PDF, TXT or read online on Scribd
You are on page 1/ 5

“Program To Implement Bluetooth Connectivity”

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="turn on"
android:id="@+id/btnOn"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btnVisible"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="get Discoverable"
android:layout_below="@+id/btnOn"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btnList"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="list Devices"
android:layout_below="@+id/btnVisible"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btnOff"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Off"
android:layout_below="@+id/btnList"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<ListView
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/btnOn"
android:layout_alignLeft="@+id/btnOn"
android:layout_below="@+id/text2"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnOff"
android:text="Bluetooth devices are : "
android:layout_marginTop="40dp"
android:layout_marginLeft="40dp"
/>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


Button bOn, bVisible, bOff, bList;
ListView lv;
BluetoothAdapter BA;
Set<BluetoothDevice> pairedDevice;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bOn = findViewById(R.id.btnOn);
bVisible = findViewById(R.id.btnVisible);
bOff = findViewById(R.id.btnOff);
bList = findViewById(R.id.btnList);

BA = BluetoothAdapter.getDefaultAdapter();
lv = findViewById(R.id.l1);
bOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this, new
String[]{android.Manifest.permission.BLUETOOTH}, 100);
}
if (!BA.isEnabled()) {
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(i);

Toast.makeText(MainActivity.this, "Bluethooth Turned On",


Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bluethooth already On",
Toast.LENGTH_SHORT).show();
}
}
});
bVisible.setOnClickListener(new View.OnClickListener() {
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(i);
}
});
bList.setOnClickListener(new View.OnClickListener() {
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
pairedDevice = BA.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bt : pairedDevice) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing PairedDevice",
Toast.LENGTH_SHORT).show();

ArrayAdapter ad = new ArrayAdapter(MainActivity.this,


android.R.layout.simple_list_item_1, list);
lv.setAdapter(ad);
}
});

bOff.setOnClickListener(new View.OnClickListener() {
@SuppressLint("MissingPermission")
@Override
public void onClick(View v) {
BA.disable();
Toast.makeText(MainActivity.this, "Turned Off", Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy