Tech Blog

Android

Discover,enable and disable Bluetooth in Android programmatically

By Yashi Verma2 min read
← Back to blog

Before learning how to discover and enable Bluetooth in Android let’s know a few things about Bluetooth.

Android Platform includes Bluetooth framework support that allows the device to wirelessly exchange data with other Bluetooth devices

Android provides the Bluetooth API to perform these various functions.

  • Scan other Bluetooth devices
  • Fetch a list of paired devices
  • Connect to the other devices 

Let’s get started with the code :

Give necessary permissions within the app’s manifest –

The BLUETOOTH permission permits the app to connect, disconnect, and transfer data with another Bluetooth device.

The BLUETOOTH_ADMIN permission permits the app to discover new Bluetooth devices and change the device’s Bluetooth settings.

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

Create an object of Bluetooth adapter to interface with Bluetooth –

We can communicate with Bluetooth using the BluetoothAdapter class provided by Android . If the adapter returns null, this implies Bluetooth is not supported in the current device. 

private BluetoothAdapter mObjBTAdapter;
mObjBTAdapter = BluetoothAdapter.getDefaultAdapter();

Discover Nearby BT Devices:

1.create a BroadcastReceiver for ACTION_FOUND

private final BroadcastReceiver mObjBlueToothReceiver = new BroadcastReceiver() {
   public void onReceive(Context pObjContext, Intent pObjIntent) {
       static final String BLUETOOTH_ACTION = pObjIntent.getAction();
       if (BluetoothDevice.ACTION_FOUND.equals(BLUETOOTH_ACTION)) {
           // BT device found. Get the BluetoothDevice
           // information from the Intent.
           BluetoothDevice deviceInfo = pObjIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            static final String DEVICE_NAME = deviceInfo.getName();
            static final String MAC_ADDRESS = deviceInfo.getAddress(); 
       }
   }
};

2. register the BroadcastReceiver

 IntentFilter btFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  registerReceiver(mObjBlueToothReceiver, btFilter);

3. call startDiscovery to discover nearby BT devices

mObjBTAdapter.startDiscovery(); 

4. unregister the BroadcastReceiver inside onDestroy

@Override
protected void onDestroy() {
   super.onDestroy();
   ...
   // unregistering the receiver for ACTION_FOUND 
   unregisterReceiver(mObjBlueToothReceiver);
}

Enable Bluetooth:

We can use the provided code snippet to turn ON  the Bluetooth –

We used startActivityForResult() method with ACTION_REQUEST_ENABLE intent action parameter to enable Bluetooth. The second parameter requestCode is a locally defined integer that must be greater than 0.

 if (!mObjBTAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, requestCode);
        }
OR
if (!mObjBTAdapter.isEnabled()){ mObjBTAdapter.enable(); }

Disable Bluetooth:

We can use the provided code snippet to turn OFF the Bluetooth –

if (mObjBTAdapter.isEnabled()){  mObjBTAdapter.disable(); }

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →