before doing an Android version of Bluetooth with a Sphygmomanometer communication project, One of the biggest challenges is automatic pairing. The Internet to check the data is used to reflect Createbond () and Setpin (), but the test is paired or will appear prompt, but the pairing is successful I began to find how to close this Bluetooth pairing prompt box, followed by the great Android source help me. In Source Bluetoothdevice There are two hidden methods in the class Cancelbondprocess () and Cancelpairinguserinput () one is to cancel the pairing process one is to cancel the user input below is the code for automatic pairing Mainfest,xml registration<receiver android:name= ". Bluetoothconnectactivityreceiver "> <intent-filter> <action android:name=" Android.bluetooth.device.a Ction. Pairing_request "/> </intent-filter></receiver>handle it when you receive the broadcast and set the pre-entered password in Public classBluetoothconnectactivityreceiverextendsbroadcastreceiver{String STRPSW= "0"; @Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method Stub if(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 (), Btdevice, STRPSW);//pairing the phone with the Bluetooth collectorClsutils.createbond (Btdevice.getclass (), btdevice); Clsutils.cancelpairinguserinput (Btdevice.getclass (), btdevice); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }} /************************************ bluetooth Pairing function * **************/ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportAndroid.bluetooth.BluetoothDevice;ImportAndroid.util.Log; Public classclsutils{/*** Pairing with Device reference Source: Platform/packages/apps/settings.git */settings/src/com/android/settings/bluetooth/cachedbluetoo Thdevice.java*/ Static Public BooleanCreatebond (Class btclass, Bluetoothdevice btdevice)throwsException {Method Createbondmethod= Btclass.getmethod ("Createbond"); Boolean returnvalue=(Boolean) Createbondmethod.invoke (Btdevice); returnReturnvalue.booleanvalue (); } /*** to de-match the device reference source: Platform/packages/apps/settings.git */settings/src/com/android/settings/bluetooth/cachedbluet Oothdevice.java*/ Static Public BooleanRemovebond (Class btclass, Bluetoothdevice btdevice)throwsException {Method Removebondmethod= Btclass.getmethod ("Removebond"); Boolean returnvalue=(Boolean) Removebondmethod.invoke (Btdevice); returnReturnvalue.booleanvalue (); } Static Public BooleanSetpin (Class btclass, Bluetoothdevice btdevice, String str)throwsException {Try{Method Removebondmethod= Btclass.getdeclaredmethod ("Setpin", Newclass[] {byte[].class}); Boolean returnvalue=(Boolean) Removebondmethod.invoke (Btdevice,Newobject[] {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 Booleancancelpairinguserinput (Class btclass, bluetoothdevice device)throwsException {Method Createbondmethod= Btclass.getmethod ("Cancelpairinguserinput"); //cancelbondprocess ()Boolean returnvalue =(Boolean) Createbondmethod.invoke (device); returnReturnvalue.booleanvalue (); } //Cancel Pairing Static Public Booleancancelbondprocess (Class btclass, bluetoothdevice device)throwsException {Method Createbondmethod= Btclass.getmethod ("cancelbondprocess"); Boolean returnvalue=(Boolean) Createbondmethod.invoke (device); returnReturnvalue.booleanvalue (); } /** * * @paramClsshow*/ Static Public voidprintallinform (Class clsshow) {Try { //get all the methodsmethod[] Hidemethod =Clsshow.getmethods (); inti = 0; for(; i < hidemethod.length; i++) {LOG.E ("Method Name", Hidemethod[i].getname () + "; and the I is:" +i); } //Get all Constantsfield[] 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 (); } }}Direct Use when executing: Public Static Booleanpair (String straddr, String strpsw) {Booleanresult =false; Bluetoothadapter Bluetoothadapter=bluetoothadapter. Getdefaultadapter (); Bluetoothadapter.canceldiscovery (); if(!bluetoothadapter.isenabled ()) {bluetoothadapter.enable (); } if(!bluetoothadapter.checkbluetoothaddress (STRADDR)) { //Check if the Bluetooth address is validLOG.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 (), device, STRPSW); //pairing the phone with the Bluetooth collectorClsutils.createbond (Device.getclass (), device); Remotedevice= device;//after pairing, pass this device object to the global Remotedeviceresult =true; } Catch(Exception e) {//TODO auto-generated Catch blockLOG.D ("MyLog", "Setpin failed!"); E.printstacktrace (); } // } Else{LOG.D ("MyLog", "has bond_bonded"); Try{Clsutils.createbond (Device.getclass (), device); Clsutils.setpin (Device.getclass (), device, STRPSW); //pairing the phone with the Bluetooth collectorClsutils.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 (); } } returnresult; }
How to implement Android Bluetooth development automatic pairing connection, do not pop up the prompt box