How to on-off the bluetooth progamatically in android:-
Here, we will use the class "BluetoothAdapter" , which allow us to all bluetooth interaction and perform its tasks.
Firstly , we have to set permission in manifest to access device bluetooth.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Now we have to check whether bluetooth is on or off.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean isBluetoothOn = bluetoothAdapter.isEnabled();
A the end you can on or off the bluetooth as per your requirement.
if (!isBluetoothOn) {
bluetoothAdapter.enable();
}
else if(isBluetoothOn) {
bluetoothAdapter.disable();
}
0 Comment(s)