java - 蓝牙启用窗口打开

标签 java android

我刚刚开始学习 Java 和处理蓝牙。要启用蓝牙,我使用以下代码:

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);

如果蓝牙关闭,则在执行此代码时,用户将看到一个窗口,可以在“启用蓝牙”或“取消”之间进行选择。 当用户做出选择并且此窗口关闭时,我需要执行另一个代码。

如何检查此窗口现在是否打开?

我知道有一个“onActivityResult”方法可以在窗口关闭时返回结果,但我肯定不能使用它,所以我正在寻找其他方法。

import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;


public class MyBluetooth {

  private BluetoothManager bluetoothManager;
  private BluetoothAdapter bluetoothAdapter;



  public void Init() {
    bluetoothManager = (BluetoothManager) Context.getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();

    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBtIntent, 1);
    }
  }
}

最佳答案

能否详细说明一下为什么不能使用onActivityResult()? 因为当该窗口关闭时,您将在此处处理应用程序并运行所需的代码。

关于java - 蓝牙启用窗口打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56980325/

相关文章:

android - Google Analytics Android SDK V4 混淆

java - 如何用Java制作一个图 block

java - org.springframework.transaction.CannotCreateTransactionException : Could not open Hibernate Session for transaction

java - 我的项目运行,但有几个错误,例如我需要删除的行

android - AndEngine - SpriteGroup 在新的 attachChild 上闪烁

java - 无法将模拟器与本地主机连接?安卓 |火力基地

java - libgdx/安卓 : graphics disappearing after the app is destroyed/paused

java - 处理程序 postDelayed 执行几次

java - 有没有办法在套接字 channel 上注销选择器

Java读取并显示模板文档,填充GUI变量然后打印输出