Bluetooth篇 开发实例之八 匹配
2024-09-01 14:25:59
自己写的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";
最新文章
- centos下开启ftp服务
- C# Word中设置/更改文本方向
- 关于设置anroid系统时间
- 禁止盗链,强制回登录页面web.config配置
- Pycharm 2016 2 激活
- xmind的第九天笔记
- codeforces B. Flag Day 解题报告
- Java里的File I/O
- (转)PHP的语言结构和函数的区别
- CentOS中TFTP配置
- Redis的配置
- Ext.Panel的主要功能
- Linux kernel API的查看
- 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示
- d3.js 绘制极坐标图(polar plot)
- ThreadLocal是否会导致内存泄露
- startup.bat 一闪而过解决方案
- Ubuntu编译安装最新的webkit
- loadrunner&#160;运行脚本-命令行运行脚本
- Python图形编程探索系列-04-网上图片与标签组件的结合
热门文章
- sources-x.list
- [类和对象]4 C++ static &; friend
- JavaScript里面的正则以及eval
- hdu1712 分组背包 ACboy needs your help
- nyoj 题目737 合并石子(一)
- 【bzoj2238】Mst 最小生成树+树链剖分+线段树
- hdu 2616 Kill the monster (DFS)
- P2052 [NOI2011]道路修建
- Zigzag数组 -- 面试宝典
- 【CF Edu 28 B. Math Show】