Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to enable or disable the Bluetooth inside the physical device.
You will learn how to use the Android BluetoothAdapter API to be able to control the Bluetooth connection.
Here are the steps:
1- Open up Android Studio.
2- Declare and initialize Android BluetoothAdapter like this: bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
3- First, you need to check if the device has a Bluetooth capability like this:
if (bluetoothAdapter == null) {
Toast.makeText(MainActivity.this, "This device doesn't support Bluetooth",
Toast.LENGTH_LONG).show();
}
4- Next, you need to check the current status of the Bluetooth connection using bluetoothAdapter.isEnabled() and change the label of the Android Button based on the status. If the Bluetooth is ON, the button label will show "Turn Bluetooth OFF", and if the Bluetooth is OFF, the button label will show "Turn Bluetooth ON".
if (!bluetoothAdapter.isEnabled()) {
buttonBlue.setText("Turn Bluetooth ON");
} else {
buttonBlue.setText("Turn Bluetooth OFF");
}
5- Inside the button Onclicklistener you also need to check the current Bluetooth status to turn the Bluetooth ON or OFF as well as to change the label of the button.
6- To enable the Bluetooth, you will need to use Android Intent and startActivityForResult like this:
Intent bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(bluetoothIntent, 1);
7- Next, you need to use onActivityResult to know whether the user has given or rejected the Bluetooth permission. Based on the result, if the permission was granted, it will show a Toast message and will change the label of the button. Otherwise, it will show another Toast message indicating that the user has canceled the permission.
if (resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, "Bluetooth is ON", Toast.LENGTH_SHORT).show();
buttonBlue.setText("Turn Bluetooth OFF");
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(MainActivity.this, "Bluetooth operation is cancelled",
Toast.LENGTH_SHORT).show();
}
8- To disable Bluetooth, you need to use bluetoothAdapter like this: bluetoothAdapter.disable();
9- Make sure you add the following permissions inside Androidmanifest.xml file. These permissions will allow you to control the Bluetooth connection on your physical device.
android:name="android.permission.BLUETOOTH"
android.permission.BLUETOOTH_ADMIN
10- Now build and run the app to see the output :)
Links:
Android BluetoothAdapter API: https://developer.android.com/guide/t...
Android emulator limitations:
https://developer.android.com/studio/...
Website: https://www.codingdemos.com
FaceBook: / codingdemos
En esta página del sitio puede ver el video en línea Android Enable and Disable Bluetooth Programmatically de Duración hora minuto segunda en buena calidad , que subió el usuario Coding Demos 06 junio 2020, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 16,249 veces y le gustó 110 a los espectadores. Disfruta viendo!