In this tutorial, we will learn to turn ON, get visible, list devices and turn OFF Bluetooth with the help of given GUI
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rcppp.bluetoothapp">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:onClick="turnOn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Visible"
android:onClick="getVisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices"
android:onClick="listDevices"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Off"
android:onClick="turnOff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired Devices"
android:textColor="@android:color/holo_red_dark"/>
<ListView android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.bluetoothapp;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends Activity {
private ListView listView;
private BluetoothAdapter ba;
private Set<BluetoothDevice> pairedDevices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ba = BluetoothAdapter.getDefaultAdapter();
listView = findViewById(R.id.listView);
}
public void turnOn(View v) {
if(!ba.isEnabled()) {
Intent intentOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intentOn, 0);
Toast.makeText(getApplicationContext(), "Turned ON", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Already ON", Toast.LENGTH_SHORT).show();
}
}
public void turnOff(View v) {
ba.disable();
Toast.makeText(getApplicationContext(), "Turned OFF", Toast.LENGTH_SHORT).show();
}
public void getVisible(View v) {
Intent intentVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intentVisible, 0);
}
public void listDevices(View v) {
pairedDevices = ba.getBondedDevices();
ArrayList<String> list = new ArrayList<String>();
for(BluetoothDevice b : pairedDevices) {
list.add(b.getName());
}
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
}
}