zoukankan      html  css  js  c++  java
  • 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";

  • 相关阅读:
    HDU 1261 字串数(排列组合)
    Codeforces 488C Fight the Monster
    HDU 1237 简单计算器
    POJ 2240 Arbitrage
    POJ 3660 Cow Contest
    POJ 1052 MPI Maelstrom
    POJ 3259 Wormholes
    POJ 3268 Silver Cow Party
    Codesforces 485D Maximum Value
    POJ 2253 Frogger(最短路)
  • 原文地址:https://www.cnblogs.com/H-BolinBlog/p/5476773.html
Copyright © 2011-2022 走看看