Package COM. xys. mets. common. bluetooth; <br/> /*********************************** <br/> * Bluetooth pairing function ****************/<br/> 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/> * � latform/packages/apps/settings. git <br/> */settings/src/COM/Android/settings/Bluetooth/cacheddomainthdevice. java <br/> */<br/> static public Boolean createbond (class btclass, javasthdevice btdevice) <br/> throws exception {<br/> method createbondmethod = btclass. getmethod ("createbond"); <br/> Boolean returnvalue = (Boolean) createbondmethod. invoke (btdevice); <br/> return returnvalue. booleanvalue (); <br/>}</P> <p>/** <br/> * latlatlatlatlatlatlatlatlatlatlatform/ packages/apps/settings. git <br/> */settings/src/COM/Android/settings/Bluetooth/cacheddomainthdevice. java <br/> */<br/> static public Boolean removebond (class btclass, javasthdevice btdevice) <br/> throws exception {<br/> method removebondmethod = btclass. getmethod ("removebond"); <br/> Boolean returnvalue = (Boolean) removebondmethod. invoke (btdevice); <br/> return returnvalue. booleanvalue (); <br/>}</P> <p> static public Boolean setpin (class btclass, effecthdevice btdevice, <br/> string Str) throws exception {<br/> try {<br/> method removebondmethod = btclass. getdeclaredmethod ("setpin", <br/> new class [] {byte []. class}); <br/> Boolean returnvalue = (Boolean) removebondmethod. invoke (btdevice, <br/> new object [] {Str. getbytes ()}); <br/> log. E ("returnvalue", "" + returnvalue); <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/> return true; </P> <p >}</P> <p>/** <br/> * @ Param clsshow <br/> */<br/> static public void printallinform (class clsshow) {<br/> try {<br/> // zookeeper Compliance compliance rules <br/> method [] hidemethod = clsshow. getmethods (); <br/> int I = 0; <br/> for (; I <pidemethod. length; I ++) {<br/> log. E ("method name", hidemethod [I]. getname () + "; and the I is:" + I ); </P> <p >}< br/> // zookeeper <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/>
Usage:
Public static Boolean pair (string straddr, string strpsw ){
Boolean result = false;
Descrithadapter = descrithadapter. getdefaadapter adapter ();
Descrithadapter. canceldiscovery ();
If (! Descrithadapter. isenabled ()){
Optional thadapter. Enable ();
}
If (! Descrithadapter. checkdomainthaddress (straddr) {// check whether the Bluetooth address is valid
Log. D ("mylog", "devadd UN effient! ");
}
Effecthdevice device = effecthadapter. getremotedevice (straddr );
If (device. getbondstate ()! = Effecthdevice. bond_bonded ){
Try {
Clsutils. setpin (device. getclass (), device, strpsw); // pairing the mobile phone with the Bluetooth collector
Clsutils. createbond (device. getclass (), device );
Remotedevice = device; // send the device object to the global remotedevice after pairing.
Result = true;
} Catch (exception e ){
// Todo auto-generated Catch Block
Log. D ("mylog", "setpin failed! ");
E. printstacktrace ();
}//
}
Else
{
Remotedevice = device; // if the device is successfully bound, the device object is directly sent to the global remotedevice.
Result = true;
}
Return result;
}