蓝牙配对主要在settings应用下面的com.android.settings.bluetooth目录下,主要是有广播侦听BluetoothPairingRequest.java,配对对话框BluetoothPairingDialog.java,BluetoothPairingDialogFragment.java,
配对控制类BluetoothPairingController.java.实现的最终经过分析修改如下:
1.蓝牙配对请求广播
package com.android.settings.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.PowerManager;
import android.os.UserHandle;
import android.util.Log;
public final class BluetoothPairingRequest extends BroadcastReceiver {
private static final String TAG = "BluetoothPairingRequest";
private static final boolean DEBUG = true;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// SPRD:bug#785347 action may be null
if (!BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
return;
}
// convert broadcast intent into activity intent (same action string)
Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent);
PowerManager powerManager =
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceAddress = device != null ? device.getAddress() : null;
String deviceName = device != null ? device.getName() : null;
boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground(
context, deviceAddress, deviceName);
if (DEBUG) {
Log.d(TAG,"powerManager.isInteractive() == "+powerManager.isInteractive());
Log.d(TAG,"shouldShowDialog == "+shouldShowDialog);
}
//remove shouldShowDialog
//update by lsj start
/*if (powerManager.isInteractive()) {
// Since the screen is on and the BT-related activity is in the foreground,
// just open the dialog
context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
} else {
// Put up a notification that leads to the dialog
intent.setClass(context, BluetoothPairingService.class);
context.startServiceAsUser(intent, UserHandle.CURRENT);
}*/
if (powerManager.isInteractive()) {
// Since the screen is on and the BT-related activity is in the foreground,
// just open the dialog
BluetoothPairingSure sureControl=new BluetoothPairingSure(pairingIntent,context);
sureControl.sureParing();
} else {
// Put up a notification that leads to the dialog
BluetoothPairingSure sureControlNotify=new BluetoothPairingSure(intent,context);
sureControlNotify.sureParing();
}
//update by lsj end
}
}
2.自定义BluetoothPairingSure .java替换BluetoothPairingController.java
package com.android.settings.bluetooth;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.android.settings.R;
import com.android.settings.bluetooth.BluetoothPairingDialogFragment.BluetoothPairingDialogListener;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.setting