在Android中启用禁用蓝牙

时间:2010-09-19 08:12:41

标签: android bluetooth

我想以编程方式在android中启用和禁用蓝牙。请帮我解决这个问题。提前谢谢。

4 个答案:

答案 0 :(得分:4)

我知道这已得到解答,但万一其他人想要这些信息。有两种方法可以启用蓝牙,一种是使用Intents并发送用户请求

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(enableIntent, someIntegerValue);

第二种是只调用适配器上的enable方法,如果不需要或不需要用户输入,则只使用此方法。

BluetoothAdapter blue = BluetoothAdapter.getDefaultAdapter();
if (!blue.isEnabled())
    blue.enable();

禁用你只需在适配器上调用disable方法。

答案 1 :(得分:2)

您可能需要阅读Android文档以自行查找答案: http://developer.android.com/guide/topics/wireless/bluetooth.html

答案 2 :(得分:0)

您可以通过点击按钮(开启)调用功能turnOn()直接打开蓝牙:

void turnOn()
{
    if (bluetoothAdapter == null)
    {
        status.setText("BlueTooth adapter not found");
    }
    else if (bluetoothAdapter.isEnabled())
    {
        Toast.makeText(MainActivity.this, "Bluetooth is already on.", Toast.LENGTH_SHORT).show();
    }
    else
        bluetoothAdapter.enable();
}

答案 3 :(得分:0)

启用蓝牙

Intent blintent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivity(blintent);

用于禁用蓝牙 BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.disable();

相关问题