源:http://www.cnblogs.com/xiaochao1234/p/3793172.html
上次讲解Android的蓝牙基本用法,这次讲得深入些,探讨下蓝牙方面的隐藏API。用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出,那么如何去使用这两项功能呢?本文利用JAVA的反射机制去调用这两项功能对应的函数:createBond和removeBond,具体的发掘和实现步骤如下:
1、使用Git工具下载platform/packages/apps/Settings.git,在Setting源码中查找关于建立配对和解除配对的API,知道这两个API的宿主(BluetoothDevice);
2、使用反射机制对BluetoothDevice枚举其所有方法和常量,看看是否存在:
结果如下:
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却隐藏),则自己实现调用方法:
PS : SDK之所以不给出隐藏的API肯定有其原因,也许是出于安全性或者是后续版本兼容性的考虑,因此不能保证隐藏API能在所有Android平台上很好地运行。。。
本文程序运行效果如下:
main.xml源码如下:
工具类ClsUtils.java源码如下:
主程序testReflect.java的源码如下: