Bluetooth篇 开发实例之八 匹配

本文介绍如何在Android应用中实现蓝牙设备的匹配与解除匹配功能,通过反射调用隐藏的API,实现无需通过系统设置即可完成蓝牙设备的配对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/H-BolinBlog/p/5476773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值