【问题标题】:How to receive notification regarding the BT connection states如何接收有关 BT 连接状态的通知
【发布时间】:2015-08-25 10:34:40
【问题描述】:

我正在尝试在连接状态更改为“正在连接、已连接、断开连接、断开连接”时接收通知。我注册了 ACTION_CONNECTION_STATE_CHANGED,但我总是在“switch-case”的“default”语句中收到日志。

请让我知道如何正确接收有关以下代码中 switch-case 中提到的状态的通知?

代码

final int connState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.ERROR);

            switch (connState) {
            case BluetoothAdapter.STATE_CONNECTED:
                Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.STATE_CONNECTED"));
                tvStatus.setText("BT STATE_CONNECTED.");

                break;
            case BluetoothAdapter.:
                Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.STATE_CONNECTED"));
                tvStatus.setText("BT STATE_CONNECTED.");

                break;
            case BluetoothAdapter.STATE_CONNECTING:
                Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.STATE_CONNECTING"));
                tvStatus.setText("BT is STATE_CONNECTING.");
                break;
            case BluetoothAdapter.STATE_DISCONNECTING:
                Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.STATE_DISCONNECTING"));
                tvStatus.setText("BT STATE_DISCONNECTING.");
                break;
            case BluetoothAdapter.STATE_DISCONNECTED:
                Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.STATE_DISCONNECTED"));
                tvStatus.setText("BT STATE_DISCONNECTED.");
                break;
            default:
                Log.wtf(TAG, LogAnd.show("onReceive", "connState: UNHANDELED CASE."));
                break;
            }
            break;

【问题讨论】:

    标签: android android-intent broadcastreceiver intentfilter android-bluetooth


    【解决方案1】:

    你需要

    BluetoothAdapter.EXTRA_STATE
    

    而不是

    BluetoothAdapter.EXTRA_CONNECTION_STATE
    

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 2018-01-17
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多