實(shí)現(xiàn)的主要功能(藍(lán)牙配對成功如何與遠(yuǎn)程設(shè)備一直連接)
1.當(dāng)藍(lán)牙配對成功連接時(shí),斷開遠(yuǎn)程端設(shè)備會(huì)自動(dòng)連接
2.當(dāng)設(shè)備長時(shí)間鎖屏?xí)?dǎo)致CachedBluetoothDevice自動(dòng)清空,如果藍(lán)牙斷開就不會(huì)自動(dòng)連接的處理
實(shí)現(xiàn)步驟
監(jiān)控藍(lán)牙斷開連接狀態(tài)時(shí)發(fā)生哪些改變媒體音頻與通話音頻(a2dp與hfp)
1.在com.android.bluetooth.btservice.AdapterProperties這個(gè)類中藍(lán)牙連接狀態(tài)
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這個(gè)類中藍(lán)牙連接狀態(tài)
/** 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... 及保存參數(shù)
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這個(gè)類中藍(lán)牙連接狀態(tài)
// 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);
}
廣播監(jiān)聽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);
//藍(lán)牙關(guān)閉時(shí)停止服務(wù)
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);
}
//這下面就是上面留意的幾個(gè)action及相應(yīng)的參數(shù)
}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存儲(chǔ)值
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點(diǎn)擊斷開連接時(shí)取消保存
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;
}
}
}
創(chuàng)建服務(wù)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();
}
}
}
}
}
藍(lán)牙自動(dòng)連接完畢經(jīng)測試長時(shí)間滅屏重啟遠(yuǎn)距離斷開等場景再次檢測到之前連接設(shè)備時(shí)會(huì)自動(dòng)連接