android系统蓝牙自动配对连接

本文详细介绍了Android系统中蓝牙自动配对连接的实现过程,主要涉及BluetoothPairingRequest.java、BluetoothPairingDialog.java、BluetoothPairingDialogFragment.java和BluetoothPairingController.java四个关键组件。通过接收BluetoothPairingRequest广播,根据屏幕状态决定直接打开对话框或显示通知。此外,还介绍了一个自定义的BluetoothPairingSure.java类,用于替换BluetoothPairingController.java,实现配对逻辑的控制。
摘要由CSDN通过智能技术生成

蓝牙配对主要在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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值