打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to enable/disable bluetooth programmatically in android

HI all,

I want to enable/disable bluetooth through the program..I has the following code.

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    if (!mBluetoothAdapter.isEnabled()) {        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

But this sort of code is not working in SDK 1.5..How can i do the same in SDK 1.5.?

asked Sep 27 '10 at 18:07
user458295
2543611

    
How is it not working? Are you getting an error? If so what is the error? –  Adam Driscoll Sep 27 '10 at 18:12
    
BluetoothAdapter is showing error in SDK 1.5 –  user458295 Sep 27 '10 at 18:34

5 Answers

up vote 16 down vote accepted

Android BluetoothAdapter docs say it has been available since API Level 5. API Level 5 is Android 2.0.

You can try using a backport of the Bluetooth API (have not tried it personally): http://code.google.com/p/backport-android-bluetooth/

answered Sep 27 '10 at 19:10
James Schek
12.5k13357

this code worked for me..

//Disable bluetoothBluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    if (mBluetoothAdapter.isEnabled()) {    mBluetoothAdapter.disable(); } 
answered Jun 8 '11 at 9:09
prijin
7781815

    
it really works for me also. simple method to disconnect the bluetooth in android devices. thanks a lot buddy. –  Android Developer Jun 14 '11 at 6:39
2  
if you add BLUETOOTH_ADMIN permission it's work but if not you need to use startActivityForResult(enableBtIntent, 0); to enable your bluetooth –  Majid Golshadi Jan 11 '14 at 14:17
    
Thanks for your useful answer +1 . just I want to add for who doesn't know how to enable it: mBluetoothAdapter.enable() –  Chris Sim Apr 21 at 13:45

Here is a bit more robust way of doing this, also handling the return values of enable()\disable() methods:

public static boolean setBluetooth(boolean enable) {    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    boolean isEnabled = bluetoothAdapter.isEnabled();    if (enable && !isEnabled) {        return bluetoothAdapter.enable();     }    else if(!enable && isEnabled) {        return bluetoothAdapter.disable();    }    // No need to change bluetooth state    return true;}

And add the following permissions into your manifest file:

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

But remember these important points:

This is an asynchronous call: it will return immediately, and clients should listen for ACTION_STATE_CHANGED to be notified of subsequent adapter state changes. If this call returns true, then the adapter state will immediately transition from STATE_OFF to STATE_TURNING_ON, and some time later transition to either STATE_OFF or STATE_ON. If this call returns false then there was an immediate problem that will prevent the adapter from being turned on - such as Airplane mode, or the adapter is already turned on.

answered Nov 22 '13 at 10:47
Caner
19.6k1576109

1  
if you add BLUETOOTH_ADMIN permission it's work but if not you need to use startActivityForResult(enableBtIntent, 0); to enable your bluetooth –  Majid Golshadi Jan 11 '14 at 14:16
    
perfect solution –  Hiren Patel Apr 19 '14 at 9:32
    
thnks for the addition highlighted information –  Rahul Rastogi Feb 24 at 11:55

The solution of prijin worked perfectly for me. It is just fair to mention that two additional permissions are needed:

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

When these are added, enabling and disabling works flawless with the default bluetooth adapter.

answered Oct 25 '13 at 19:04
Peter Osburg
131118

To Enable the Bluetooth you could use either of the following functions:

 public void enableBT(View view){    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    if (!mBluetoothAdapter.isEnabled()){        Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);         // The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult()         // implementation as the requestCode parameter.         int REQUEST_ENABLE_BT = 1;        startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT);        }  }

The second function is:

public void enableBT(View view){    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    if (!mBluetoothAdapter.isEnabled()){        mBluetoothAdapter.enable();    }}

The difference is that the first function makes the app ask the user a permission to turn on the Bluetooth or to deny. The second function makes the app turn on the Bluetooth directly.

To Disable the Bluetooth use the following function:

public void disableBT(View view){    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    if (mBluetoothAdapter.isEnabled()){        mBluetoothAdapter.disable();    }}

NOTE/ The first function needs only the following permission to be defined in the AndroidManifest.xml file:

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

While, the second and third functions need the following permissions:

<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
BLE简介和Android BLE编程
Android之Bluetooth
[转载]蓝牙4.0
Android BLE与终端通信(二)——Android Bluetooth基础搜索蓝牙设备显示列表
关于Android 5.x的低功耗蓝牙BLE开发简介
Android蓝牙BluetoothDevice类使用指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服