Bluetooth篇 开发实例之八 匹配
自己写的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篇 开发实例之八 匹配的更多相关文章
- Bluetooth篇 开发实例之七 匹配&UUID
匹配和通信是两回事. 1.用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出.但是可以通过反射来获取. 知道这两个API的 ...
- Bluetooth篇 开发实例之九 和蓝牙模块通信
首先,我们要去连接蓝牙模块,那么,我们只要写客户端的程序就好了,蓝牙模块就相当于服务端. 连接就需要UUID. #蓝牙串口服务SerialPortServiceClass_UUID = ‘{00001 ...
- Android Developer -- Bluetooth篇 开发实例之四 API详解
http://www.open-open.com/lib/view/open1390879771695.html 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每 ...
- Android Developer -- Bluetooth篇 开发实例之一 扫描设备
第一步:声明Bluetooth Permissions <!-- 设置蓝牙访问权限 --> <uses-permission android:name="android.p ...
- Bluetooth篇 开发实例之十一 官网的Bluetooth Chat sample的bug
当没有匹配的设备和没有找到可用设备的时候. // If there are paired devices, add each one to the ArrayAdapter if (pairedDev ...
- Bluetooth篇 开发实例之十 官网的Bluetooth Chat sample app.
运行的时候,会报错: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Action ...
- Android Developer -- Bluetooth篇 开发实例之三 管理连接
Managing a Connection When you have successfully connected two (or more) devices, each one will have ...
- Android Developer -- Bluetooth篇 开发实例之二 连接设备
连接设备 In order to create a connection between your application on two devices, you must implement bot ...
- Bluetooth篇 开发实例之六 蓝牙RSSI计算距离
计算公式: d = 10^((abs(RSSI) - A) / (10 * n)) 其中: d - 计算所得距离 RSSI - 接收信号强度(负值) A - 发射端和接收端相隔1米时的信号强度 n - ...
随机推荐
- 软工实践 - 第十六次作业 Alpha 冲刺 (7/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10013959.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...
- Alpha 冲刺
队名:我头发呢队 组长博客 作业博客 杰(组长) 过去两天完成了哪些任务 查阅Python爬取音源的资料,如 Python3爬虫抓取网易云音乐热评实战 Python爬取高品质QQ音乐(2) 如何爬网易 ...
- c#中获得MD5字符串方法
在用户登录的过程中,我们会遇到要查询对比用户名密码的是否存在或者是否正确,但是数据库中存放的是通过MD5加密的字符串,所有我们可以先把用户输入的用户名或者是密码先转为DM5字符串再跟数据库查出的MD5 ...
- java值转递?引用传递?
值传递是传递的是原值的副本,引用传递传递的是原值. 在Java中,如果是基本数据类型,传递的是该参数字面量值的拷贝.如果是引用数据类型,传递的是该参数所引用对象在堆中地址的拷贝. swap(int a ...
- MVC4.0 JSON JsonResult 序列化之后 对JSON 进行修改 EXTJS4.0 分页
事情是这样的:我在MVC 下 前后台交互 用JsonResult 返回给前台使用. public JsonResult AjaxFindHospitalInfo() { List<T> l ...
- 【bzoj3669】[Noi2014]魔法森林 Kruskal+LCT
原文地址:http://www.cnblogs.com/GXZlegend/p/6797748.html 题目描述 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看 ...
- [codeforces] 585D Lizard Era: Beginning || 双向dfs
原题 有n(n<=2)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使n个任务结束后三个人得到的值是一样的.输出每次要派哪两个人,如果不行输出Impossible. n< ...
- POJ 1236 Networks of School Tarjan 基础
题目大意: 给一个有向图,一个文件可以从某个点出发传递向他能连的边 现在有两个问题 1.至少需要多少个放文件可以让整个图都有文件 2.可以进行一个操作:给一对点(u,v)连一条u->v的有向边, ...
- php 字符串重要函数
1.chop() 从字符串右端移除字符 chop(string,charlist) $str="hello world~"; echo chop($str,"ld~&qu ...
- 汕头市队赛 SRM 08 A
比赛没参加 所以回来补题咯 A还是自己YY出来了的 可惜比赛没有打 描述 给一个 01 串设为其 S,询问是否存在只出现两次的 01 串 T. 这里的出现定义为存在一串下标 ,满足 且 . 输入格式 ...