当前位置:操作系统 > 安卓/Android >>

Android提高第十三篇之探秘蓝牙隐藏API

上次讲解Android的蓝牙基本用法,这次讲得深入些,探讨下蓝牙方面的隐藏API。用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出,那么如何去使用这两项功能呢?本文利用JAVA的反射机制去调用这两项功能对应的函数:createBond和removeBond,具体的发掘和实现步骤如下:

1.使用Git工具下载platform/packages/apps/Settings.git,在Setting源码中查找关于建立配对和解除配对的API,知道这两个API的宿主(BluetoothDevice);

2.使用反射机制对BluetoothDevice枚举其所有方法和常量,看看是否存在:

view plaincopy to clipboardprint?
static public void printAllInform(Class clsShow) {  
    try {  
        // 取得所有方法  
        Method[] hideMethod = clsShow.getMethods();  
        int i = 0;  
        for (; i < hideMethod.length; i++) {  
            Log.e("method name", hideMethod[i].getName());  
        }  
        // 取得所有常量  
        Field[] allFields = clsShow.getFields();  
        for (i = 0; i < allFields.length; i++) {  
            Log.e("Field name", allFields[i].getName());  
        }  
    } catch (SecurityException e) {  
        // throw new RuntimeException(e.getMessage());  
        e.printStackTrace();  
    } catch (IllegalArgumentException e) {  
        // throw new RuntimeException(e.getMessage());  
        e.printStackTrace();  
    } catch (Exception e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  

 static public void printAllInform(Class clsShow) {
  try {
   // 取得所有方法
   Method[] hideMethod = clsShow.getMethods();
   int i = 0;
   for (; i < hideMethod.length; i++) {
    Log.e("method name", hideMethod[i].getName());
   }
   // 取得所有常量
   Field[] allFields = clsShow.getFields();
   for (i = 0; i < allFields.length; i++) {
    Log.e("Field name", allFields[i].getName());
   }
  } catch (SecurityException e) {
   // throw new RuntimeException(e.getMessage());
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   // throw new RuntimeException(e.getMessage());
   e.printStackTrace();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 } 

结果如下:

11-29 09:19:12.012: method name(452): cancelBondProcess
11-29 09:19:12.020: method name(452): cancelPairingUserInput
11-29 09:19:12.020: method name(452): createBond
11-29 09:19:12.020: method name(452): createInsecureRfcommSocket
11-29 09:19:12.027: method name(452): createRfcommSocket
11-29 09:19:12.027: method name(452): createRfcommSocketToServiceRecord
11-29 09:19:12.027: method name(452): createScoSocket
11-29 09:19:12.027: method name(452): describeContents
11-29 09:19:12.035: method name(452): equals
11-29 09:19:12.035: method name(452): fetchUuidsWithSdp
11-29 09:19:12.035: method name(452): getAddress
11-29 09:19:12.035: method name(452): getBluetoothClass
11-29 09:19:12.043: method name(452): getBondState
11-29 09:19:12.043: method name(452): getName
11-29 09:19:12.043: method name(452): getServiceChannel
11-29 09:19:12.043: method name(452): getTrustState
11-29 09:19:12.043: method name(452): getUuids
11-29 09:19:12.043: method name(452): hashCode
11-29 09:19:12.043: method name(452): isBluetoothDock
11-29 09:19:12.043: method name(452): removeBond
11-29 09:19:12.043: method name(452): setPairingConfirmation
11-29 09:19:12.043: method name(452): setPasskey
11-29 09:19:12.043: method name(452): setPin
11-29 09:19:12.043: method name(452): setTrust
11-29 09:19:12.043: method name(452): toString
11-29 09:19:12.043: method name(452): writeToParcel
11-29 09:19:12.043: method name(452): convertPinToBytes
11-29 09:19:12.043: method name(452): getClass
11-29 09:19:12.043: method name(452): notify
11-29 09:19:12.043: method name(452): notifyAll
11-29 09:19:12.043: method name(452): wait
11-29 09:19:12.051: method name(452): wait
11-29 09:19:12.051: method name(452): wait

3.如果枚举发现API存在(SDK却隐藏),则自己实现调用方法:

view plaincopy to clipboardprint?
/** 
 * 与设备配对 参考源码:platform/packages/apps/Settings.git 
 * SettingssrccomandroidsettingsluetoothCachedBluetoothDevice.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 
 * SettingssrccomandroidsettingsluetoothCachedBluetoothDevice.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();  

 /**
  * 与设备配对 参考源码:platform/packages/apps/Settings.git
  * SettingssrccomandroidsettingsluetoothCachedBluetoothDevice.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
  * SettingssrccomandroidsettingsluetoothCachedBluetoothDevice.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();
 }

PS:SDK之所以不给出隐藏的API肯定有其原因,也许是出于安全性或者是后续版本兼容性的考虑,因此不能保证隐藏API能在所有Android平台上很好地运行。。。

本文程序运行效果如下:

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,