android - CONNECTION_STATE_CHANGED 和 STATE_CHANGED 之间的区别

标签 android bluetooth

Android 蓝牙接收器中的 Action CONNECTION_STATE_CHANGED 和 STATE_CHANGED 有什么区别?

            else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED .equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                    BluetoothAdapter.STATE_DISCONNECTED);
                if (state == BluetoothAdapter.STATE_CONNECTED) {
                    //nothing
                } else {

                }
        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            if (state == BluetoothAdapter.STATE_OFF) {

            }
        }

最佳答案

根据docs区别如下:

ACTION_CONNECTION_STATE_CHANGED

Intent 用于将本地蓝牙适配器的连接状态更改广播到远程设备的配置文件。

ACTION_STATE_CHANGED

本地蓝牙适配器的状态已更改。例如,蓝牙已打开或关闭。

换句话说,一个 Intent 用于改变连接状态,另一个用于改变蓝牙适配器本身的状态。

编辑:

要检测设备是否移入和移出范围,您需要使用以下 Intent :

对于这两种情况,您都需要普通的 BLUETOOTH 权限和 BLUETOOTH_ADMIN 权限:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

BroadcastReceiver 的 intent 过滤器看起来像这样:

<intent-filter> 
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>

Here是关于 BluetoothDevice 的一般文档。

关于android - CONNECTION_STATE_CHANGED 和 STATE_CHANGED 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435725/

相关文章:

java - Android AudioManager 事件监听器?

iphone - 如何将 HID 配置文件连接到 iOS 设备?

ios - 在IOS7中获取已连接蓝牙耳机的操作

Android 蓝牙 StartDiscovery() 总是返回 false

android - 如何通过蓝牙将音频从一台 Android 设备流式传输到另一台 Android 设备?

android - 在用户同意的情况下在 Android 中禁用蓝牙

android - 同一个Activity中不同的SoftInputMode,有可能吗?

Android:如何使用UriMatcher实现多表、查询参数、Activity的切换?

android - IntelliJ IDEA - 无法评估表达式找不到当前堆栈框架的源类

java - 如何使我的 ListView 行高适合里面的内容?