Blog Details

img
Design

How do you use Bluetooth on Android

Spoke Right / 18 Nov, 2023

To exchange data with other devices wirelessly, Bluetooth is used. The Bluetooth framework supported by the Android platform allows a device to send or receive data between two different devices. The Bluetooth API of Android is used to perform these tasks and many more:

To scan Bluetooth devices

To connect and transfer data from and to other devices

To manage multiple connections

Android Bluetooth API:

The interfaces classes to work with Bluetooth are included in the android.bluetooth package. These are:

  • BluetoothAdapter
  • BluetoothDevice
  • BluetoothSocket
  • BluetoothServerSocket
  • BluetoothClass
  • BluetoothProfile
  • BluetoothProfile.ServiceListener
  • BluetoothHeadset
  • BluetoothA2dp
  • BluetoothHealth
  • BluetoothHealthCallback
  • BluetoothHealthAppConfiguration

BluetoothAdapter class:

To perform the fundamental tasks, like to initiate a device discovery, to query a list of paired or bonded devices, to create a BluetoothServerSocket instance to listen for connection requests, etc, the BluetoothAdapter class is used.

Constants of BluetoothAdapter class:

There are various constants provided by the BluetoothAdapter class. Some of these constants are:

  • String ACTION_REQUEST_ENABLE
  • String ACTION_REQUEST_DISCOVERABLE
  • String ACTION_DISCOVERY_STARTED
  • String ACTION_DISCOVERY_FINISHED

Methods of BluetoothAdapter class:

The BluetoothAdapter class contains various methods. Some of the important methods of the BluetoothAdapter class are:

  • static synchronized BluetoothAdapter getDefaultAdapter(): Used to return the instance of BluetoothAdapter.
  • boolean enable(): Used to enable the Bluetooth adapter, if it is disabled.
  • boolean isEnabled(): Used to return true if the Bluetooth adapter is enabled.
  • boolean disable(): Used to disable the Bluetooth adapter, if it is enabled.
  • String getName(): Used to return the name of the Bluetooth adapter.
  • boolean setName(String name): Used to change the Bluetooth name.
  • int getState(): Used to return the current state of the local Bluetooth adapter.
  • Set getBondedDevices(): Used to return a set of paired or bonded BluetoothDevice objects.
  • boolean startDiscovery(): Used to start the discovery process.

Example: To enable, disable, and make discoverable Bluetooth programmatically:

activity_main.xml:

In the activity_main.xml file, we will drag a Textview and three buttons from the pallet.



 
    
 
    
 
 
 
 
 
 
    
 
    
 

AndroidManifest.xml:

In the AndroidManifest.xml file, we will provide the below permissions:

Syntax:


File: AndroidManifest.xml:





 

 

 
    
        
        
    
 

 

    

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to enable, disable, and to make the Bluetooth discoverable.

package com.example.radioapp;
 
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.Button;
import android.widget.ListView;
 
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
 
public class MainActivity extends Activity  {
    Button b1,b2,b3,b4;
    private BluetoothAdapter BA;
    private SetpairedDevices;
    ListView lv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        b1 = (Button) findViewById(R.id.button);
        b2=(Button)findViewById(R.id.button2);
        b3=(Button)findViewById(R.id.button3);
        b4=(Button)findViewById(R.id.button4);
 
        BA = BluetoothAdapter.getDefaultAdapter();
        lv = (ListView)findViewById(R.id.listView);
    }
 
    public void on(View v){
        if (!BA.isEnabled()) {
            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 0);
            Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
        }
    }
 
    public void off(View v){
        BA.disable();
        Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
    }
 
 
    public  void visible(View v){
        Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivityForResult(getVisible, 0);
    }
 
 
    public void list(View v){
        pairedDevices = BA.getBondedDevices();
 
        ArrayList list = new ArrayList();
 
        for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
        Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
 
        final ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
 
        lv.setAdapter(adapter);
    }
}

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0