• 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";

  • 相关阅读:
    双列集合
    单列集合
    Comparable和Comparator的区别
    用友U8 |【固定资产】固定资产生成凭证,点完计提折旧后没有凭证出来
    用友U8 |【应收管理】为什么在应收模块弃审销售发票,弃审不成功?
    用友U8 | 【总账】应收核销明细报错,提示:将截断字符串或二进制数据
    用友U8 | 【网上银行】网上银行模块网银支付后台字段说明
    用友U8 | 【实施导航】引入交易单位档案,提示:账号不能为空!
    用友U8 | 【实施导航】已经创建的人员档案,如何更新维护银行信息
    用友U8 |【合同管理】合同结算单不能失效
  • 原文地址:https://www.cnblogs.com/H-BolinBlog/p/5476773.html
Copyright © 2020-2023  润新知