自己写的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";

Bluetooth篇 开发实例之八 匹配的更多相关文章

  1. Bluetooth篇 开发实例之七 匹配&UUID

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

  2. Bluetooth篇 开发实例之九 和蓝牙模块通信

    首先,我们要去连接蓝牙模块,那么,我们只要写客户端的程序就好了,蓝牙模块就相当于服务端. 连接就需要UUID. #蓝牙串口服务SerialPortServiceClass_UUID = ‘{00001 ...

  3. Android Developer -- Bluetooth篇 开发实例之四 API详解

    http://www.open-open.com/lib/view/open1390879771695.html 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每 ...

  4. Android Developer -- Bluetooth篇 开发实例之一 扫描设备

    第一步:声明Bluetooth Permissions <!-- 设置蓝牙访问权限 --> <uses-permission android:name="android.p ...

  5. Bluetooth篇 开发实例之十一 官网的Bluetooth Chat sample的bug

    当没有匹配的设备和没有找到可用设备的时候. // If there are paired devices, add each one to the ArrayAdapter if (pairedDev ...

  6. Bluetooth篇 开发实例之十 官网的Bluetooth Chat sample app.

    运行的时候,会报错: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Action ...

  7. Android Developer -- Bluetooth篇 开发实例之三 管理连接

    Managing a Connection When you have successfully connected two (or more) devices, each one will have ...

  8. Android Developer -- Bluetooth篇 开发实例之二 连接设备

    连接设备 In order to create a connection between your application on two devices, you must implement bot ...

  9. Bluetooth篇 开发实例之六 蓝牙RSSI计算距离

    计算公式: d = 10^((abs(RSSI) - A) / (10 * n)) 其中: d - 计算所得距离 RSSI - 接收信号强度(负值) A - 发射端和接收端相隔1米时的信号强度 n - ...

随机推荐

  1. 编译TypeScript(TypeScript转JavaScript)

    1.配置tsconfig.json文件 tsconfig.json文件配置说明 { "compilerOptions": { //生成相关说明,TypeScript编译器如何编译. ...

  2. Adaptive Boosting

    Boosting boosting和bagging很类似,所使用的多个分类器类型都是一致的.另外,他们的主要区别点如下: boosting中不同的分类器是通过串行得到的,每个分类器都是根据已经训练出来 ...

  3. 通过设计表快速了解sql语句中字段的含义

    打开Navicat-------> 选择数据库 ------->右键设计表------>查看下方注释

  4. BI商业智能培训系列——(三)SSAS入门

    简介:     一个客户端工具,提供了设计.创建和管理来自数据仓库的多维数据集的功能,还提供对OLAP数据客户端访问功能. 解决方案如下: 相关概念: 维度: 维度可以理解为划分依据,简单的说就是看问 ...

  5. 【POJ3294】 Life Forms(SA)

    ...又是TLE,对于单组数据肯定TLE不了,问题是多组的时候就呵呵了... 按height分组去搞,然后判一下是否不属于同一个串... ; var x,y,rank,sa,c,col,h,rec:. ...

  6. BZOJ 1901: Zju2112 Dynamic Rankings | 带修改主席树

    题目: emmmm是个权限题 题解: 带修改主席树的板子题,核心思想是用树状数组维护动态前缀和的性质来支持修改 修改的时候修改类似树状数组一样进行logn个Insert 查询的时候同理,树状数组的方法 ...

  7. 雅礼集训 Day5 T3 题 解题报告

    题 题目背景 由于出题人赶时间所以没办法编故事来作为背景. 题目描述 一开始有\(n\)个苹果,\(m\)个人依次来吃苹果,第\(i\)个人会尝试吃\(u_i\)或\(v_i\)号苹果,具体来说分三种 ...

  8. CORS跨域cookie传递

    服务端 Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:* Access-Control-Allow-Origin ...

  9. Java基础语法实例(1)——实习第一天

    来到广州实习的第一天,我选择的是JavaEE,因为以后的方向是Java,所以就选择了它.感觉有一段时间没有接触Java了.趁此机会好好努力,将基础巩固好. Java输入及循环,判断,字符转换,数组定义 ...

  10. elementui 日期选择器设置当前默认日期(picker-options),以及当前日期以后的无法选择(default-value)

    目前官方的日期默认是当前日期,打开之后长这样子:现在是三月13日,但是有的需求是当前日期在后面. 就像这样: 代码如下: default-value是设置当前日期默认值的."timeDefa ...