蓝牙自动配对,即搜索到其它蓝牙设备之后直接进行配对,不需要弹出配对确认框或者密钥输入框。

转载请注明出处http://blog.csdn.net/qq_25827845/article/details/52400782

源码下载地址:https://github.com/chaohuangtianjie994/BlueTooth-AutoPair

经过最近一段时间得研究,针对网上给出的案例。总结了一个亲测好使的Demo。

说明如下:

1、本Demo用来连接蓝牙设备HC-05,如果你要连接其他蓝牙设备,注意修改相关名字以及修改设备初试pin值。

2、将Demo安装在Android手机上,点击按钮,可以实现与目标蓝牙设备的自动配对。

3、若目标蓝牙设备为Android手机的蓝牙,则只能保证本设备不弹出配对框,对方还是会弹出配对框。但是!!不管目标蓝牙点击“确认”or“取消”,在本设备中都显示已经成功配对。实测表明,确实已经配对了,可以进行数据传输。

4、由于使用了广播机制,所以需要在Androidmanifest.xml进行如下配置。

先配置蓝牙使用权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

然后配置action,将需要用到的广播进行注册

<receiver android:name="com.ywq.broadcast.BluetoothReceiver" >
    <intent-filter android:priority="1000">
        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST"/>
        <action android:name="android.bluetooth.device.action.FOUND" />
    </intent-filter>
</receiver>

程序运行流程:

1、点击按钮,判断蓝牙是否打开,,执行bluetoothAdapter.startDiscovery();由本地蓝牙设备扫描远程蓝牙设备,startDiscovery()方法是一个异步方法,调用后立即返回。该方法会进行蓝牙设备的搜索,持续12秒。

2、搜索时,系统会发送3个广播,分别为:ACTION_DISCOVERY_START:开始搜索 、ACTION_DISCOVERY_FINISHED:搜索结束、 ACTION_FOUND:找到设备,该Intent中包含两个extra fields;

3、在广播接收类中BluetoothReceiver.java中,当设备找到之后会执行其onReceive方法。

4、String action = intent.getAction(); //得到action,

第一次action的值为BluetoothDevice.ACTION_FOUND,当找到的设备是我们目标蓝牙设备时,调用createBond方法来进行配对。ClsUtils.createBond(btDevice.getClass(), btDevice);该方法执行后,系统会收到一个请求配对的广播,即android.bluetooth.device.action.PAIRING_REQUEST。最后进行自动配对操作。

5、配对操作借助工具类ClsUtils.java得到了Android蓝牙API中隐藏的方法,实现自动配对,不弹出配对框的功能。

代码如下:

MainActivity.java

  1. package com.example.mybuletooth;
  2. import android.app.Activity;
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity implements OnClickListener{
  9. /** Called when the activity is first created. */
  10. private Button autopairbtn=null;
  11. private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. autopairbtn=(Button) findViewById(R.id.button1);
  17. autopairbtn.setOnClickListener(this);
  18. }
  19. //设置按钮的监听方法
  20. @Override
  21. public void onClick(View arg0) {
  22. if (!bluetoothAdapter.isEnabled())
  23. {
  24. bluetoothAdapter.enable();//异步的,不会等待结果,直接返回。
  25. }else{
  26. bluetoothAdapter.startDiscovery();
  27. }
  28. }
  29. }

BluetoothReceiver.java

  1. package com.ywq.broadcast;
  2. import com.ywq.tools.ClsUtils;
  3. import android.bluetooth.BluetoothDevice;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.util.Log;
  8. public class BluetoothReceiver extends BroadcastReceiver{
  9. String pin = "1234";  //此处为你要连接的蓝牙设备的初始密钥,一般为1234或0000
  10. public BluetoothReceiver() {
  11. }
  12. //广播接收器,当远程蓝牙设备被发现时,回调函数onReceiver()会被执行
  13. @Override
  14. public void onReceive(Context context, Intent intent) {
  15. String action = intent.getAction(); //得到action
  16. Log.e("action1=", action);
  17. BluetoothDevice btDevice=null;  //创建一个蓝牙device对象
  18. // 从Intent中获取设备对象
  19. btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  20. if(BluetoothDevice.ACTION_FOUND.equals(action)){  //发现设备
  21. Log.e("发现设备:", "["+btDevice.getName()+"]"+":"+btDevice.getAddress());
  22. if(btDevice.getName().contains("HC-05"))//HC-05设备如果有多个,第一个搜到的那个会被尝试。
  23. {
  24. if (btDevice.getBondState() == BluetoothDevice.BOND_NONE) {
  25. Log.e("ywq", "attemp to bond:"+"["+btDevice.getName()+"]");
  26. try {
  27. //通过工具类ClsUtils,调用createBond方法
  28. ClsUtils.createBond(btDevice.getClass(), btDevice);
  29. } catch (Exception e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. }
  34. }else
  35. Log.e("error", "Is faild");
  36. }else if(action.equals("android.bluetooth.device.action.PAIRING_REQUEST")) //再次得到的action,会等于PAIRING_REQUEST
  37. {
  38. Log.e("action2=", action);
  39. if(btDevice.getName().contains("HC-05"))
  40. {
  41. Log.e("here", "OKOKOK");
  42. try {
  43. //1.确认配对
  44. ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
  45. //2.终止有序广播
  46. Log.i("order...", "isOrderedBroadcast:"+isOrderedBroadcast()+",isInitialStickyBroadcast:"+isInitialStickyBroadcast());
  47. abortBroadcast();//如果没有将广播终止,则会出现一个一闪而过的配对框。
  48. //3.调用setPin方法进行配对...
  49. boolean ret = ClsUtils.setPin(btDevice.getClass(), btDevice, pin);
  50. } catch (Exception e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54. }else
  55. Log.e("提示信息", "这个设备不是目标蓝牙设备");
  56. }
  57. }
  58. }

工具类ClsUtils.java

  1. package com.ywq.tools;
  2. /************************************ 蓝牙配对函数 * **************/
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Field;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.util.Log;
  7. public class ClsUtils
  8. {
  9. /**
  10. * 与设备配对 参考源码:platform/packages/apps/Settings.git
  11. * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
  12. */
  13. static public boolean createBond(Class btClass, BluetoothDevice btDevice)
  14. throws Exception
  15. {
  16. Method createBondMethod = btClass.getMethod("createBond");
  17. Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
  18. return returnValue.booleanValue();
  19. }
  20. /**
  21. * 与设备解除配对 参考源码:platform/packages/apps/Settings.git
  22. * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
  23. */
  24. static public boolean removeBond(Class<?> btClass, BluetoothDevice btDevice)
  25. throws Exception
  26. {
  27. Method removeBondMethod = btClass.getMethod("removeBond");
  28. Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
  29. return returnValue.booleanValue();
  30. }
  31. static public boolean setPin(Class<? extends BluetoothDevice> btClass, BluetoothDevice btDevice,
  32. String str) throws Exception
  33. {
  34. try
  35. {
  36. Method removeBondMethod = btClass.getDeclaredMethod("setPin",
  37. new Class[]
  38. {byte[].class});
  39. Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
  40. new Object[]
  41. {str.getBytes()});
  42. Log.e("returnValue", "" + returnValue);
  43. }
  44. catch (SecurityException e)
  45. {
  46. // throw new RuntimeException(e.getMessage());
  47. e.printStackTrace();
  48. }
  49. catch (IllegalArgumentException e)
  50. {
  51. // throw new RuntimeException(e.getMessage());
  52. e.printStackTrace();
  53. }
  54. catch (Exception e)
  55. {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59. return true;
  60. }
  61. // 取消用户输入
  62. static public boolean cancelPairingUserInput(Class<?> btClass,
  63. BluetoothDevice device)  throws Exception
  64. {
  65. Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
  66. //        cancelBondProcess(btClass, device);
  67. Boolean returnValue = (Boolean) createBondMethod.invoke(device);
  68. return returnValue.booleanValue();
  69. }
  70. // 取消配对
  71. static public boolean cancelBondProcess(Class<?> btClass,
  72. BluetoothDevice device)
  73. throws Exception
  74. {
  75. Method createBondMethod = btClass.getMethod("cancelBondProcess");
  76. Boolean returnValue = (Boolean) createBondMethod.invoke(device);
  77. return returnValue.booleanValue();
  78. }
  79. //确认配对
  80. static public void setPairingConfirmation(Class<?> btClass,BluetoothDevice device,boolean isConfirm)throws Exception
  81. {
  82. Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation",boolean.class);
  83. setPairingConfirmation.invoke(device,isConfirm);
  84. }
  85. /**
  86. *
  87. * @param clsShow
  88. */
  89. static public void printAllInform(Class clsShow)
  90. {
  91. try
  92. {
  93. // 取得所有方法
  94. Method[] hideMethod = clsShow.getMethods();
  95. int i = 0;
  96. for (; i < hideMethod.length; i++)
  97. {
  98. Log.e("method name", hideMethod[i].getName() + ";and the i is:"
  99. + i);
  100. }
  101. // 取得所有常量
  102. Field[] allFields = clsShow.getFields();
  103. for (i = 0; i < allFields.length; i++)
  104. {
  105. Log.e("Field name", allFields[i].getName());
  106. }
  107. }
  108. catch (SecurityException e)
  109. {
  110. // throw new RuntimeException(e.getMessage());
  111. e.printStackTrace();
  112. }
  113. catch (IllegalArgumentException e)
  114. {
  115. // throw new RuntimeException(e.getMessage());
  116. e.printStackTrace();
  117. }
  118. catch (Exception e)
  119. {
  120. // TODO Auto-generated catch block
  121. e.printStackTrace();
  122. }
  123. }
  124. }

Androidmanifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.mybuletooth"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="21" />
  9. <uses-permission android:name="android.permission.BLUETOOTH"/>
  10. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name=".MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. <receiver android:name="com.ywq.broadcast.BluetoothReceiver" >
  25. <intent-filter android:priority="1000">
  26. <action android:name="android.bluetooth.device.action.PAIRING_REQUEST"/>
  27. <action android:name="android.bluetooth.device.action.FOUND" />
  28. </intent-filter>
  29. </receiver>
  30. </application>
  31. </manifest>

布局配置文件activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context="com.example.mybuletooth.MainActivity" >
  10. <Button
  11. android:id="@+id/button1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentLeft="true"
  15. android:layout_alignParentTop="true"
  16. android:layout_marginLeft="54dp"
  17. android:layout_marginTop="56dp"
  18. android:text="自动配对" />
  19. <TextView
  20. android:id="@+id/textView1"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_centerVertical="true"
  24. android:text="点击按钮,自动搜索蓝牙设备,并且进行配对" />
  25. </RelativeLayout>

针对网上其它帖子中的demo不好使的原因,在此给出一些我的看法,是不是这样不敢保证,至少部分是这些原因吧。。。

1、出现一个一闪而过的配对框怎么办?

答:那是因为广播没有停止,须得调用abortBroadcast();将广播停止。

2、自动配对框还是会弹出来怎么办?

答:网上好多帖子代码有误,或者没有说清楚。请注意相关配置和工具类中函数的使用。

这是本人亲测好使的自动配对Demo,仅供参考,希望对大家有所帮助。有问题可以联系我。

重要更新:********************************************************************************

2016-10-20 ,今天和一个咨询我的小伙伴详细的聊了会儿天。他的问题是,下图所示的if语句块进不去。

它的btDevice.getBondState( )=12,但是BluetoothDevice.BOND_NONE=10,这不是肯定进不去么。

其中,查阅SDK,可以看到BluetoothDevice的这几个函数和数字的含义是什么。

参考网址:http://www.cnblogs.com/over140/archive/2010/12/21/1912482.html

如下所示:

我一看,天呐,很明显的低级错误。我让他打开设置看看,是否显示已经配对。结果自然是已经配对了。

产生原因:这个demo在跑之前,他已经在手机-设置-蓝牙中手动把目标蓝牙配对了。那还玩个毛呀

当手动取消配对后,程序运行正常,log打印和预期一样,自动配对实现。

提示:

通过这个小失误,可以看出,评论里好多说这也不行,那也不行的。既然好多人都说好使,那你为什么就不行呢?还是多从自身找问题吧,心思缜密点,避免这种低级失误。大哥,你是程序猿好不好。

                                         

源码下载地址:https://github.com/chaohuangtianjie994/BlueTooth-AutoPair

如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,有问题可以进群366533258讨论哈~

Android蓝牙自动配对Demo,亲测好使!!!(转)的更多相关文章

  1. Android蓝牙自动配对Demo,亲测好使!!!

    蓝牙自动配对,即搜索到其它蓝牙设备之后直接进行配对,不需要弹出配对确认框或者密钥输入框. 转载请注明出处http://blog.csdn.net/qq_25827845/article/details ...

  2. android 实现蓝牙自动配对连接

    BluetoothConnectActivityReceiver.java:监听蓝牙配对的广播 代码: package com.imte.Broadcast; import com.imte.util ...

  3. 支付宝支付demo(亲测)

    支付宝支付demo 这个是java后台调起支付的demo,直接将body返回给安卓端即可调起支付 package com.dyy.test; import java.text.SimpleDateFo ...

  4. android系统的源代码获取(亲测可用)

    1.在线阅读各版本源代码: http://androidxref.com/ 2.下载到本地: http://blog.csdn.net/yin1031468524/article/details/55 ...

  5. <亲测好使>mac os 安装mcrypt扩展

    以前安装opencart的时候倒是不需要mcrypt 这个库.但是新版本需要了.加上自己的 是mac环境.当时闲麻烦,就一直没装.这次下午就寻思给装上吧! 1.首先你要先安装xcode这个工具.不然没 ...

  6. Arduino中数据类型转换 int转换为char 亲测好使,itoa()函数

    由于博主最近在做一个项目,需要采集不同传感器的数据,包括float型的HCHO,以及int型的PM2.5数据.但是最终向服务器上传的数据都得转换为char型才能发送,这是借鉴了一个github上面的实 ...

  7. Arduino中数据类型转换 float/double转换为char 亲测好使,dtostrf()函数

    如何轻松玩转Arduino单片机,当我在进行数据转换的时候,遇到了问题,尝试了C语言和C++中的好多函数,都没有达到将float型数据转换为char型的目的.苦苦查阅资料后,终于找到了一个大神级函数! ...

  8. mysql 1067终极解决办法 亲测好使

       进入mysql data 目录 删除 ib_logfile0  ib_logfile1   ibdata1   这三个文件 重启mysql

  9. Android 蓝牙开发之搜索、配对、连接、通信大全

            蓝牙( Bluetooth®):是一种无线技术标准,可实现固定设备.移动设备和楼宇个人域网之间的短距离数据 交换(使用2.4-2.485GHz的ISM波段的UHF无线电波).蓝牙设备最 ...

随机推荐

  1. setlocale - 设置当前的区域选项

    总览 (SYNOPSIS) #include <locale.h> char *setlocale(int category, const char * locale); 描述 (DESC ...

  2. Harbor私有镜像仓库(下)

    Harbor私有镜像仓库(下) 链接:https://pan.baidu.com/s/1MAb0dllUwmoOk7TeVCZOVQ 提取码:ldt5 复制这段内容后打开百度网盘手机App,操作更方便 ...

  3. 转载:Angular的filter总结

    过滤器(filter)正如其名,作用就是接收一个输入,通过某个规则进行处理,然后返回处理后的结果.主要用在数据的格式化上,例如获取一个数组 中的子集,对数组中的元素进行排序等.ng内置了一些过滤器,它 ...

  4. zTree节点排序、jsTree节点排序

    项目中遇到了这个问题,网上也没找到比较清晰的答案,索性提供一个方案吧. 原理:将整个树形插件的数据源进行排序,插件在构造UI时,自然也是按照顺序来排列的,目前这种思路适用于 zTree 和 jsTre ...

  5. html2canvas截图白边显示问题

    html2canvas(document.getElementById('resource_chart'),{ useCORS:true, logging:false, width:$('#resou ...

  6. 火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件

    目前很多插件不支持 Firefox 3.5 哦1. Add N Edit Cookies 查看和修改本地的Cookie,Cookie欺骗必备. 下载:http://code.google.com/p/ ...

  7. nucleus plus学习总结(后续)

    前言:     刚刚抽筋点了保存发布,结果要审核,那就分开写个续好了. 内容: signal     信号是异步通知task的一种机制,HISR是不可以接收信号的,但是可以发送信号.     TCB中 ...

  8. 【Flutter学习】页面布局之其它布局处理

    一,概述 Flutter中拥有30多种预定义的布局widget,常用的有Container.Padding.Center.Flex.Row.Colum.ListView.GridView.按照< ...

  9. OC学习篇之---总结和学习目录

    今天终于把OC的基础知识学习完了,但是这些知识只是最基础的,还有很多高级知识,这个可能需要后面慢慢的去学习才能体会到.下面就是这次学习OC的目录教程,如果大家发现有什么不正确的地方,请指正,小弟是新生 ...

  10. K-th Closest Distance

    题目链接 题意:n个数,q次查询,查询[l , r] 内, | a[i] - p | 第k大的数 思路:主席树维护下权值大小,二分答案,查询区间[p - mid, p + mid] 的个数 #incl ...