How to implement Android Bluetooth development automatic pairing connection, do not pop up the prompt box

Source: Internet
Author: User
Tags getmessage

How to implement Android Bluetooth development automatic pairing connection, do not pop up the prompt box

Before making an Android version of Bluetooth, the biggest challenge is to automatically pair.

Surfing the internet for information says it is using reflex createbond () and Setpin (), but pairing or prompting when testing, but pairing is successful.

I started to find out how to close this Bluetooth pairing prompt box, behind the great Android source help me.

There are two hidden methods in the source Bluetoothdevice class.

Cancelbondprocess () and Cancelpairinguserinput ()

One of the two methods is to cancel the pairing process one is to cancel the user input

Here is the code for auto pairing

Mainfest,xml Registration

<receiver android:name= ". Bluetoothconnectactivityreceiver "> <intent-filter> <action android:name=" Android.bluetooth.device.a Ction. Pairing_request "/> </intent-filter></receiver> self-processing when receiving the broadcast and setting the pre-entered password in
 public class bluetoothconnectactivityreceiver extends broadcastreceiver{string  strpsw =  "0"; @Overridepublic  void onreceive (Context context, intent intent) {// todo auto-generated method stubif  (Intent.getaction (). Equals (" Android.bluetooth.device.action.PAIRING_REQUEST ")) {bluetoothdevice btdevice =  Intent.getparcelableextra (Bluetoothdevice.extra_device);// byte[] pinbytes =  Bluetoothdevice.convertpintobytes ("1234");// device.setpin (pinbytes); LOG.I ("tag11111",  "ddd"); Try{clsutils.setpin (Btdevice.getclass (), &NBSP;BTDEVICE,&NBSP;STRPSW);  //   Paired Clsutils.createbond (Btdevice.getclass (),  btdevice) for mobile phone and Bluetooth collector; Clsutils.cancelpairinguserinput (Btdevice.getclass (),  btdevice);} catch  (exception e) {// todo auto-generated catch blocke.printstacktrace ();}}}} 
<b>/************************************  Bluetooth Pairing function  * **************/import  java.lang.reflect.field;import java.lang.reflect.method;import android.bluetooth.bluetoothdevice; import android.util.log;public class clsutils{/** *  and equipment pairing   reference Source: 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 ();} /** *  and Device de-pairing   reference Source: Platform/packages/apps/settings.git * /settings/src/com/android/ Settings/bluetooth/cachedbluetoothdevice.java */static public boolean removebond (Class  btclass, bluetoothdevice&nBsp;btdevice) Throws exception{method removebondmethod = btclass.getmethod ("RemoveBond"); boolean returnvalue =  (Boolean)  removebondmethod.invoke (btdevice);return  Returnvalue.booleanvalue ();} Static public boolean setpin (class btclass, bluetoothdevice btdevice,string &NBSP;STR)  throws exception{try{method removebondmethod = btclass.getdeclaredmethod (" Setpin ", new class[]{byte[].class}); boolean returnvalue =  (Boolean)  removebondmethod.invoke (btdevice,new object[]{ Str.getbytes ()}); LOG.E ("returnvalue",  ""  + returnvalue);} 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 ();} Return true;}   Cancel user input static public boolean cancelpairinguserinput (class btclass,bluetoothdevice  device) Throws exception{method createbondmethod = btclass.getmethod (" Cancelpairinguserinput ");// cancelbondprocess () boolean returnvalue =  (Boolean)   Createbondmethod.invoke (device); Return returnvalue.booleanvalue ();}   de-pairing static public boolean cancelbondprocess (class btclass,bluetoothdevice  Device) Throws exception{method createbondmethod = btclass.getmethod ("cancelBondProcess"); boolean returnvalue =  (Boolean)  createbondmethod.invoke (device);return  Returnvalue.booleanvalue ();} /** *  *  @param  clsshow */static public void printallinform (Class  clsshow) {try{//  obtained all Methods Method[] hidemethod = clsshow.getmethods (); int i =  0;for  (;  i < hidemethod.length; i++) {log.e ("Method name",  hidemethod[i].getname ()  +  " ; And the i is: "+ i);}   Get all Constants 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 ();}}} </b>
<b>public static boolean pair (STRING&NBSP;STRADDR,&NBSP;STRING&NBSP;STRPSW) {boolean  result = false; Bluetoothadapter bluetoothadapter = bluetoothadapter.getdefaultadapter (); Bluetoothadapter.canceldiscovery ();if  (!bluetoothadapter.isenabled ()) {bluetoothadapter.enable ();} if  (! Bluetoothadapter.checkbluetoothaddress (straddr)) { //  Check if the Bluetooth address is valid LOG.D ("MyLog",  "Devadd un  effient! ");} Bluetoothdevice device = bluetoothadapter.getremotedevice (STRADDR);if  (device.getBondState ()  != bluetoothdevice.bond_bonded) {try{log.d ("MyLog",  "not bond_bonded"); Clsutils.setpin (Device.getclass (), &NBSP;DEVICE,&NBSP;STRPSW); //  phone and Bluetooth collector pair Clsutils.createbond ( Device.getclass (),  device);remotedevice = device; //  The device object is passed to the global remotedeviceresult = true after pairing;} catch  (exception e) {// todo auto-generated catch blocklog.d ("MyLog",   " Setpin failed! "); E.printstacktrace ();} &NBSP;//}ELSE{LOG.D ("MyLog",  "has bond_bonded"); Try{clsutils.createbond (Device.getclass (),  Device); Clsutils.setpin (Device.getclass (), &NBSP;DEVICE,&NBSP;STRPSW); //  phone and Bluetooth collector pair Clsutils.createbond ( Device.getclass (),  device);remotedevice = device; //  If the binding succeeds, The device object is passed directly to the global Remotedeviceresult = true;} catch  (exception e) {// todo auto-generated catch blocklog.d ("MyLog",  " Setpin failed! "); E.printstacktrace ();}} Return result;} </b> turn from

How to implement the Android Bluetooth development auto-pairing connection, does not pop up the prompt box

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.