如何实现android蓝牙开发 自动配对连接,并不弹出提示框

之前做一个android版的蓝牙,遇到最大的难题就是自动配对.

上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了

我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。

在源码 BluetoothDevice类中还有两个隐藏方法

cancelBondProcess()和cancelPairingUserInput()

这两个方法一个是取消配对进程一个是取消用户输入

下面是自动配对的代码

Mainfest,xml注册

<receiverandroid:name=".BluetoothConnectActivityReceiver"><intent-filter><actionandroid:name="android.bluetooth.device.action.PAIRING_REQUEST"/></intent-filter></receiver>自己在收到广播时处理并将预先输入的密码设置进去
publicclassBluetoothConnectActivityReceiverextendsBroadcastReceiver{StringstrPsw="0";@OverridepublicvoidonReceive(Contextcontext,Intentintent){//TODOAuto-generatedmethodstubif(intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")){BluetoothDevicebtDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);//byte[]pinBytes=BluetoothDevice.convertPinToBytes("1234");//device.setPin(pinBytes);Log.i("tag11111","ddd");try{ClsUtils.setPin(btDevice.getClass(),btDevice,strPsw);//手机和蓝牙采集器配对ClsUtils.createBond(btDevice.getClass(),btDevice);ClsUtils.cancelPairingUserInput(btDevice.getClass(),btDevice);}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}
<b>/************************************蓝牙配对函数***************/importjava.lang.reflect.Field;importjava.lang.reflect.Method;importandroid.bluetooth.BluetoothDevice;importandroid.util.Log;publicclassClsUtils{/***与设备配对参考源码:platform/packages/apps/Settings.git*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java*/staticpublicbooleancreateBond(ClassbtClass,BluetoothDevicebtDevice)throwsException{MethodcreateBondMethod=btClass.getMethod("createBond");BooleanreturnValue=(Boolean)createBondMethod.invoke(btDevice);returnreturnValue.booleanValue();}/***与设备解除配对参考源码:platform/packages/apps/Settings.git*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java*/staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)throwsException{MethodremoveBondMethod=btClass.getMethod("removeBond");BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice);returnreturnValue.booleanValue();}staticpublicbooleansetPin(ClassbtClass,BluetoothDevicebtDevice,Stringstr)throwsException{try{MethodremoveBondMethod=btClass.getDeclaredMethod("setPin",newClass[]{byte[].class});BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice,newObject[]{str.getBytes()});Log.e("returnValue",""+returnValue);}catch(SecurityExceptione){//thrownewRuntimeException(e.getMessage());e.printStackTrace();}catch(IllegalArgumentExceptione){//thrownewRuntimeException(e.getMessage());e.printStackTrace();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returntrue;}//取消用户输入staticpublicbooleancancelPairingUserInput(ClassbtClass,BluetoothDevicedevice)throwsException{MethodcreateBondMethod=btClass.getMethod("cancelPairingUserInput");//cancelBondProcess()BooleanreturnValue=(Boolean)createBondMethod.invoke(device);returnreturnValue.booleanValue();}//取消配对staticpublicbooleancancelBondProcess(ClassbtClass,BluetoothDevicedevice)throwsException{MethodcreateBondMethod=btClass.getMethod("cancelBondProcess");BooleanreturnValue=(Boolean)createBondMethod.invoke(device);returnreturnValue.booleanValue();}/****@paramclsShow*/staticpublicvoidprintAllInform(ClassclsShow){try{//取得所有方法Method[]hideMethod=clsShow.getMethods();inti=0;for(;i<hideMethod.length;i++){Log.e("methodname",hideMethod[i].getName()+";andtheiis:"+i);}//取得所有常量Field[]allFields=clsShow.getFields();for(i=0;i<allFields.length;i++){Log.e("Fieldname",allFields[i].getName());}}catch(SecurityExceptione){//thrownewRuntimeException(e.getMessage());e.printStackTrace();}catch(IllegalArgumentExceptione){//thrownewRuntimeException(e.getMessage());e.printStackTrace();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}</b>
<b>publicstaticbooleanpair(StringstrAddr,StringstrPsw){booleanresult=false;BluetoothAdapterbluetoothAdapter=BluetoothAdapter.getDefaultAdapter();bluetoothAdapter.cancelDiscovery();if(!bluetoothAdapter.isEnabled()){bluetoothAdapter.enable();}if(!BluetoothAdapter.checkBluetoothAddress(strAddr)){//检查蓝牙地址是否有效Log.d("mylog","devAdduneffient!");}BluetoothDevicedevice=bluetoothAdapter.getRemoteDevice(strAddr);if(device.getBondState()!=BluetoothDevice.BOND_BONDED){try{Log.d("mylog","NOTBOND_BONDED");ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对ClsUtils.createBond(device.getClass(),device);remoteDevice=device;//配对完毕就把这个设备对象传给全局的remoteDeviceresult=true;}catch(Exceptione){//TODOAuto-generatedcatchblockLog.d("mylog","setPiNfailed!");e.printStackTrace();}//}else{Log.d("mylog","HASBOND_BONDED");try{ClsUtils.createBond(device.getClass(),device);ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对ClsUtils.createBond(device.getClass(),device);remoteDevice=device;//如果绑定成功,就直接把这个设备对象传给全局的remoteDeviceresult=true;}catch(Exceptione){//TODOAuto-generatedcatchblockLog.d("mylog","setPiNfailed!");e.printStackTrace();}}returnresult;}</b>转自

更多相关文章

  1. 蓝牙实现签到功能(中央与周边)
  2. 又优化了一下Android(安卓)ListView 异步加载图片(续)
  3. Activity的构成
  4. Android常用的一些服务demo源码
  5. Android新的网络请求框架volley源码解释及示例
  6. Google推Fast Pair机制Android也能快速配对蓝牙耳机
  7. 框架和流程——OkHttp 源码详解(一)
  8. Android蓝牙BLE开发(二)——对BLE设备的扫描连接以及读写数据
  9. Android-BLE 1. 基本类的介绍

随机推荐

  1. Android抓取当前运行apk包名和类名
  2. Android之选项菜单和上下文菜单解析
  3. Android(安卓)Studio for Ubuntu 14.04
  4. Version of SQLite used in Android?
  5. Android基础(四) 常用控件
  6. 关于Android(安卓)火车票查询App
  7. Android(安卓)html+动态添加控件,实现text
  8. Android 再按一次返回键退出 代码
  9. android多屏幕适配相关知识和官方翻译文
  10. android技术牛人博客