展会信息港展会大全

Android提高第十三篇之探秘蓝牙隐藏API
来源:互联网   发布日期:2015-09-28 16:02:15   浏览:656次  

导读:上次讲解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 blocke.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 blocke.printStackTrace();} }

结果如下:

11-29 09:19:12.012: method name(452): cancelBondProcess11-29 09:19:12.020: method name(452): cancelPairingUserInput11-29 09:19:12.020: method name(452): createBond11-29 09:19:12.020: method name(452): createInsecureRfcommSocket11-29 09:19:12.027: method name(452): createRfcommSocket11-29 09:19:12.027: method name(452): createRfcommSocketToServiceRecord11-29 09:19:12.027: method name(452): createScoSocket11-29 09:19:12.027: method name(452): describeContents11-29 09:19:12.035: method name(452): equals11-29 09:19:12.035: method name(452): fetchUuidsWithSdp11-29 09:19:12.035: method name(452): getAddress11-29 09:19:12.035: method name(452): getBluetoothClass11-29 09:19:12.043: method name(452): getBondState11-29 09:19:12.043: method name(452): getName11-29 09:19:12.043: method name(452): getServiceChannel11-29 09:19:12.043: method name(452): getTrustState11-29 09:19:12.043: method name(452): getUuids11-29 09:19:12.043: method name(452): hashCode11-29 09:19:12.043: method name(452): isBluetoothDock11-29 09:19:12.043: method name(452): removeBond11-29 09:19:12.043: method name(452): setPairingConfirmation11-29 09:19:12.043: method name(452): setPasskey11-29 09:19:12.043: method name(452): setPin11-29 09:19:12.043: method name(452): setTrust11-29 09:19:12.043: method name(452): toString11-29 09:19:12.043: method name(452): writeToParcel11-29 09:19:12.043: method name(452): convertPinToBytes11-29 09:19:12.043: method name(452): getClass11-29 09:19:12.043: method name(452): notify11-29 09:19:12.043: method name(452): notifyAll11-29 09:19:12.043: method name(452): wait11-29 09:19:12.051: method name(452): wait11-29 09:19:12.051: method name(452): wait

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

view plaincopy to clipboardprint?/*** 与设备配对 参考源码:platform/packages/apps/Settings.git* Settingssrccomandroidsettings

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港