1.当蓝牙配对成功连接时,非主动断开会自动连接
2.当设备长时间锁屏会导致CachedBluetoothDevice自动清空,如果蓝牙断开就不会自动连接的处理
监控蓝牙断开连接状态时发生哪些改变媒体音频与通话音频(a2dp与hfp)
1.在com.android.bluetooth.btservice.AdapterProperties这个类中蓝牙连接状态
void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
if (!validateProfileConnectionState(state) ||
!validateProfileConnectionState(prevState)) {
// Previously, an invalid state was broadcast anyway,
// with the invalid state converted to -1 in the intent.
// Better to log an error and not send an intent with
// invalid contents or set mAdapterConnectionState to -1.
errorLog("Error in sendConnectionStateChange: "
+ "prevState " + prevState + " state " + state);
return;
}
synchronized (mObject) {
updateProfileConnectionState(profile, state, prevState);
if (updateCountersAndCheckForConnectionStateChange(state, prevState)) {
setConnectionState(state);
//留意BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED
Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
convertToAdapterState(state));
intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,
convertToAdapterState(prevState));
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mService.sendBroadcastAsUser(intent, UserHandle.ALL,
mService.BLUETOOTH_PERM);
Log.d(TAG, "CONNECTION_STATE_CHANGE: " + device + ": "
+ prevState + " -> " + state);
}
}
}
2.在com.android.bluetooth.a2dpsink.A2dpSinkStateMachine这个类中蓝牙连接状态
/** Handles A2DP connection state change intent broadcasts. */
private class IntentBroadcastHandler extends Handler {
private void onConnectionStateChanged(BluetoothDevice device, int prevState, int state) {
Intent intent = new Intent(BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
//FIXME intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
//留意android.bluetooth.a2dp.sink... 及保存参数
intent = new Intent("android.bluetooth.a2dp.sink.profile.action.CONNECTION_STATE_CHANGED");
intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
log("Connection state " + device + ": " + prevState + "->" + state);
mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.A2DP_SINK,
state, prevState);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_CONNECTION_STATE_CHANGED:
onConnectionStateChanged((BluetoothDevice) msg.obj, msg.arg1, msg.arg2);
break;
}
}
}
3.在com.android.bluetooth.hfpclient.HeadsetClientStateMachine这个类中蓝牙连接状态
// This method does not check for error condition (newState == prevState)
private void broadcastConnectionState(BluetoothDevice device, int newState, int prevState) {
Log.d(TAG, "Connection state " + device + ": " + prevState + "->" + newState);
/*
* Notifying the connection state change of the profile before sending
* the intent for connection state change, as it was causing a race
* condition, with the UI not being updated with the correct connection
* state.
*/
mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.HEADSET_CLIENT,
newState, prevState);
//留意
Intent intent = new Intent(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
...
mService.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
}
广播监听com.android.settings.bluetooth.DockEventReceiver
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
...
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
Log.d(TAG,"szjjyh ACTION_STATE_CHANGED btState = "+btState);
//蓝牙关闭时停止服务
if (btState == BluetoothAdapter.STATE_OFF){
Intent intent2 = new Intent(context, BluetoothConnectService.class);
context.stopService(intent2);
}
if (btState != BluetoothAdapter.STATE_TURNING_ON) {
Intent i = new Intent(intent);
i.setClass(context, DockService.class);
beginStartingService(context, i);
}
//这下面就是上面留意的几个action及相应的参数
}else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
int oldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, 0);
changeDeviceStatus(device,newState,oldState,intent,context);
}else if ("android.bluetooth.a2dp.sink.profile.action.CONNECTION_STATE_CHANGED".equals(intent.getAction())) {
int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
changeDeviceStatus(device,newState,oldState,intent,context);
}else if (BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
changeDeviceStatus(device,newState,oldState,intent,context);
}
}
public static void changeDeviceStatus(BluetoothDevice device,int newState, int oldState ,Intent intent,Context context) {
if (!Utils.isAutoConnectBluetooth()) {
return;
}
Log.d(TAG,"szjjyh changeDeviceStatus newState = "+newState+" oldState = "+oldState+
"device = "+device.getAddress());
synchronized (sStartingServiceSync) {
if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
if (oldState==BluetoothProfile.STATE_CONNECTING&&newState == BluetoothProfile.STATE_CONNECTED) {
//留意saveConnectDevice
LocalBluetoothPreferences.saveConnectDevice(context,device.getAddress());
Intent intent2 = new Intent(context, BluetoothConnectService.class);
context.stopService(intent2);
}else if ((oldState==BluetoothProfile.STATE_CONNECTED||oldState==BluetoothProfile.STATE_CONNECTING)
&&newState == BluetoothProfile.STATE_DISCONNECTED){
if (LocalBluetoothPreferences.getConnectDevice(context,"").equals(device.getAddress())) {
Intent i = new Intent(intent);
i.setClass(context, BluetoothConnectService.class);
beginStartingService(context, i);
}
}else if (oldState==BluetoothProfile.STATE_DISCONNECTING&&newState == BluetoothProfile.STATE_DISCONNECTED){
Intent intent2 = new Intent(context, BluetoothConnectService.class);
context.stopService(intent2);
}else if (oldState==BluetoothProfile.STATE_CONNECTED&&newState == BluetoothProfile.STATE_DISCONNECTING){
LocalBluetoothPreferences.saveConnectDevice(context,"");
}
}
}
}
com.android.settings.bluetooth.LocalBluetoothPreferences存储值
static void saveConnectDevice(Context context, String addr) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString(KEY_CONNECT_DEVICE , addr);
editor.apply();
}
com.android.settings.bluetooth.DeviceProfilesSettings点击断开连接时取消保存
public final class DeviceProfilesSettings extends DialogFragment implements
CachedBluetoothDevice.Callback, DialogInterface.OnClickListener, OnClickListener {
...
Button cancle = (Button) mRootView.findViewById(R.id.cancle);
cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCachedDevice.unpair();
//留意
LocalBluetoothPreferences.saveConnectDevice(getContext(), "");
com.android.settings.bluetooth.Utils.updateSearchIndex(getContext(),
BluetoothSettings.class.getName(), mCachedDevice.getName(),
getString(R.string.bluetooth_settings),
R.drawable.ic_settings_bluetooth, false);
dismiss();
}
});
...
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
EditText deviceName = (EditText) mRootView.findViewById(R.id.name);
mCachedDevice.setName(deviceName.getText().toString());
break;
case DialogInterface.BUTTON_NEUTRAL:
mCachedDevice.unpair();
//留意
LocalBluetoothPreferences.saveConnectDevice(getContext(), "");
com.android.settings.bluetooth.Utils.updateSearchIndex(getContext(),
BluetoothSettings.class.getName(), mCachedDevice.getName(),
getString(R.string.bluetooth_settings),
R.drawable.ic_settings_bluetooth, false);
break;
}
}
}
创建服务com.android.settings.bluetooth.BluetoothConnectService
public class BluetoothConnectService extends Service {
private static final String TAG = "BluetoothConnectService";
private Timer timer;
private LocalBluetoothAdapter mLocalAdapter;
private CachedBluetoothDeviceManager mDeviceManager;
private LocalBluetoothProfileManager mProfileManager;
@Override
public void onCreate() {
super.onCreate();
// isOpenTimer(true);
LocalBluetoothManager manager = Utils.getLocalBtManager(this);
if (manager == null) {
Log.e(TAG, "szjjyh Can't get LocalBluetoothManager: exiting");
return;
}
mLocalAdapter = manager.getBluetoothAdapter();
mDeviceManager = manager.getCachedDeviceManager();
mProfileManager =manager.getProfileManager();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "szjjyh onDestroy");
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "szjjyh onStartCommand");
isOpenTimer(true);
return START_STICKY;
}
private void isOpenTimer(boolean isOpenWindow) {
if (isOpenWindow) {
if (timer == null) {
timer = new Timer();
timer.scheduleAtFixedRate(new RefreshTask(), 0, 1000 * 60 * 1);
}
} else {
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
private class RefreshTask extends TimerTask {
@Override
public void run() {
autoConnectDevice();
}
}
private void autoConnectDevice() {
Set<BluetoothDevice> bondedDevices = mLocalAdapter.getBondedDevices();
if (bondedDevices == null) {
stopSelf();
return;
}
Log.d(TAG, "szjjyh RefreshTask bondedDevices ="+bondedDevices.size());
for (BluetoothDevice device : bondedDevices) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
if (LocalBluetoothPreferences.getConnectDevice(this, "").equals(device.getAddress())) {
if (cachedDevice == null) {
cachedDevice = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, device);
}
int bondState = cachedDevice.getBondState();
if (bondState == BluetoothDevice.BOND_BONDED&&!cachedDevice.isConnected()) {
cachedDevice.connect(false);
} else {
stopSelf();
}
}
}
}
}
蓝牙自动连接完毕经测试长时间灭屏重启远距离断开等场景再次检测到之前连接设备时会自动连接