如何在android简单代码的基础上自动启用蓝牙?

如何在android简单代码的基础上自动启用蓝牙?,android,Android,这就是我试图使用的代码。你能告诉我它有什么问题吗 我的代码: public void onStart() { super.onStart(); if(D) Log.e(TAG, "++ ON START ++"); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()) {

这就是我试图使用的代码。你能告诉我它有什么问题吗

我的代码:

    public void onStart() {
    super.onStart();
    if(D) Log.e(TAG, "++ ON START ++");
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          if (!mBluetoothAdapter.isEnabled()) {

          }else{
               mBluetoothAdapter.enable(); 
          } ;
    } {
        if (mChatService == null) setupChat();

}

你有什么问题?我的猜测是,您需要将BLUETOOTH和BLUETOOTH_管理权限添加到您的应用程序中

请注意,首选的解决方案是使用意图提示用户是否要启用蓝牙:

        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableIntent);
还要注意,对
启用的调用是异步的,它将立即返回并在后台启用蓝牙。因此,蓝牙可能尚未真正启用,它可能仍在准备中。另见

编辑添加了禁用/等待/启用/等待示例代码

这是请求蓝牙关闭,然后等待它打开的示例代码。它必须在单独的线程中运行,而不是在UI线程中运行。可以将其封装在
Runnable
类中,如果成功完成,该类会将(最好是
volatile
)标志设置为true

注意:已知此示例代码在一些不允许从用户应用程序调用diable/enable的旧设备上存在问题

            BluetoothAdapter.getDefaultAdapter().disable();
            while (BluetoothAdapter.getDefaultAdapter().isEnabled())
            {
                try
                {
                    Thread.sleep(100L);
                }
                catch (InterruptedException ie)
                {
                    // unexpected interruption while disabling Bluetooth
                    Thread.currentThread().interrupt(); // restore interrupted flag
                    return;
                }
            }
            // disabled, re-enabling Bluetooth
            BluetoothAdapter.getDefaultAdapter().enable();
            while (!BluetoothAdapter.getDefaultAdapter().isEnabled())
            {
                try
                {
                    Thread.sleep(100L);
                }
                catch (InterruptedException ie)
                {
                    // unexpected interruption while enabling bluetooth
                    Thread.currentThread().interrupt(); // restore interrupted flag
                    return;
                }
            }

如何根据简单的代码自动打开蓝牙而无需用户请求?@DanielTang如我所说,请确保您在清单中同时拥有
bluetooth
bluetooth\u ADMIN
权限,然后您可以调用`mBluetoothAdapter.enable()
,但是,如果您想立即使用蓝牙,则需要等待
mBluetoothAdapter.isEnabled()`为真,这可能需要几秒钟。我确信我同时拥有蓝牙和蓝牙管理权限。您的意思是我可以取消mBluetoothAdapter.isEnabled()以非常快速地启用蓝牙??正确,一旦您请求禁用或启用蓝牙,它将花费相同的时间。我已在上面的解决方案中添加了此等待的示例代码。在您的代码中,它会自动打开手机上的蓝牙,但在连接其他蓝牙设备时仍有错误。
public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
        if (mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.disable(); 
        } 
        else(!mBluetoothAdapter.isEnabled()) {
             mBluetoothAdapter.enable(); 
        }
    }
}