Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Imtiaz

Saifullah

Android Guide
with
Imtiaz Saifullah
Email: Imtiazsaifullah.m@gmail.com
FB: facebook.com/ImtiazSaif.Khan
Ping/Text: +92-331-7370872
Imtiaz
Saifullah

Bluetooth Paired Devices


Imtiaz
Saifullah Bluetooth Paired Devices
Android Bluetooth List Paired Devices

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list.

// Get paired devices.


Set<BluetoothDevice> pairedDevices = bAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    // There are paired devices. Get the name and address of each paired device.
    for (BluetoothDevice device : pairedDevices) {
        String deviceName = device.getName();
        String deviceHardwareAddress = device.getAddress(); // MAC address
    }
}

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah Bluetooth Paired Devices
we need to set Bluetooth permissions in our android manifest file like show below to use Bluetooth features in our
android applications.

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

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah Bluetooth Paired Devices
Open activity_main.xml file from \res\layout folder path and write the code like as shown below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnGet"
        android:text="Get Paired Devices"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="200dp" />
    <ListView
        android:id="@+id/deviceList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah Bluetooth Paired Devices
open your main activity file MainActivity.java and write the code like as shown below
public class MainActivity extends AppCompatActivity {
private ListView lstvw;
private ArrayAdapter aAdapter;
private BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btnGet);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bAdapter==null){
Toast.makeText(getApplicationContext(),"Bluetooth Not Supported",Toast.LENGTH_SHORT).show();
}
else{
Set<BluetoothDevice> pairedDevices = bAdapter.getBondedDevices();
ArrayList list = new ArrayList();
if(pairedDevices.size()>0){
for(BluetoothDevice device: pairedDevices){
String devicename = device.getName();
String macAddress = device.getAddress();
list.add("Name: "+devicename+"MAC Address: "+macAddress);
}
lstvw = (ListView) findViewById(R.id.deviceList);
aAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
lstvw.setAdapter(aAdapter);
} } } }); } }

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah

You might also like