目录

一、配对方法

二、解除配对方法

三、配对/解除配对结果

四、justwork配对模式下,不弹出配对框

五、pincode配对模式下,不弹出配对框

六、小结


在之前的文章【Android】蓝牙开发—— 经典蓝牙配对介绍(Java代码实现演示)附Demo源码 中,简单介绍和演示了经典蓝牙的配对方式,今天就在之前的基础上继续讲讲,蓝牙的配对、解除配对以及如何实现不弹出配对框。

关于“配对”、“解除配对”的说法,还有叫“绑定”、“解绑”,这里提前说一下,在之前的文章中出现,防止小伙伴们不理解。

一、配对方法

蓝牙配对方式,目前有两种,一种是通过反射的方法,还有一种是直接调用官方API的方法。

第一种:反射的方法,在低于Android API 19时,配对的方法是隐藏的方法,所以只有通过反射方法实现。

 /*** 第一种* 执行配对 反射* @param bluetoothDevice 蓝牙设备* @return true 执行绑定 false 未执行绑定*/public boolean boundDevice(BluetoothDevice bluetoothDevice){if(bluetoothDevice == null){Log.e(TAG,"boundDevice-->bluetoothDevice == null");return false;}try {return ClsUtils.createBond(BluetoothDevice.class,bluetoothDevice);} catch (Exception e) {e.printStackTrace();}return true;}

ClsUtils.java中createBond()方法如下:

/*** 与设备配对 参考源码:platform/packages/apps/Settings.git* /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java*/@SuppressWarnings("unchecked")static public boolean createBond(@SuppressWarnings("rawtypes") Class btClass, BluetoothDevice btDevice)throws Exception {Method createBondMethod = btClass.getMethod("createBond");Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);return returnValue.booleanValue();}

第二种:官方API,但是只支持Android API 19以上的设备。

/*** 第二种* 执行配对 官方API* @param bluetoothDevice  蓝牙设备* @return  true 执行绑定 false 未执行绑定*/public boolean boundDeviceAPI(BluetoothDevice bluetoothDevice){if(bluetoothDevice == null){return false;}//注意:Android 4.4版本之后的APIif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {return bluetoothDevice.createBond();}return false;}

二、解除配对方法

蓝牙解除配对的方式,因为是隐藏的方法,目前只有通过反射进行调用。

/*** 执行解除配对  反射* @param bluetoothDevice 蓝牙设备* @return  true 执行解绑  false未执行解绑*/public boolean disBoundDevice(BluetoothDevice bluetoothDevice){if(bluetoothDevice == null){Log.e(TAG,"disBoundDevice-->bluetoothDevice == null");return false;}try {return ClsUtils.removeBond(BluetoothDevice.class,bluetoothDevice);} catch (Exception e) {e.printStackTrace();}return true;}

ClsUtils.java中removeBond()方法如下:

/*** 与设备解除配对 参考源码:platform/packages/apps/Settings.git* /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java*/@SuppressWarnings("unchecked")static public boolean removeBond(Class btClass, BluetoothDevice btDevice)throws Exception {Method removeBondMethod = btClass.getMethod("removeBond");Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);return returnValue.booleanValue();}

三、配对/解除配对结果

配对、解除配对的结果是通过系统广播出来的,所以我们在使用的时候,需要注册广播接收器来获取配对状态。

这里我们自定义广播接收器。在onCreate()中注册该广播,在onDestroy()中注销广播。

 /*** 蓝牙广播接收器*/private class BtBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if(TextUtils.equals(action,BluetoothDevice.ACTION_BOND_STATE_CHANGED)){BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int bondSate = bluetoothDevice.getBondState();switch(bondSate) {case BluetoothDevice.BOND_NONE:Log.d(TAG, "已解除配对");break;case BluetoothDevice.BOND_BONDING:Log.d(TAG, "正在配对...");break;case BluetoothDevice.BOND_BONDED:Log.d(TAG, "已配对");break;}}}}
 //注册广播接收btBroadcastReceiver = new BtBroadcastReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //配对状态监听registerReceiver(btBroadcastReceiver,intentFilter);
//注销广播接收
unregisterReceiver(btBroadcastReceiver);

四、justwork配对模式下,不弹出配对框

1、配对的时候

justwork配对模式下,不管是调用反射的配对方法还是官方API的配对方法,都不会弹出配对框,实现自动配对。

2、经典蓝牙第一次连接的时候(即连接之前,与设备是没有配对的状态)

经典蓝牙建立连接时,有两种方式,一种是建立安全的连接,即需要配对的连接;

