Kurarin Jan Maikeru
Kurarin Jan Maikeru

Reputation: 21

Turn on bluetooth automatically when application is open

I'm creating a project which receives data from an arduino device. When I launches the application, it sends a confirmation if I want to Turn On my bluetooth. My goal is that, when I launches the application, it will automatically turn ON the bluetooth in the device bypassing the confirmation phase. Is this possible???

Upvotes: 0

Views: 5418

Answers (3)

Vinay Agrahar
Vinay Agrahar

Reputation: 33

Add these below lines in oncreate method,it will turn on the bluetooth once you open the app

BluetoothAdapter BA=BluetoothAdapter.getDefaultAdapter(); BA.enable();

Upvotes: 0

prakash
prakash

Reputation: 109

Once the Activity started means, For bluetooth enabling function , you can give following code in onStart or onCreate method

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
if (! mBtAdapter.isEnabled()) {
    mBtAdapter.enable(); 
}

and also add the following permissions

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Upvotes: 0

Ritesh Gune
Ritesh Gune

Reputation: 16729

In the first activity or the base activity you can do as follows:

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 

if (! mBtAdapter.isEnabled()) {
    mBtAdapter.enable(); 
}

Provide following permissions in your manifest files.

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Note: IMHO it is a good practice to ask user for confirmation before turning the bluetooth on.

Upvotes: 9

Related Questions