最佳答案
我有一个应用程序,上面有一个按钮,我用它来打开和关闭 BT。里面有以下代码
public void buttonFlip(View view) {
flipBT();
buttonText(view);
}
public void buttonText(View view) {
Button buttonText = (Button) findViewById(R.id.button1);
if (mBluetoothAdapter.isEnabled() || (mBluetoothAdapter.a)) {
buttonText.setText(R.string.bluetooth_on);
} else {
buttonText.setText(R.string.bluetooth_off);
}
}
private void flipBT() {
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
} else {
mBluetoothAdapter.enable();
}
}
我调用按钮 Flip,它会翻转 BT 状态,然后调用 ButtonText,它会更新 UI。然而,我所面临的问题是,BT 需要几秒钟才能打开——在这几秒钟内,BT 状态没有启用,使我的按钮显示蓝牙关闭,即使它将在2秒内打开。
我在 BluetoothAdapter android 文档中找到了 STATE_CONNECTING
常量,但是... ... 作为一个新手,我根本不知道如何使用它。
我有两个问题: