博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bluetooth-蓝牙事件监听
阅读量:6423 次
发布时间:2019-06-23

本文共 5000 字,大约阅读时间需要 16 分钟。

今天在做项目的时候,需要监听一个蓝牙耳机的连接状态。就写了一个小的测试方法。记录如下

看代码

这要处理的是蓝牙监听事件

package com.example.alert;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothHeadset;import android.bluetooth.BluetoothProfile;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.Toast;public class HeadsetPlugReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub        String action = intent.getAction();        if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {            Log.e("123", "BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED");        } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {            Log.e("123", "BluetoothAdapter.ACTION_STATE_CHANGED");            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);            switch (state) {            case BluetoothAdapter.STATE_OFF:                Log.e("123", "BluetoothAdapter.STATE_OFF");                break;            case BluetoothAdapter.STATE_TURNING_ON:                Log.e("123", "BluetoothAdapter.STATE_TURNING_ON");                break;            case BluetoothAdapter.STATE_ON:                Log.e("123", "BluetoothAdapter.STATE_ON");                break;            case BluetoothAdapter.STATE_TURNING_OFF:                Log.e("123", "BluetoothAdapter.STATE_TURNING_OFF");                break;            }        } else if (action                .equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {            Log.e("123", "BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED");            int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);            switch (state) {            case BluetoothProfile.STATE_DISCONNECTED:                Log.e("123", "BluetoothProfile.STATE_DISCONNECTED");                break;            case BluetoothProfile.STATE_CONNECTING:                Log.e("123", "BluetoothProfile.STATE_CONNECTING");                break;            case BluetoothProfile.STATE_CONNECTED:                Log.e("123", "BluetoothProfile.STATE_CONNECTED");                break;            case BluetoothProfile.STATE_DISCONNECTING:                Log.e("123", "BluetoothProfile.STATE_DISCONNECTING");                break;            }        }    }}

蓝牙的监听,需要加入权限,如下

 

下面是我们打印的结果

1.没有蓝牙耳机连接的情况下

蓝牙从断开到连接

1 09-21 11:45:27.223: E/123(15118): BluetoothAdapter.ACTION_STATE_CHANGED  2 09-21 11:45:27.223: E/123(15118): BluetoothAdapter.STATE_TURNING_ON  3 09-21 11:45:27.707: E/123(15118): BluetoothAdapter.ACTION_STATE_CHANGED  4 09-21 11:45:27.708: E/123(15118): BluetoothAdapter.STATE_ON

从连接到断开

1 09-21 11:49:09.126: E/123(15118): BluetoothAdapter.ACTION_STATE_CHANGED  2 09-21 11:49:09.126: E/123(15118): BluetoothAdapter.STATE_TURNING_OFF  3 09-21 11:49:09.271: E/123(15118): BluetoothAdapter.ACTION_STATE_CHANGED  4 09-21 11:49:09.271: E/123(15118): BluetoothAdapter.STATE_OFF

2.有蓝牙耳机连接的情况

 关闭蓝牙耳机(蓝牙保持打开)

1 09-21 22:12:15.439: E/123(17042): BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED  2 09-21 22:12:15.449: E/123(17042): BluetoothProfile.STATE_DISCONNECTED  3 09-21 22:12:16.457: E/123(17042): BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED

连接蓝牙耳机

1 09-21 22:14:22.356: E/123(17042): BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED  2 09-21 22:14:22.356: E/123(17042): BluetoothProfile.STATE_CONNECTED  3 09-21 22:14:22.357: E/123(17042): BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED

关闭蓝牙

1 09-21 22:15:12.558: E/123(17042): BluetoothAdapter.ACTION_STATE_CHANGED  2 09-21 22:15:12.559: E/123(17042): BluetoothAdapter.STATE_TURNING_OFF  3 09-21 22:15:13.557: E/123(17042): BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED  4 09-21 22:15:13.557: E/123(17042): BluetoothProfile.STATE_DISCONNECTED  5 09-21 22:15:13.908: E/123(17042): BluetoothAdapter.ACTION_STATE_CHANGED  6 09-21 22:15:13.908: E/123(17042): BluetoothAdapter.STATE_OFF

打开蓝牙

1 09-21 22:16:06.976: E/123(17042): BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED  2 09-21 22:16:06.976: E/123(17042): BluetoothProfile.STATE_CONNECTING  3 09-21 22:16:06.978: E/123(17042): BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED  4 09-21 22:16:07.036: E/123(17042): BluetoothAdapter.ACTION_STATE_CHANGED  5 09-21 22:16:07.036: E/123(17042): BluetoothAdapter.STATE_ON  6 09-21 22:16:10.616: E/123(17042): BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED  7 09-21 22:16:10.616: E/123(17042): BluetoothProfile.STATE_CONNECTED  8 09-21 22:16:10.619: E/123(17042): BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED

 从上面的结果我们能够明白蓝牙监听相关事件

的职责

1.BluetoothAdapter.ACTION_STATE_CHANGED:这个主要是用来监听蓝牙打开与否的状态,它穿过来的参数包含蓝牙正在断开、断开、正在连接、连接的状态,每次状态更改,都会触发这个广播

2.BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:这个主要是监听蓝牙设备连接状态。它同样包含四种状态

3.BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:这个就是监听蓝牙耳机的连接状态了。

至于他们监听的顺序,目前还不大清楚,似乎没有一个严格的先后顺序。就是感觉是

如果设备处于连接状态,优先级比较高

如果设备处于断开状态,它的存在条件优先级比较高

转载地址:http://pfgra.baihongyu.com/

你可能感兴趣的文章
DIV+CSS命名规范有助于SEO
查看>>
web项目buildPath与lib的区别
查看>>
我的友情链接
查看>>
ifconfig:command not found的解决方法
查看>>
计算机是怎么存储数字的
查看>>
Codeforces Round #369 (Div. 2) A. Bus to Udayland 水题
查看>>
adb上使用cp/mv命令的替代方法(failed on '***' - Cross-device link解决方法)
查看>>
C++标准库简介、与STL的关系。
查看>>
Spring Boot 3 Hibernate
查看>>
查询EBS请求日志的位置和名称
查看>>
大型机、小型机、x86服务器的区别
查看>>
J2EE十三个规范小结
查看>>
算法(第四版)C#题解——2.1
查看>>
网关支付、银联代扣通道、快捷支付、银行卡支付分别是怎么样进行支付的?...
查看>>
大数据开发实战:Stream SQL实时开发一
查看>>
C++返回引用的函数例程
查看>>
dll 问题 (转)
查看>>
REST API用得也痛苦
查看>>
test for windows live writer plugins
查看>>
Tiny210 U-BOOT(二)----配置时钟频率基本原理
查看>>