Android bluetooth can turn on

Android Bluetooth Turn ON or OFF with Examples

In android, Bluetooth is a communication network protocol, which allows 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 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.

Android Enable or Turn On Bluetooth

In android, By using the 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.

Intent eintent = new Intent(BluetoothAdapter. ACTION_REQUEST_ENABLE );
startActivityForResult(eintent, intVal);

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.

Android Disable or Turn OFF Bluetooth

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 as shown below to use Bluetooth features in our android applications.

Android Bluetooth Turn ON / OFF Example

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.

activity_main.xml

< 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» >
< Button
android :id= «@+id/btnOn»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Turn On» android :layout_marginLeft= «100dp» android :layout_marginTop= «200dp»/>
< Button
android :id= «@+id/btnOFF»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_alignBottom= «@+id/btnOn»
android :layout_toRightOf= «@+id/btnOn»
android :text= «Turn OFF»/>

Читайте также:  Подключить bluetooth наушники ipad

Now open your main activity file MainActivity.java from \java\com.tutlane.bluetoothexample path and write the code like as shown below

MainActivity.java

package com.tutlane.bluetoothexample;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity @Override
protected void onCreate(Bundle savedInstanceState) super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button btntOn = (Button)findViewById(R.id. btnOn );
Button btntOff = (Button)findViewById(R.id. btnOFF );
final BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
btntOn.setOnClickListener( new View.OnClickListener() @Override
public void onClick(View v) if ( bAdapter == null )
Toast.makeText(getApplicationContext(), «Bluetooth Not Supported» ,Toast. LENGTH_SHORT ).show();
>
else if (! bAdapter .isEnabled()) startActivityForResult( new Intent(BluetoothAdapter. ACTION_REQUEST_ENABLE ), 1 );
Toast.makeText(getApplicationContext(), «Bluetooth Turned ON» ,Toast. LENGTH_SHORT ).show();
>
>
>
>);
btntOff.setOnClickListener( new View.OnClickListener() @Override
public void onClick(View v) bAdapter .disable();
Toast.makeText(getApplicationContext(), «Bluetooth Turned OFF» , Toast. LENGTH_SHORT ).show();
>
>);
>
>

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

AndroidManifest.xml

If you observe above code, we added required Bluetooth permissions in manifest file to access Bluetooth features in android applications.

Output of Android Bluetooth Turn ON / OFF Example

When we run the above program in the android studio we will get the result as shown below.

Android Bluetooth Turn ON or OFF with Example Result

When we click on Turn ON button, the device Bluetooth gets switched ON and when we click on Turn OFF button, the device Bluetooth gets switched OFF.

This is how we can turn ON / OFF or enable/disable Bluetooth in android applications based on our requirements.

Источник

3 ways to turn on Bluetooth on Android (including Samsung devices)

3 ways to turn on Bluetooth on Android (including Samsung devices)

To connect your smartphone to another device without the use of cables, you must first learn how to turn on Bluetooth on Android. While active, the Android Bluetooth chip available on phones and tablets lets you connect to all kinds of gadgets and accessories, like headsets, speakers, keyboards, other mobile devices, and even PCs. This tutorial illustrates three ways to enable and turn off Bluetooth on Samsung and other Android smartphones and tablets:

Skip to chapter

NOTE: This guide applies to Android 10 and was created using Nokia 5.3 and Samsung Galaxy A51. If you do not know your Android version, read How to check the Android version on your smartphone or tablet. The procedures are similar on most Android-powered devices, although you might come across some small differences, depending on your device’s manufacturer. On Samsung, Bluetooth settings are somewhat different, so make sure to also check out the dedicated Bonus chapter at the end of this guide.

1. Turn on the Android Bluetooth from Quick Settings (includes Samsung Bluetooth)

The fastest way to enable Bluetooth on phone or tablet is from the Android Quick Settings menu. Swipe down from the top of your screen to access it.

Читайте также:  Philips docking speaker with bluetooth

Источник

How to turn on bluetooth on button click

In my application, I need to turn on bluetooth of my device on a button click. How can I achieve that? An example will be really helpful. Also, what permissions I require to include in my mainfest.xml for the same?

3 Answers 3

In the manifest file for permissions:

Source code to enable Bluetooth

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) < // Device does not support Bluetooth >if (!mBluetoothAdapter.isEnabled())

If enabling Bluetooth succeeds, your Activity will receive the RESULT_OK result code in the onActivityResult() callback. If Bluetooth was not enabled due to an error (or the user responded «No») then the result code will be RESULT_CANCELED .

