我刚刚开始学习 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/