In android, Bluetooth is a communication network protocol, which allow a devices to connect wirelessly to exchange the data with other Bluetooth devices.
Generally, in android applications by using Bluetooth API’s we can implement Bluetooth functionalities, such as enable or disable a Bluetooth, searching for available Bluetooth devices, connecting with the devices and managing the data transfer between devices within the range.
In android, we can perform Bluetooth related activities by using BluetoothAdapter class in our applications. To know more about BluetoothAdapter, check this Android Bluetooth with Examples.
In android, By using startActivityForResult() method with ACTION_REQUEST_ENABLE intent action parameter we can enable or turn on Bluetooth in our android applications.
Following is the code snippet to enable a Bluetooth by using BluetoothAdapter parameter ACTION_REQUEST_ENABLE.
If you observe above code snippet, we used startActivityForResult() method with ACTION_REQUEST_ENABLE intent action parameter to enable a Bluetooth. The second parameter intVal is a locally defined integer that must be greater than 0.
In android, we can disable or turn off Bluetooth just by invoking a BluetoothAdapter method disable().
Following is the code snippet to disable or turn off Bluetooth in android applications using disable() function.
As we discussed in previous tutorial Android Bluetooth with Examples, we need to set Bluetooth permissions in our android manifest file like show below to use Bluetooth features in our android applications.
<manifest ... >
Following is the example of turning on or off Bluetooth on button click in android applications.
Create a new android application using android studio and give names as BluetoothExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.
Now open your main activity file MainActivity.java from \java\com.tutlane.bluetoothexample path and write the code like as shown below
If you observe above code, we used startActivityForResult() method with BluetoothAdapter action parameter ACTION_REQUEST_ENABLE intent to enable or disable a Bluetooth in our application.
As discussed, we need to set Bluetooth permissions in android manifest file (AndroidManifest.xml) to access Bluetooth features in android applications. Now open android manifest file (AndroidManifest.xml) and write the code like as shown below
If you observe above code, we added required Bluetooth permissions in manifest file to access Bluetooth features in android applications.
When we run above program in android studio we will get the result like as shown below.
When we click on Turn ON button, the device Bluetooth get switched ON and when we click on Turn OFF button, the device Bluetooth get switched OFF.
This is how we can Turn ON / OFF or enable / disable Bluetooth in android applications based on our requirements.