Android提高之藍(lán)牙隱藏API探秘
前面文章講解了Android的藍(lán)牙基本用法,本文講得深入些,探討下藍(lán)牙方面的隱藏API。用過(guò)Android系統(tǒng)設(shè)置(Setting)的人都知道藍(lán)牙搜索之后可以建立配對(duì)和解除配對(duì),但是這兩項(xiàng)功能的函數(shù)沒(méi)有在SDK中給出,那么如何去使用這兩項(xiàng)功能呢?本文利用JAVA的反射機(jī)制去調(diào)用這兩項(xiàng)功能對(duì)應(yīng)的函數(shù):createBond和removeBond,具體的發(fā)掘和實(shí)現(xiàn)步驟如下:
1.使用Git工具下載platform/packages/apps/Settings.git,在Setting源碼中查找關(guān)于建立配對(duì)和解除配對(duì)的API,知道這兩個(gè)API的宿主(BluetoothDevice);
2.使用反射機(jī)制對(duì)BluetoothDevice枚舉其所有方法和常量,看看是否存在:
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(); } }
結(jié)果如下:
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.如果枚舉發(fā)現(xiàn)API存在(SDK卻隱藏),則自己實(shí)現(xiàn)調(diào)用方法:
/** * 與設(shè)備配對(duì) 參考源碼: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(); } /** * 與設(shè)備解除配對(duì) 參考源碼: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(); }
此處注意:SDK之所以不給出隱藏的API肯定有其原因,也許是出于安全性或者是后續(xù)版本兼容性的考慮,因此不能保證隱藏API能在所有Android平臺(tái)上很好地運(yùn)行。
本文程序運(yùn)行效果如下圖所示:
main.xml源碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:layout_height="wrap_content" android:id="@+id/btnSearch" android:text="Search" android:layout_width="160dip"></Button> <Button android:layout_height="wrap_content" android:layout_width="160dip" android:text="Show" android:id="@+id/btnShow"></Button> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout> <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ListView> </LinearLayout>
工具類ClsUtils.java源碼如下:
package com.testReflect; import java.lang.reflect.Field; import java.lang.reflect.Method; import android.bluetooth.BluetoothDevice; import android.util.Log; public class ClsUtils { /** * 與設(shè)備配對(duì) 參考源碼: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(); } /** * 與設(shè)備解除配對(duì) 參考源碼: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(); } /** * * @param clsShow */ 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(); } } }
主程序testReflect.java的源碼如下:
package com.testReflect; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class testReflect extends Activity { Button btnSearch, btnShow; ListView lvBTDevices; ArrayAdapter<String> adtDevices; List<String> lstDevices = new ArrayList<String>(); BluetoothDevice btDevice; BluetoothAdapter btAdapt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSearch = (Button) this.findViewById(R.id.btnSearch); btnSearch.setOnClickListener(new ClickEvent()); btnShow = (Button) this.findViewById(R.id.btnShow); btnShow.setOnClickListener(new ClickEvent()); lvBTDevices = (ListView) this.findViewById(R.id.ListView01); adtDevices = new ArrayAdapter<String>(testReflect.this, android.R.layout.simple_list_item_1, lstDevices); lvBTDevices.setAdapter(adtDevices); lvBTDevices.setOnItemClickListener(new ItemClickEvent()); btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本機(jī)藍(lán)牙功能 if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 開(kāi)藍(lán)牙 btAdapt.enable(); // 注冊(cè)Receiver來(lái)獲取藍(lán)牙設(shè)備相關(guān)的結(jié)果 IntentFilter intent = new IntentFilter(); intent.addAction(BluetoothDevice.ACTION_FOUND); intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(searchDevices, intent); } private BroadcastReceiver searchDevices = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Bundle b = intent.getExtras(); Object[] lstName = b.keySet().toArray(); // 顯示所有收到的消息及其細(xì)節(jié) for (int i = 0; i < lstName.length; i++) { String keyName = lstName[i].toString(); Log.e(keyName, String.valueOf(b.get(keyName))); } // 搜索設(shè)備時(shí),取得設(shè)備的MAC地址 if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_NONE) { String str = "未配對(duì)|" + device.getName() + "|" + device.getAddress(); lstDevices.add(str); // 獲取設(shè)備名稱和mac地址 adtDevices.notifyDataSetChanged(); } } } }; class ItemClickEvent implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { btAdapt.cancelDiscovery(); String str = lstDevices.get(arg2); String[] values = str.split("http://|"); String address=values[2]; btDevice = btAdapt.getRemoteDevice(address); try { if(values[0].equals("未配對(duì)")) { Toast.makeText(testReflect.this, "由未配對(duì)轉(zhuǎn)為已配對(duì)", 500).show(); ClsUtils.createBond(btDevice.getClass(), btDevice); } else if(values[0].equals("已配對(duì)")) { Toast.makeText(testReflect.this, "由已配對(duì)轉(zhuǎn)為未配對(duì)", 500).show(); ClsUtils.removeBond(btDevice.getClass(), btDevice); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 按鍵處理 * @author GV * */ class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { if (v == btnSearch) {//搜索附近的藍(lán)牙設(shè)備 lstDevices.clear(); Object[] lstDevice = btAdapt.getBondedDevices().toArray(); for (int i = 0; i < lstDevice.length; i++) { BluetoothDevice device=(BluetoothDevice)lstDevice[i]; String str = "已配對(duì)|" + device.getName() + "|" + device.getAddress(); lstDevices.add(str); // 獲取設(shè)備名稱和mac地址 adtDevices.notifyDataSetChanged(); } // 開(kāi)始搜索 setTitle("本機(jī)藍(lán)牙地址:" + btAdapt.getAddress()); btAdapt.startDiscovery(); } else if(v==btnShow){//顯示BluetoothDevice的所有方法和常量,包括隱藏API ClsUtils.printAllInform(btDevice.getClass()); } } } }
希望本文實(shí)例能夠?qū)Υ蠹疫M(jìn)行Android程序開(kāi)發(fā)有一定的借鑒幫助作用。
- android 微信 sdk api調(diào)用不成功解決方案
- android monkey自動(dòng)化測(cè)試改為java調(diào)用monkeyrunner Api
- Android 高版本API方法在低版本系統(tǒng)上的兼容性處理
- Android 調(diào)用百度地圖API示例
- android開(kāi)發(fā)教程之獲取使用當(dāng)前api的應(yīng)用程序名稱
- android通過(guò)google api獲取天氣信息示例
- android通過(guò)Location API顯示地址信息的實(shí)現(xiàn)方法
- Android通過(guò)原生APi獲取所在位置的經(jīng)緯度
- Android指紋識(shí)別API初試
- Android開(kāi)發(fā)學(xué)習(xí)筆記之通過(guò)API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式
- Android 支付寶支付、微信支付、銀聯(lián)支付 整合第三方支付接入方法(后臺(tái)訂單支付API設(shè)計(jì))
- Android4.4 WebAPI實(shí)現(xiàn)拍照上傳功能
- 使用android隱藏api實(shí)現(xiàn)亮度調(diào)節(jié)的方法
- Android API開(kāi)發(fā)之SMS短信服務(wù)處理和獲取聯(lián)系人的方法
- Android 用 camera2 API 自定義相機(jī)
- Android基于API的Tabs3實(shí)現(xiàn)仿優(yōu)酷t(yī)abhost效果實(shí)例
- Android 多媒體播放API簡(jiǎn)單實(shí)例
- 最新Android版本、代號(hào)、對(duì)應(yīng)API/NDK級(jí)別、發(fā)布時(shí)間及市場(chǎng)份額
相關(guān)文章
Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例
這篇文章主要介紹了Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例,講解了ContentProvider對(duì)系統(tǒng)中聯(lián)系人及多媒體資源的管理例子,需要的朋友可以參考下2016-04-04ContentProvider客戶端處理provider邏輯分析
這篇文章主要為大家介紹了ContentProvider客戶端處理provider邏輯分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android多功能時(shí)鐘開(kāi)發(fā)案例(基礎(chǔ)篇)
這篇文章主要為大家詳細(xì)介紹了Android多功能時(shí)鐘開(kāi)發(fā)案例的基礎(chǔ)知識(shí),為開(kāi)發(fā)Android時(shí)鐘打下基礎(chǔ),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開(kāi)關(guān)頁(yè)面和判斷熱點(diǎn)是否打開(kāi)(親測(cè)可用)
跳轉(zhuǎn)手機(jī)熱點(diǎn)的頁(yè)面肯定是用intent,重點(diǎn)是action不知道是什么,網(wǎng)上最常見(jiàn)的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁(yè)面,本文介紹Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開(kāi)關(guān)頁(yè)面和判斷熱點(diǎn)是否打開(kāi),感興趣的朋友一起看看吧2023-08-08android二級(jí)listview列表實(shí)現(xiàn)代碼
今天來(lái)實(shí)現(xiàn)以下大眾點(diǎn)評(píng)客戶端的橫向listview二級(jí)列表,感興趣的朋友可以研究下2013-01-01Android編程實(shí)現(xiàn)的一鍵鎖屏程序詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)的一鍵鎖屏程序,結(jié)合實(shí)例形式詳細(xì)分析了Android一鍵鎖屏的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空詳解
這篇文章主要給大家介紹了關(guān)于Kotlin如何優(yōu)雅地判斷EditText數(shù)據(jù)是否為空的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Android Surfaceview的繪制與應(yīng)用
這篇文章主要介紹了Android Surfaceview的繪制與應(yīng)用的相關(guān)資料,需要的朋友可以參考下2017-07-07