Don’t forget there is another way to turn on bluetooth without using the Intent request. Just call mBluetoothAdapter.enable(); this will turn it on without user popup permission dialog. (only use this way if no user input wanted)

@JPM yes, but 1) this requires BLUETOOTH_ADMIN permission; 2) it’s strongly discouraged in the Android SDK documentation to ever use enable() unless you implement your own UI for user consent or if you’re implementing a system settings manager app or similar.

Turn on Bluetooth in Android it’s not difficult.But you must pay attention to some details.There are 3 kinds of methods to turn on Bluetooth in Android.

In order to get the default bluetooth adapter, ie use BluetoothAdapter.getDefaultAdapter() ; You need this permission:

In order to force to open Bluetooth, ie use BluetoothAdapter.enable() ; You need this permission:

/** * Bluetooth Manager * * @author ifeegoo www.ifeegoo.com * */ public class BluetoothManager < /** * Whether current Android device support Bluetooth. * * @return true:Support Bluetooth false:not support Bluetooth */ public static boolean isBluetoothSupported() < return BluetoothAdapter.getDefaultAdapter() != null ? true : false; >/** * Whether current Android device Bluetooth is enabled. * * @return true:Bluetooth is enabled false:Bluetooth not enabled */ public static boolean isBluetoothEnabled() < BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter != null) < return bluetoothAdapter.isEnabled(); >return false; > /** * Force to turn on Bluetooth on Android device. * * @return true:force to turn on Bluetooth success * false:force to turn on Bluetooth failure */ public static boolean turnOnBluetooth() < BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter != null) < return bluetoothAdapter.enable(); >return false; > > 

The method above to turn on Bluetooth will not tell the user whether you turn on Bluetooth success or not.When you call the method enable(), you will not turn on Bluetooth 100%.Because of the reason of some security apps refuse you to do this and so on.You need to pay attention to the return value of the method enable().

The official api of the method enable() tells us that :

Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a “power manager” app.

Читайте также:  Использовать телефон bluetooth наушники

So it’s not suitable for you to turn on Bluetooth by the method enable() unless you make the user know what you will do.

2.Use System Alert to remind users that you will turn on Bluetooth by startActivityForResult.

this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON) need this permission: public class MainActivity extends Activity < /** * Custom integer value code for request to turn on Bluetooth,it's equal *requestCode in onActivityResult. */ private static final int REQUEST_CODE_BLUETOOTH_ON = 1313; /** * Bluetooth device discovery time,second。 */ private static final int BLUETOOTH_DISCOVERABLE_DURATION = 250; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); if ((BluetoothManager.isBluetoothSupported()) && (!BluetoothManager.isBluetoothEnabled())) < this.turnOnBluetooth(); >> /** * Use system alert to remind user that the app will turn on Bluetooth */ private void turnOnBluetooth() < // request to turn on Bluetooth Intent requestBluetoothOn = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); // Set the Bluetooth discoverable. requestBluetoothOn .setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); // Set the Bluetooth discoverable time. requestBluetoothOn.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BLUETOOTH_DISCOVERABLE_DURATION); // request to turn on Bluetooth this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON); >@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) < if (requestCode == REQUEST_CODE_BLUETOOTH_ON) < switch (resultCode) < // When the user press OK button. case Activity.RESULT_OK: < // TODO >break; // When the user press cancel button or back button. case Activity.RESULT_CANCELED: < // TODO >break; default: break; > > > > 

This is a better way for you to turn on Bluetooth on Android.

3.Guide users to the system Bluetooth settings to turn on Bluetooth by themselves.

// Guide users to system Bluetooth settings to turn on Bluetooth by himselves. this.startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); 

In my app.Considering the code logical and also the user experience, I use the follow steps to turn on Bluetooth:

1.use enable() to turn on Bluetooth.But I will tell the users that I will turn on Bluetooth, if they allow me to do this, I will call enable() to turn on Bluetooth.This is better than call the system alert, because we can control the content to remind user. You must pay attention that it’s not 100% to turn on Bluetooth by enable() , some security apps or other reasons refuse you to do this.But we can estimate that whether we turn on Bluetooth success or not by the return value of the method enable().

2.If the method enable() return false,then we use startActivityForResult to remind users that we will turn on Bluetooth.

3.If the step 2 will not work by some reasons, you can guide the users to go to the system Bluetooth settings to turn on Bluetooth by themselves.

Add more:since Android 6.0 we need to deal with the permission of Bluetooth.

Источник

Оцените статью
Adblock
detector