今天在做项目的时候,需要监听一个蓝牙耳机的连接状态。就写了一个小的测试方法。记录如下
看代码
这要处理的是蓝牙监听事件
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;
} } } }
蓝牙的监听,需要加入权限,如下
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
下面是我们打印的结果
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:这个就是监听蓝牙耳机的连接状态了。
至于他们监听的顺序,目前还不大清楚,似乎没有一个严格的先后顺序。就是感觉是
如果设备处于连接状态,优先级比较高
如果设备处于断开状态,它的存在条件优先级比较高