Lance_NullPoint 2013-08-01 07:12 采纳率: 0%
浏览 27918

安卓设备如何监听蓝牙的连接状态

如题,在我的APP中,成功将手机连接上了一个蓝牙串口,现在想监听它们之间的连接状态。在网上找到的方式都是用BroadcastReceiver来监听蓝牙状态改变的广播,于是我实现了这种方式,代码部分如下:
    private void registerBoradcastReceiver() {
    IntentFilter stateChangeFilter = new IntentFilter(
            BluetoothAdapter.ACTION_STATE_CHANGED);
    IntentFilter connectedFilter = new IntentFilter(
            BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter disConnectedFilter = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(stateChangeReceiver, stateChangeFilter);
    registerReceiver(stateChangeReceiver, connectedFilter);
    registerReceiver(stateChangeReceiver, disConnectedFilter);
}

    private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED == action) {
            showchange(action);
        }
        if (BluetoothDevice.ACTION_ACL_DISCONNECTED == action) {
            showchange(action);
        }
        if (BluetoothAdapter.ACTION_STATE_CHANGED == action) {
            showchange(action);
        }
    }
};

但是调试的时候并没有监听到任何的状态变化,求指教。
  • 写回答

6条回答 默认 最新

  • hua907 2014-02-26 07:08
    关注

    将IntentFilter connectedFilter = new IntentFilter(
    BluetoothDevice.ACTION_ACL_CONNECTED);
    换成IntentFilter connectedFilter = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);再试下
    同时,注意加上蓝牙的相关权限

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建