连接配对密码已知且固定的蓝牙设备时,明明在代码里就可以完成配对,却依旧被系统弹出配对窗口.

这无疑是令人难受的.

所以,便尝试着去屏蔽掉这个配对窗口.

要点:

  • 中断系统发出的蓝牙配对广播
  • 需要用到ClsUtils开源框架
  • 个人测试在Android 4.4上成功拦截,在4.2上不行,也许是高版本把蓝牙配对广播设为了有序广播?

code:

public class BluetoothConnectReceiver extends BroadcastReceiver { private final String TAG = "BluetoothConnectReceiver"; @SuppressLint("NewApi") @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, TAG + " -> BluetoothConnectReceiver start!"); if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i(TAG, TAG + " -> ddd"); try { Log.i(TAG, TAG + " -> Device Name = " + btDevice.getName()); if ("Device Name X".equals(btDevice.getName())) { abortBroadcast(); ClsUtils.setPin(btDevice.getClass(), btDevice, "password"); ClsUtils.createBond(btDevice.getClass(), btDevice); ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); } } catch (Exception e) { e.printStackTrace(); } } } }

通过预设的密码直接静默配对.

manifest:

<receiver android:name=".BluetoothConnectReceiver" >   <intent-filter android:priority="1000" >      <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />   </intent-filter></receiver>

接收器的优先级设为最高1000,保证最先被广播通知.

更多相关文章

  1. Android(安卓)广播接受者
  2. Android(六)Activity、请求码与结果码、Intent、广播、服务
  3. Android随笔之——Android广播机制Broadcast详解
  4. android 蓝牙连接通信
  5. Android通过蓝牙HC06与Arduino通信实例
  6. 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
  7. 蓝牙实现签到功能(中央与周边)
  8. Android(安卓)版本更新下载自动安装APK,并解决Android6.0安装失败
  9. Android用Broadcast实现EventBus的功能和用法

随机推荐

  1. 【linux】循序渐进学运维-基础篇-文件的
  2. css 实现中文繁体翻译 font-variant-ea
  3. Vue(5): APP.vue引入CSS样式文件和动态切
  4. 【linux-56】文件系统管理-df,du
  5. 用好HugePage,告别Linux性能故障
  6. 一个可能让你的页面渲染速度提升数倍的CS
  7. 一些鲜为人知的的CSS3属性
  8. Linux中常用压缩及解压命令
  9. CSS calc() 函数
  10. 常用的 tailwindcss 模板站点