栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android蓝牙配对时不弹框

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android蓝牙配对时不弹框

Android蓝牙配对框,根据输出log看到,包名是com.android.settings.bluetooth.BluetoothPairingDialog

是在Android原生Setting里面弹出的

对话框代码路径:packagesappsSettingssrccomandroidsettingsbluetoothBluetoothPairingDialog.java

然后跟一下代码,找到触发这个配对框触发的地方:packagesappsSettingssrccomandroidsettingsbluetoothBluetoothPairingRequest.java

public final class BluetoothPairingRequest extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
      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 (powerManager.isInteractive() && shouldShowDialog) {
      // 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);
    }
  }
}

由此想出处理方法:

自己定义一个BroadcastReceiver ,自动确认配对,然后拦截掉广播

java代码:

public final class BluetoothPairingRequestReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            return;
        }
        BluetoothDevice device =
                intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        try {
            device.setPairing/confirm/iation(true);//确认配对
            abortBroadcast();//拦截掉广播
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

AndroidManifest.xml:


	 //这里写成1000,最优先接收到广播
		
	

完毕

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号