【问题标题】:Detecting state changes made to the BluetoothAdapter?检测对蓝牙适配器所做的状态更改?
【发布时间】: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 常量,但是......我只是不知道如何使用它,作为一个新手等等。

所以,我有两个问题:

  1. 有没有办法将 UI 元素(如按钮或图像)动态绑定到 BT 状态,这样当 BT 状态发生变化时,按钮也会发生变化?
  2. 否则,我想按下按钮并获得正确的状态(我想让它说 BT 开启,即使它只是连接,因为它将在 2 秒内开启)。我该怎么做?

【问题讨论】:

标签: java android bluetooth


【解决方案1】:

您需要注册一个BroadcastReceiver 来监听BluetoothAdapter 状态的任何变化:

作为 Activity 中的私有实例变量(或在单独的类文件中......您喜欢哪个):

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                 BluetoothAdapter.ERROR);
            switch (state) {
            case BluetoothAdapter.STATE_OFF:
                setButtonText("Bluetooth off");
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                setButtonText("Turning Bluetooth off...");
                break;
            case BluetoothAdapter.STATE_ON:
                setButtonText("Bluetooth on");
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                setButtonText("Turning Bluetooth on...");
                break;
            }
        }
    }
};

请注意,这假定您的 Activity 实现了一个方法 setButtonText(String text),该方法将相应地更改 Button 的文本。

然后在您的Activity 中,注册和注销BroadcastReceiver,如下所示,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* ... */

    // Register for broadcasts on BluetoothAdapter state change
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();

    /* ... */

    // Unregister broadcast listeners
    unregisterReceiver(mReceiver);
}

【讨论】:

  • Alex,这几乎行得通。一些东西。首先,我认为您的意思是注册 BluetoothAdapter,而不是 BluetoothDevice,因为 BluetoothDevice 是在您配对设备时,它只有一个 BluetoothDevice.ACTION_BOND_STATE_CHANGED,此处不适用。我还添加了一个;在broadcastReceiver代码之后(我只是为将来遇到相同问题并阅读此内容的人编写此代码),但是,我收到“Java.lang.RuntimeException:无法实例化活动ComponentInfo”,这通常意味着我的活动未在清单中注册 - 我需要在其中注册接收器吗?
  • 哈!我是个白痴 :) 我发现了我的问题。所以,是的,除了我上面提到的代码之外,这很好用:) 谢谢亚历克斯!
  • 没问题!抱歉打错了……当我打字的时候,我知道一定有几个:)。
  • 我个人认为 Alex 的答案是迄今为止最好的答案。请记住,您也可以在 AndroidManifest.xml 中注册广播接收器。请注意您放入广播接收器中的代码,因为它有时会在您的应用程序实例创建之前执行,至少在我个人的经验中是这样。
  • 您应该在 onStart 和 onStop 中注册和取消注册。不保证会调用 onDestroy!
【解决方案2】:
public void discoverBluetoothDevices(View view)
    {
        if (bluetoothAdapter!=null)

            bluetoothAdapter.startDiscovery();
            Toast.makeText(this,"Start Discovery"+bluetoothAdapter.startDiscovery(),Toast.LENGTH_SHORT).show();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多