This article from http://blog.csdn.net/hellogv/, reference must indicate the source!
The last time I explained the basic usage of Android Bluetooth, I went into depth and discussed the hidden APIs of Bluetooth. Anyone who has used Android settings (setting) knows that after Bluetooth searchingEstablish pairingAndUnpairingBut these two functions are not provided in the SDK. How can we use these two functions? This article uses the Java reflection mechanism to call the functions corresponding to these two functions: createbond and removebond. The specific steps are as follows:
1. Use the GIT tool to download platform/packages/apps/settings. Git and findEstablish pairingAndUnpairingAnd know the host of these two APIs );
2. Use the reflection mechanism to enumerate all of the methods and constants of thdevice to see if they exist:
Static public void printallinform (class clsshow) {<br/> try {<br/> // obtain all methods <br/> method [] hidemethod = clsshow. getmethods (); <br/> int I = 0; <br/> for (; I <pidemethod. length; I ++) {<br/> log. E ("method name", hidemethod [I]. getname (); <br/>}< br/> // obtain all constants <br/> Field [] allfields = clsshow. getfields (); <br/> for (I = 0; I <allfields. length; I ++) {<br/> log. E ("field name", allfields [I]. getname (); <br/>}< br/>} catch (securityexception e) {<br/> // throw new runtimeexception (E. getmessage (); <br/> E. printstacktrace (); <br/>} catch (illegalargumentexception e) {<br/> // throw new runtimeexception (E. getmessage (); <br/> E. printstacktrace (); <br/>} catch (exception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>}
The result is as follows:
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): getdomainthclass
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): isw.thdock
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): Policy
11-29 09:19:12. 043: Method Name (452): policyall
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. If the enumeration finds that the API exists (the SDK is hidden), you can call the API by yourself:
/** <Br/> * source code for Device pairing: Platform/packages/apps/settings. git <br/> */settings/src/COM/Android/settings/Bluetooth/cacheddomainthdevice. java <br/> */<br/> static public Boolean createbond (class btclass, javasthdevice btdevice) throws exception {<br/> method createbondmethod = btclass. getmethod ("createbond"); <br/> Boolean returnvalue = (Boolean) createbondmethod. invoke (btdevice); <br/> return returnvalue. booleanvalue (); <br/>}</P> <p>/** <br/> * for device unmatching, refer to source code: Platform/packages/apps/settings. git <br/> */settings/src/COM/Android/settings/Bluetooth/cacheddomainthdevice. java <br/> */<br/> static public Boolean removebond (class btclass, javasthdevice btdevice) throws exception {<br/> method removebondmethod = btclass. getmethod ("removebond"); <br/> Boolean returnvalue = (Boolean) removebondmethod. invoke (btdevice); <br/> return returnvalue. booleanvalue (); <br/>}
PS: the reason why the SDK does not provide hidden APIs may be due to security or later version compatibility, therefore, it cannot be ensured that the hidden API can run well on all android platforms...
The program running effect in this article is as follows:
The source code of Main. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout Android: Id = "@ + ID/linearlayout01" <br/> Android: layout_height = "wrap_content" Android: layout_width = "fill_parent"> <br/> <button Android: layout_height = "wrap_content" Android: Id = "@ + ID/btnsearch" <br/> Android: TEXT = "Search" Android: layout_width = "160dip"> </button> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "160dip" Android: text = "show" Android: id = "@ + ID/btnshow"> </button> <br/> </linearlayout> <br/> <linearlayout Android: id = "@ + ID/linearlayout02" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </linearlayout> <br/> <listview Android: id = "@ + ID/listview01" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> </listview> <br/> </linearlayout> <br/>
The tool clsutils. Java source code is as follows:
Package COM. testreflect; </P> <p> Import Java. lang. reflect. field; <br/> Import Java. lang. reflect. method; </P> <p> Import android. bluetooth. export thdevice; <br/> Import android. util. log; </P> <p> public class clsutils {</P> <p>/** <br/> * source code for pairing with devices: platform/packages/apps/settings. git <br/> */settings/src/COM/Android/settings/Bluetooth/cacheddomainthdevice. java <br/> */<br/> static public Boolean createbond (class btclass, javasthdevice btdevice) throws exception {<br/> method createbondmethod = btclass. getmethod ("createbond"); <br/> Boolean returnvalue = (Boolean) createbondmethod. invoke (btdevice); <br/> return returnvalue. booleanvalue (); <br/>}</P> <p>/** <br/> * for device unmatching, refer to source code: Platform/packages/apps/settings. git <br/> */settings/src/COM/Android/settings/Bluetooth/cacheddomainthdevice. java <br/> */<br/> static public Boolean removebond (class btclass, javasthdevice btdevice) throws exception {<br/> method removebondmethod = btclass. getmethod ("removebond"); <br/> Boolean returnvalue = (Boolean) removebondmethod. invoke (btdevice); <br/> return returnvalue. booleanvalue (); <br/>}</P> <p>/** <br/> * @ Param clsshow <br/> */<br/> static public void printallinform (class clsshow) {<br/> try {<br/> // obtain all methods <br/> method [] hidemethod = clsshow. getmethods (); <br/> int I = 0; <br/> for (; I <pidemethod. length; I ++) {<br/> log. E ("method name", hidemethod [I]. getname (); <br/>}< br/> // obtain all constants <br/> Field [] allfields = clsshow. getfields (); <br/> for (I = 0; I <allfields. length; I ++) {<br/> log. E ("field name", allfields [I]. getname (); <br/>}< br/>} catch (securityexception e) {<br/> // throw new runtimeexception (E. getmessage (); <br/> E. printstacktrace (); <br/>} catch (illegalargumentexception e) {<br/> // throw new runtimeexception (E. getmessage (); <br/> E. printstacktrace (); <br/>} catch (exception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>
The source code of the main program testreflect. Java is as follows:
Package COM. testreflect; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. list; <br/> Import android. app. activity; <br/> Import android. bluetooth. export thadapter; <br/> Import android. bluetooth. export thdevice; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. intentfilter; <br/> Import android. OS. Bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. adapterview; <br/> Import android. widget. arrayadapter; <br/> Import android. widget. button; <br/> Import android. widget. listview; <br/> Import android. widget. toast; </P> <p> public class testreflect extends activity {<br/> button btnsearch, btnshow; <br/> listview lvbtdevices; <br/> arrayadapter <string> adtdevice S; <br/> List <string> lstdevices = new arraylist <string> (); <br/> define thdevice btdevice; <br/> define thadapter btadapt; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> btnsearch = (button) This. findviewbyid (R. id. btnsearch); <br/> btnsearch. setonclicklistener (New clickevent (); <br/> btnshow = (Button) This. findviewbyid (R. id. btnshow); <br/> btnshow. setonclicklistener (New clickevent (); </P> <p> lvbtdevices = (listview) This. findviewbyid (R. id. listview01); <br/> adtdevices = new arrayadapter <string> (testreflect. this, <br/> android. r. layout. simple_list_item_1, lstdevices); <br/> lvbtdevices. setadapter (adtdevices); <br/> lvbtdevices. setonitemclicklistener (New itemclickevent (); </P> <p> btadapt = bluet Oothadapter. getdefaadapter adapter (); // initialize the local Bluetooth function <br/> If (btadapt. getstate () = descrithadapter. state_off) // enable Bluetooth <br/> btadapt. enable (); </P> <p> // register a receiver to obtain results related to the bluetooth device. <br/> intentfilter intent = new intentfilter (); <br/> intent. addaction (descrithdevice. action_found); <br/> intent. addaction (descrithdevice. action_bond_state_changed); <br/> registerreceiver (searchdevices, intent); </P> <p >}</P> <p> Private B Roadcastreceiver searchdevices = new broadcastreceiver () {<br/> Public void onreceive (context, intent) {<br/> string action = intent. getaction (); <br/> bundle B = intent. getextras (); <br/> object [] lstname = B. keyset (). toarray (); </P> <p> // display all received messages and their details <br/> for (INT I = 0; I <lstname. length; I ++) {<br/> string keyname = lstname [I]. tostring (); <br/> log. E (keyname, String. valueof (B. get (Keyname); <br/>}< br/> // obtain the device's MAC address when searching for a device <br/> If (effecthdevice. action_found.equals (Action) {<br/> export thdevice device = intent <br/>. getparcelableextra (effecthdevice. extra_device); </P> <p> If (device. getbondstate () = descrithdevice. bond_none) {<br/> string STR = "unpaired |" + device. getname () + "|" + device. getaddress (); <br/> lstdevices. add (STR); // obtain the device name and MAC address <br/> adtdevices. notifydatase Tchanged (); <br/>}< br/>}; </P> <p> class itemclickevent implements adapterview. onitemclicklistener {</P> <p> @ override <br/> Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, <br/> long arg3) {<br/> btadapt. canceldiscovery (); <br/> string STR = lstdevices. get (arg2); <br/> string [] values = Str. split ("// |"); <br/> string address = values [2]; </P> <p> btdevice = btadapt. getremotedevice (Address); <br/> try {<br/> If (Values [0]. equals ("not paired") <br/>{< br/> toast. maketext (testreflect. this, "changed from unpaired to paired", 500 ). show (); <br/> clsutils. createbond (btdevice. getclass (), btdevice); <br/>}< br/> else if (Values [0]. equals ("paired") <br/>{< br/> toast. maketext (testreflect. this, "changed from paired to unpaired", 500 ). show (); <br/> clsutils. removebond (btdevice. getclass (), btdevice); <br/>}< br/>} catch (exception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * button processing <br/> * @ author GV <br/> */<br/> class clickevent implements view. onclicklistener {</P> <p> @ override <br/> Public void onclick (view v) {<br/> If (V = btnsearch) {// search for nearby bluetooth devices <br/> lstdevices. clear (); </P> <p> object [] lstdevice = btadapt. getbondeddevices (). toarray (); <br/> for (INT I = 0; I <lstdevice. length; I ++) {<br/> effecthdevice device = (effecthdevice) lstdevice [I]; <br/> string STR = "paired |" + device. getname () + "|" + device. getaddress (); <br/> lstdevices. add (STR); // obtain the device name and MAC address <br/> adtdevices. notifydatasetchanged (); <br/>}< br/> // start search <br/> settitle ("local Bluetooth address:" + btadapt. getaddress (); <br/> btadapt. startdiscovery (); <br/>}< br/> else if (V = btnshow) {// display all the methods and constants of systthdevice, including hiding APIs <br/> clsutils. printallinform (btdevice. getclass (); <br/>}</P> <p>}