Bluetooth automatically enters the pairing code

Source: Internet
Author: User

 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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.