How to Make Bluetooth Discoverable to Other Devices in Android?
In android devices, Bluetooth is a communication network protocol that allows devices to connect wirelessly to exchange data with other Bluetooth devices. In general, we can make use of Bluetooth API to implement Bluetooth functionalities, such as enable or disable Bluetooth, searching for available Bluetooth devices, connecting with the devices, and managing the data transfer between devices within the range. In Android, BluetoothAdapter class performs all Bluetooth related activities. By default, and Bluetooth device is in an undiscoverable mode. This means that if we switch on the Bluetooth in a device, it is visible only to those paired earlier with it. This device is invisible on devices other than the paired ones. To make this device visible, we switch on the Discoverable option, which makes the device global to its vicinity. This device now shows up on every list, be it the new devices or the earlier paired ones. Applications involving the pairing of a music device or a digital watch (syncing by Bluetooth transfers) with other devices are quite simple. These devices are set global, and any other device can discover them. To make this thing possible on an Android mobile, one can go to the Bluetooth settings and make it global or discoverable. This article wants to share with you the implementation of an application that makes the device’s Bluetooth discoverable when the Bluetooth is switched on. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.
Implementation
- The application explained in this article is basically to test if we can invoke methods programmatically to fulfill our purpose.
- We created an Android mobile application consisting of three buttons; Turn ON, Discoverable, and Turn OFF, having the functionalities invoke various Bluetooth adapter methods.
- This application’s changes are global, i.e., Bluetooth will continue to be switched on if the Turn OFF button is not clicked, or any other relatable version.
- To make the device discoverable on other devices list, a new activity must be initialized using startActivityForResult(intent, int) with the ACTION_REQUEST_DISCOVERABLE intent.
Note: It is important for the Bluetooth to be present and in a power-on mode to make it discoverable.
Step by Step Implementation
To programmatically show a list of Bluetooth Paired devices against our device in Android, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Woking with the AndroidManifest.xml file
Go to the AndroidManifest.xml file and add these permissions required by the Bluetooth adapter: BLUETOOTH, BLUETOOTH_ADMIN, and ACCESS_COARSE_LOCATION.
<uses-permission android:name=”android.permission.BLUETOOTH”/>
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
Below is the complete code for the AndroidManifest.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "org.w3wiki.bluetoothdiscoverable" > <!--Permissions Required for accessing Bluetooth services--> < uses-permission android:name = "android.permission.BLUETOOTH" /> < uses-permission android:name = "android.permission.BLUETOOTH_ADMIN" /> < uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" /> < 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 > |
Step 3: Working with the activity_main.xml file
Now go to the activity_main.xml file which represents the UI of the application. Create three Buttons, one to switch on the device’s Bluetooth, one to make the device’s Bluetooth discoverable on other devices, and one to switch off the Bluetooth service. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingLeft = "10dp" android:paddingRight = "10dp" > <!--Turn ON Button--> < Button android:id = "@+id/btnOn" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_toLeftOf = "@id/btnDiscoverable" android:text = "Turn ON" /> <!--Discoverable Button--> < Button android:id = "@+id/btnDiscoverable" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:text = "Discoverable" /> <!--Turn OFF Button--> < Button android:id = "@+id/btnOFF" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_toRightOf = "@+id/btnDiscoverable" android:text = "Turn OFF" /> </ RelativeLayout > |
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.bluetooth.BluetoothAdapter import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declare the three buttons from the layout file val btnOn = findViewById<Button>(R.id.btnOn) val btnOff = findViewById<Button>(R.id.btnOFF) val btnDisc = findViewById<Button>(R.id.btnDiscoverable) // Initialize the Bluetooth Adapter val bAdapter = BluetoothAdapter.getDefaultAdapter() // Action when Turn ON Button is clicked btnOn.setOnClickListener { // If Bluetooth support or API is absent or private in the device if (bAdapter == null ) { Toast.makeText(applicationContext, "Bluetooth Not Supported" , Toast.LENGTH_SHORT).show() } else { // Turn ON the Bluetooth using an Intent and making a Toast Message if (!bAdapter.isEnabled) { startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1 ) Toast.makeText(applicationContext, "Bluetooth Turned ON" , Toast.LENGTH_SHORT).show() } } } // Action when Turn OFF Button is clicked btnOff.setOnClickListener { // Disable the Bluetooth Adapter and make a Toast bAdapter!!.disable() Toast.makeText(applicationContext, "Bluetooth Turned OFF" , Toast.LENGTH_SHORT).show() } // Action when Discoverable Button is clicked btnDisc.setOnClickListener { // Make the Bluetooth in a Discovering State and make a Toast if (!bAdapter!!.isDiscovering) { startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 1 ) Toast.makeText(applicationContext, "Making Device Discoverable" , Toast.LENGTH_SHORT).show() } } } } |
Output: Run on Physical Device
The sequence of Input events in the output video are:
- Turn ON click (to turn on the Bluetooth and to check if the button is working)
- Turn OFF click (to turn off the Bluetooth and to check if the button is working)
- Turn ON click (turn on the Bluetooth again)
- Discoverable Click (make the device’s Bluetooth discoverable)
- Turn OFF click (turn off the Bluetooth)
Set Custom Discover Time
By default, the device stays in a Discover mode for 120 seconds. We can change this value by supplying a specific value to the intent using putExtra:
Kotlin
startActivityForResult(Intent (BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE) .putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 40 ) , 1 ) |
This changes the default value to 40 Seconds.