简体   繁体   English

在 Android 中启用禁用蓝牙

[英]Enable Disable Bluetooth in Android

我想以编程方式在 android 中启用和禁用蓝牙..请帮助我如何做到这一点..提前致谢。

I know this has been answered but just in case someone else wants this info.我知道这已经得到回答,但以防万一其他人想要这个信息。 There are two ways to enable bluetooth one is to use the Intents and send a user request启用蓝牙有两种方法,一种是使用 Intents 并发送用户请求

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

second is to just call the enable method on the adapter, only use this method if user input is not needed or wanted.第二种是在适配器上调用 enable 方法,仅在不需要或不需要用户输入时才使用此方法。

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

to disable you just call disable method on adapter.要禁用您只需在适配器上调用禁用方法。

您可能想阅读 Android 文档以自己找到答案: http : //developer.android.com/guide/topics/wireless/bluetooth.html

You can directly open Bluetooth by calling function turnOn() on click of button(on):您可以通过单击按钮(on)调用函数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();
}

for Bluetooth enable蓝牙启用

Intent blintent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); Intent blintent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivity(blintent);开始活动(模糊);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM