自己写的App匹配蓝牙设备,不需要通过系统设置去连接。

匹配和通信是两回事。

用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对解除配对,但是这两项功能的函数没有在SDK中给出。但是可以通过反射来获取。

Method[] hideMethod2 =BluetoothDevice.class.getMethods();
int k = 0;
for (; k < hideMethod2.length; k++) {
Log.e("BluetoothDevice method name", hideMethod2[k].getName());
}

知道这两个API的宿主(BluetoothDevice):

/**
* 与设备配对 参考源码: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);
return returnValue.booleanValue();
} /**
* 与设备解除配对 参考源码: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);
return returnValue.booleanValue();
}

蓝牙开发实例之一中,我们已经获取到了扫描的蓝牙设备,此时,我们需要进行匹配。

第一步:自定义类

import java.lang.reflect.Method;
import android.bluetooth.BluetoothDevice; /**
* 此类用来获取蓝牙匹配和接触匹配的方法,利用Java的反射原理来获取,因为该两种方法被android所隐藏,所以通过此方式来获取。
*
* @author THINK
*
*/
public class BtBondUtils { /**
* 与设备配对 参考源码: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");
//invoke()方法主要是为了类反射,这样你可以在不知道具体的类的情况下,根据配置的字符串去调用一个类的方法。在灵活编程的时候非常有用。
//很多框架代码都是这样去实现的。但是一般的编程,你是不需要这样做的,因为类都是你自己写的,怎么调用,怎么生成都是清楚的。
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
return returnValue.booleanValue();
} /**
* 与设备解除配对 参考源码: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);
return returnValue.booleanValue();
} }

第二步:调用方法

BtBondUtils.createBond(device.getClass(), device);

PS.会有提示匹配的弹窗。

第三步:设置Pin值,不会有匹配弹窗    --- > 此步骤待验证

    //自动配对设置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;
}

调用方法:

autoBond(device.getClass(), device, strPin);//设置pin值  

在源码里面有一个自动配对的方法,也就是把pin值自动设为"0000";

最新文章

  1. centos下开启ftp服务
  2. C# Word中设置/更改文本方向
  3. 关于设置anroid系统时间
  4. 禁止盗链,强制回登录页面web.config配置
  5. Pycharm 2016 2 激活
  6. xmind的第九天笔记
  7. codeforces B. Flag Day 解题报告
  8. Java里的File I/O
  9. (转)PHP的语言结构和函数的区别
  10. CentOS中TFTP配置
  11. Redis的配置
  12. Ext.Panel的主要功能
  13. Linux kernel API的查看
  14. 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示
  15. d3.js 绘制极坐标图(polar plot)
  16. ThreadLocal是否会导致内存泄露
  17. startup.bat 一闪而过解决方案
  18. Ubuntu编译安装最新的webkit
  19. loadrunner&#160;运行脚本-命令行运行脚本
  20. Python图形编程探索系列-04-网上图片与标签组件的结合

热门文章

  1. sources-x.list
  2. [类和对象]4 C++ static &amp; friend
  3. JavaScript里面的正则以及eval
  4. hdu1712 分组背包 ACboy needs your help
  5. nyoj 题目737 合并石子(一)
  6. 【bzoj2238】Mst 最小生成树+树链剖分+线段树
  7. hdu 2616 Kill the monster (DFS)
  8. P2052 [NOI2011]道路修建
  9. Zigzag数组 -- 面试宝典
  10. 【CF Edu 28 B. Math Show】