如何在Android中以编程方式启用/禁用蓝牙


104

我想通过程序启用/禁用蓝牙。我有以下代码。

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

但是此代码在SDK 1.5中不起作用。我该如何运作?


它怎么不起作用?您遇到错误了吗?如果是这样,错误是什么?
亚当·德里斯科尔

1
蓝牙
适配器

Answers:



164

这段代码对我有用。

//Disable bluetooth
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.disable(); 
} 

为此,您必须具有以下权限:

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

它也确实对我有用。在android设备中断开蓝牙连接的简单方法。非常感谢哥们。
阿米特·塔珀

7
如果添加BLUETOOTH_ADMIN权限有效,但如果没有,则需要使用startActivityForResult(enableBtIntent,0); 启用蓝牙
Majid Golshadi 2014年

1
感谢您有用的答案+1。只是我想为谁不知道如何启用它添加一个:mBluetoothAdapter.enable()
Chris Sim

97

这是一种更健壮的方法,也可以处理enable()\disable()方法的返回值:

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;
}

并将以下权限添加到清单文件中:

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

但是请记住以下要点:

这是一个异步调用:它将立即返回,并且客户端应侦听ACTION_STATE_CHANGED,以便随后的适配器状态更改得到通知。如果此调用返回true,则适配器状态将立即从STATE_OFF转换为STATE_TURNING_ON,并在一段时间后转换为STATE_OFF或STATE_ON。如果此调用返回false,则存在直接问题,该问题将阻止打开适配器-例如“飞行模式”,或者适配器已经打开。

更新:

好了,那么如何实现蓝牙监听器呢?

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                 BluetoothAdapter.ERROR);
            switch (state) {
            case BluetoothAdapter.STATE_OFF:
                // Bluetooth has been turned off;
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                // Bluetooth is turning off;
                break;
            case BluetoothAdapter.STATE_ON:
                // Bluetooth is on
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                // Bluetooth is turning on
                break;
            }
        }
    }
};

以及如何注册/注销接收者?(在您的Activity课堂上)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ...

    // Register for broadcasts on BluetoothAdapter state change
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

@Override
public void onStop() {
    super.onStop();

     // ...

    // Unregister broadcast listeners
    unregisterReceiver(mReceiver);
}

1
如果您添加了BLUETOOTH_ADMIN权限,它就可以了,但是如果没有,您需要使用它startActivityForResult(enableBtIntent, 0);来启用蓝牙
Majid Golshadi 2014年

1
突出显示的信息是从BluetoothAdapter文档中引用的,专门针对enable()方法。
Kevin Lee)

嘿,文档说那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.是什么意思?对于前。我用您的代码制作了一个小应用程序,它可以正常工作。但是,如果我要上传到Play商店,它将无法正常工作?
希拉尔

@Hilal它将起作用。但是用户需要在安装前征得同意。他们将看到这样的对话框:pewinternet.org/2015/11/10/…–
Caner

23

要启用蓝牙,您可以使用以下功能之一:

 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);
        }
  }

第二个功能是:

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

不同之处在于,第一个功能使应用程序向用户询问打开蓝牙或拒绝蓝牙的权限。第二个功能使该应用程序直接打开蓝牙。

要禁用蓝牙,请使用以下功能:

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

注意:第一个功能仅需要在AndroidManifest.xml文件中定义以下权限:

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

同时,第二个和第三个功能需要以下权限:

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

我认为参数(视图)不是必需的
CitrusO2 '19

6

prijin的解决方案非常适合我。公平地说,还需要两个附加权限:

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

添加这些功能后,启用和禁用默认蓝牙适配器即可正常工作。


4

当我的应用启动并正常运行时,我使用以下代码禁用了BT。不知道这是否是实现Google的正确方法,建议不要使用“ bluetooth.disable();”。无需明确的用户操作即可关闭蓝牙。

    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
    bluetooth.disable();

我只使用了以下权限。

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

2

将以下权限添加到清单文件中:

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

启用蓝牙使用此

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (!mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.enable(); 
}else{Toast.makeText(getApplicationContext(), "Bluetooth Al-Ready Enable", Toast.LENGTH_LONG).show();}

禁用蓝牙使用此

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.disable(); 
}

0

试试这个:

//this method to check bluetooth is enable or not: true if enable, false is not enable
public static boolean isBluetoothEnabled()
    {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {
            // Bluetooth is not enable :)
            return false;
        }
        else{
            return true;
        }

    }

//method to enable bluetooth
    public static void enableBluetooth(){
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable();
        }
    }

//method to disable bluetooth
    public static void disableBluetooth(){
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.disable();
        }
    }

在清单中添加这些权限

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.