还有一种就是建立不安全的连接,即不需要配对的连接。建立不安全的连接,实际上是因为没有进行配对,当然就不会有配对框弹出。

//1、建立安全的蓝牙连接,会弹出配对框
BluetoothSocket BluetoothSocket=bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
//2、建立不安全的蓝牙连接,不进行配对,就不弹出配对框
BluetoothSocket BluetoothSocket=bluetoothDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(uuid));

五、pincode配对模式下,不弹出配对框

1、注册BluetoothDevice.ACTION_PAIRING_REQUEST广播监听

 //注册广播接收btBroadcastReceiver = new BtBroadcastReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //配对状态监听if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);  //配对请求}registerReceiver(btBroadcastReceiver,intentFilter);

2、在广播接收器中处理

 /*** 蓝牙广播接收器*/private class BtBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if(TextUtils.equals(action,BluetoothDevice.ACTION_BOND_STATE_CHANGED)){BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int bondSate = bluetoothDevice.getBondState();switch(bondSate) {case BluetoothDevice.BOND_NONE:Log.d(TAG, "已解除配对");break;case BluetoothDevice.BOND_BONDING:Log.d(TAG, "正在配对...");break;case BluetoothDevice.BOND_BONDED:Log.d(TAG, "已配对");break;}}else if(TextUtils.equals(action,BluetoothDevice.ACTION_PAIRING_REQUEST)){int type = -1;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,BluetoothDevice.ERROR);Log.d(TAG, "ACTION_PAIRING_REQUEST--"+type);//PAIRING_VARIANT_PIN 0 if(type == BluetoothDevice.PAIRING_VARIANT_PIN){//防止弹出一闪而过的配对框abortBroadcast();//弹框后自动输入密码、自动确定boolean isSetPin = curBluetoothDevice.setPin("0000".getBytes());Log.d(TAG, "setPin()-->" + isSetPin);}}}}

因为官方的BluetoothDevice.setPin() 是支持API 19以上的设备,所以在低于API 19的设备上,需要使用反射的方法来获取setPin()方法。

 /*** 蓝牙广播接收器*/private class BtBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if(TextUtils.equals(action,BluetoothDevice.ACTION_BOND_STATE_CHANGED)){BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int bondSate = bluetoothDevice.getBondState();switch(bondSate) {case BluetoothDevice.BOND_NONE:Log.d(TAG, "已解除配对");break;case BluetoothDevice.BOND_BONDING:Log.d(TAG, "正在配对...");break;case BluetoothDevice.BOND_BONDED:Log.d(TAG, "已配对");break;}}else if(TextUtils.equals(action,BluetoothDevice.ACTION_PAIRING_REQUEST)){int type = -1;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,BluetoothDevice.ERROR);Log.d(TAG, "ACTION_PAIRING_REQUEST--"+type);//PAIRING_VARIANT_CONSENT 0 if(type == BluetoothDevice.PAIRING_VARIANT_PIN){//防止弹出一闪而过的配对框abortBroadcast();//弹框后自动输入密码、自动确定try {boolean isSetPin = ClsUtils.autoBond(BluetoothDevice.class,curBluetoothDevice,"0000");Log.d(TAG, "setPin()-->" + isSetPin);} catch (Exception e) {e.printStackTrace();}}}}}

ClsUtils.java中autoBond()方法如下:

 //自动配对设置Pin值static public boolean autoBond(Class btClass,BluetoothDevice device,String strPin) throws Exception {Method autoBondMethod = btClass.getMethod("setPin",new Class[]{byte[].class});Boolean result = (Boolean)autoBondMethod.invoke(device,new Object[]{strPin.getBytes()});return result;}

六、小结

1、justwork配对模式下,

(1)调用反射的配对方法或者官方API的配对方法,都能实现自动配对,不会弹出配对框。

(2)调用官方API首次连接,且建立非安全的连接,不会弹出配对框,但是本质是因为没有执行配对操作。

2、pincode配对模式下,实现自动配对的方法,只适用Android 8.0及以下的系统,Android 9.0及以上系统不生效。

3、如果想要实现弹出配对框时自动输入或点击,可以使用无障碍服务(Accessibility Service)。通过获取界面的控件,比如输入框、按钮,然后模拟输入或点击,实现自动配对。

注意:上面所说的justwork或pincode模式也不是绝对的,因为这完全是由蓝牙设备软件工程师决定的,所以实际开发时还是要根据实际的设备进行方式的选择。

