正文  网络通信 > 红外/蓝牙 >

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

上次讲解Android的蓝牙基本用法,这次讲得深入些,探讨下蓝牙方面的隐藏API。用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数...

上次讲解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 
 * Settingssrccomandroidsettings