【发布时间】:2012-03-13 23:26:35
【问题描述】:
我有一个应用程序,上面有一个按钮,用于打开和关闭 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 常量,但是......我只是不知道如何使用它,作为一个新手等等。
所以,我有两个问题:
- 有没有办法将 UI 元素(如按钮或图像)动态绑定到 BT 状态,这样当 BT 状态发生变化时,按钮也会发生变化?
- 否则,我想按下按钮并获得正确的状态(我想让它说 BT 开启,即使它只是连接,因为它将在 2 秒内开启)。我该怎么做?
【问题讨论】:
-
你不能使用布尔值并在BT开始打开后将其设置为true,然后检查布尔状态以更改按钮吗?
-
查看此 AOSP 代码,了解他们如何跟踪蓝牙开/关变化:androidxref.com/5.1.0_r1/xref/frameworks/base/services/core/…