【Android】蓝牙开发——经典蓝牙:配对与解除配对 实现配对或连接时不弹出配对框相关推荐

  1. 蓝牙配对模式 java_【Android】蓝牙开发—— 经典蓝牙配对介绍(Java代码实现演示)附Demo源码...

    目录 前言 一.连接&配对方法介绍 二.演示:第一次连接蓝牙设备  &  直接与蓝牙设备建立配对 三.总结 四.补充 五.Demo案例源码地址: 前言 前面两篇文章[Android]蓝 ...

  2. 【Android】蓝牙开发—— 经典蓝牙配对介绍(Java代码实现演示)附Demo源码

    目录 前言 一.连接&配对方法介绍 二.演示:第一次连接蓝牙设备  &  直接与蓝牙设备建立配对 三.总结 四.补充 五.Demo案例源码地址: 前言 前面两篇文章[Android]蓝 ...

  3. Android蓝牙开发 — 经典蓝牙BLE蓝牙

    一,前期基础知识储备 1)蓝牙是一种支持设备之间短距离通信的无线电技术(其他还包括红外,WIFI): 支持移动电话.笔记本电脑.无线耳机等设备之间进行信息的交换: Android支持的蓝牙协议栈:Bl ...

  4. Android蓝牙开发—经典蓝牙详细开发流程

    文章目录 开发流程 权限 核心API BlueToothAdapter getDefaultAdapter():获取BluetoothAdapter对象 判断设备是否支持蓝牙 判断蓝牙是否开启 get ...

  5. android pin码 经典蓝牙_Android蓝牙开发—经典蓝牙详细开发流程

    Android蓝牙开发-经典蓝牙详细开发流程 发布时间:2018-07-16 13:41, 浏览次数:637 , 标签: Android Android蓝牙开发前,首先要区分是经典蓝牙开发还是BLE( ...

  6. Android笔记---蓝牙开发经典蓝牙和低功耗蓝牙

    目录 前言 一般开发步骤 相关API介绍 一.通用API 1.BluetoothAdapter 2.BluetoothDevice 二.经典蓝牙(BT)API 1.BluetoothSocket 2. ...

  7. 【Android】蓝牙开发——经典蓝牙配对介绍(通过手机系统蓝牙演示)

    目录 一.蓝牙配对介绍 二.蓝牙配对过程 三.蓝牙配对方式展示 一.蓝牙配对介绍 蓝牙规范定义了两种标准配对过程,LMP配对(也称为基于PIN码)和SSP安全简易配对. LMP(Link Manage ...

  8. Android蓝牙开发—经典蓝牙和BLE(低功耗)蓝牙的区别

    找到一篇介紹BT与BLE使用差别的文章, 写的很清晰,看完基本明白了 ----------------------------------------------------------------- ...

  9. Android蓝牙开发——经典蓝牙的连接

    1.蓝牙的基本操作 蓝牙权限 android.permission.BLUETOOTH //允许程序连接到已配对的蓝牙设备,请求连接/接收连接/传输数据需要改权限, 主要用于对配对后进行操作. and ...

最新文章

  1. CVPR坐实华人主场:包揽全部奖项,四成作者来自中国,清华商汤领衔,专门设奖致敬黄煦涛...
  2. U-Mail邮件服务系统任意文件上传+执行漏洞(runtime缺陷与验证绕过)
  3. ASP.NET MVC 最佳开发实践(1)
  4. SpringBoot笔记-注册后发送邮箱点击激活(异步)
  5. (二)深入了解超文本
  6. Windows10最新MySQL8.0.23安装教程(超级详细)
  7. linux内核热修复,揭露内核黑科技 - 热补丁技术真容
  8. [转]B树(多向平衡查找树)详解
  9. windows系统上安装与使用Android NDK r8d(二)
  10. 如何从零开发一个复杂深度学习模型
  11. 安装指定的vue-router版本
  12. 深度学习2.0-普通BP神经网络
  13. Javascript语言精粹--The Excellence in Javascript
  14. Sublime Text 收藏笔记
  15. Python官方文档入门小教程
  16. Cuda驱动安装/更新
  17. 解决代理服务器端口被占用
  18. 敏捷开发(scrum)简介
  19. 凉茶配方案终审 加多宝赔偿近2亿元
  20. Linux 安装Oracle10g

热门文章

  1. vimdiff常用命令详解
  2. 读书笔记《Effective C++》条款40:明智而审慎地使用多重继承
  3. 【站内题解】十六道csdn每日一练Python题解
  4. java中BOM是什么_Java-BOM与DOM对象
  5. 高并发系统--限流算法
  6. java筆試題,Java筆試題附答案 | 學步園
  7. ThreadPoolExecutor(四)——Interrupt
  8. MATLAB De_Pcode Mex 求助
  9. 微信运营营销推广方案
  10. Java SpringMVC毕业项目实战-学生信息管理系统