蓝牙绕过系统Pin弹窗自动匹配,亲测4.0以上没问题
来源:互联网 发布:飞狐软件 编辑:程序博客网 时间:2024/05/20 05:09
今天做一个功能,APP连接蓝牙打印机,但是每次连接都要弹一个系统弹窗输入Pin,很不爽,一般Pin都是0000或者1234,所以想能不能让程序去自动输入匹配呢?随后Google了一大堆出来,基本都是以前的代码,我就从网上一个通用的工具类,做了一些修改经过边测边改一整天,基本上实现了这个功能,大部分情况下Pin窗会一闪而过,但是也偶尔会弹出来,或者连接上之后等一段时间会弹出来,这样子,希望能和大家讨论交流,下面上代码:
首先是修改过后的蓝牙自动配对工具类:
/** * Created by caolicheng on 14/12/1. * 蓝牙自动配对工具类 */import android.bluetooth.BluetoothDevice;import android.util.Log;import java.lang.reflect.Field;import java.lang.reflect.Method;import me.ele.omniknight.common.tools.AppLogger;public class ClsUtils { /** * 与设备配对 参考源码:platform/packages/apps/Settings.git * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */ static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method createBondMethod = btClass.getMethod("createBond"); Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); AppLogger.e("绑定:"+returnValue); return returnValue; } /** * 与设备解除配对 参考源码:platform/packages/apps/Settings.git * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */ static public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method removeBondMethod = btClass.getMethod("removeBond"); Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); AppLogger.e("解除绑定:"+returnValue); return returnValue; } static public boolean setPin(Class btClass, BluetoothDevice btDevice, byte[] str) throws Exception { try { Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[] {byte[].class}); Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,str); AppLogger.e("设置Pin:" + returnValue); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } //自动配对设置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()}); AppLogger.e("自动配对:"+result); return result; } static public byte[] convertPinToBytes(Class btClass,BluetoothDevice device,String strPin) throws Exception { Method convertPinToBytes = btClass.getMethod("convertPinToBytes",new Class[]{String.class}); byte[] result = (byte[]) convertPinToBytes.invoke(device,strPin); AppLogger.e("Pin转Bytes结束"); return result; } // 取消用户输入 static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); Boolean returnValue = (Boolean) createBondMethod.invoke(device); AppLogger.e("取消用户输入:" + returnValue);// cancelBondProcess(btClass, device); return returnValue; } // 取消配对 static public boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = btClass.getMethod("cancelBondProcess"); Boolean returnValue = (Boolean) createBondMethod.invoke(device); AppLogger.e("取消配对进程:"+returnValue); return returnValue; } /** * 打印所有方法和常量 * @param clsShow */ static public void printAllInform(Class clsShow) { try { // 取得所有方法 Method[] hideMethod = clsShow.getMethods(); int i = 0; for (; i < hideMethod.length; i++) { Log.e("method name", hideMethod[i].getName() + ";and the i is:" + i); } // 取得所有常量 Field[] allFields = clsShow.getFields(); for (i = 0; i < allFields.length; i++) { Log.e("Field name", allFields[i].getName()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}原理想必大家都是知道的,没错,就是反射,拿到隐藏API进行操作,那么在什么时候调用这个函数呢?我们需要监听一个隐藏的广播"android.bluetooth.device.action.PAIRING_REQUEST",这个是在蓝牙请求配对的时候会发出的广播,那么下一步怎么做呢?在监听到这个广播之后:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_NONE) { try { ClsUtils.autoBond(device.getClass(), device, "1234");// ClsUtils.createBond(device.getClass(),device);// ClsUtils.cancelPairingUserInput(device.getClass(), device); } catch (Exception e) { e.printStackTrace(); } }那么经过我的多次测试,基本上是没问题的,在4.0,4.1,4.2,4.4的手机上都有测试,中兴、联想、酷派和魅族(公司的几台测试机)。当然偶尔是有问题的。
希望能和大家一起探讨,谢谢!
0 0
- 蓝牙绕过系统Pin弹窗自动匹配,亲测4.0以上没问题
- Android 蓝牙自动匹配PIN码跳过用户交互
- Android 蓝牙4.0实现,自动匹配蓝牙设备
- Android蓝牙自动配对和Pin码设置
- android 4.0以上系统如何实现自动接听电话
- android 取消蓝牙配对框 实现自动配对 输入PIN码
- 求大神,Android4.4 自动连接蓝牙Ble不弹出输入PIN码的窗口解决办法?
- android6.0以上版本蓝牙4.0开发
- Android6.0以上系统搜索不到 蓝牙BLE 设备问题
- 蓝牙匹配
- 蓝牙签到系统 入公司自动打卡
- 关于android手机的蓝牙自动匹配实现
- 绕过android 5.0以上的pie机制
- android 6.0以上系统apk自动安装失败问题
- 安卓蓝牙4.0以上连接多台设备并接收蓝牙设备数据
- div+css ie7以上版本没问题,ie6效果不一样
- 关于安卓6.0以上系统中无法搜索到蓝牙设备的问题
- 蓝牙鼠标在多系统下自动配对
- 做Swing桌面程序,该怎样将组件与业务逻辑分离?
- Build.VERSION_CODES类
- 24. Dubbo原理解析-编码解码之编码解码流程
- HDU 4810 Wall Painting(异或 +按位容斥)
- 安卓控件——WebView
- 蓝牙绕过系统Pin弹窗自动匹配,亲测4.0以上没问题
- css中子元素根据父元素设置百分比高度的问题。
- 站姿坐姿判断人
- POJ2355——Railway tickets
- Java环境变量设置
- 安卓 LinearLayout的用法
- 冒泡算法
- Android编译过程详解
- 护肤-知乎推荐的护肤品