I read several articles, mainly accepting paired broadcasts, and then setting up pins to implement pairing, but most mobile phones on the Internet are not allowed. The following action_pair_request, no definition of this, began to bother me for a while, and the implementation difficulty is: whether to enter that broadcast response.
Define a class, which can be used directly on the Internet
package ; import ; import ; import ; import ; import ; public class ClsUtils { /** * Pair with the device Reference source code: platform/packages/apps/ * /Settings/src/com/android/settings/bluetooth/ */ static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method createBondMethod = ("createBond"); Boolean returnValue = (Boolean) (btDevice); return (); } /** * Unpair with the device Reference source code: platform/packages/apps/ * /Settings/src/com/android/settings/bluetooth/ */ static public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method removeBondMethod = ("removeBond"); Boolean returnValue = (Boolean) (btDevice); return (); } static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception { try { Method removeBondMethod = ("setPin", new Class[] {byte[].class}); Boolean returnValue = (Boolean) (btDevice, new Object[] {()}); ("returnValue", "" + returnValue); } catch (SecurityException e) { // throw new RuntimeException(()); (); } catch (IllegalArgumentException e) { // throw new RuntimeException(()); (); } catch (Exception e) { // TODO Auto-generated catch block (); } return true; } // Cancel user input static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = ("cancelPairingUserInput"); // cancelBondProcess() Boolean returnValue = (Boolean) (device); return (); } // Cancel pairing static public boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception { Method createBondMethod = ("cancelBondProcess"); Boolean returnValue = (Boolean) (device); return (); } /** * * @param clsShow */ static public void printAllInform(Class clsShow) { try { // Get all methods Method[] hideMethod = (); int i = 0; for (; i < ; i++) { ("method name", hideMethod[i].getName() + ";and the i is:" + i); } // Get all constants Field[] allFields = (); for (i = 0; i < ; i++) { ("Field name", allFields[i].getName()); } } catch (SecurityException e) { // throw new RuntimeException(()); (); } catch (IllegalArgumentException e) { // throw new RuntimeException(()); (); } catch (Exception e) { // TODO Auto-generated catch block (); } } static public boolean pair(String strAddr, String strPsw) { boolean result = false; BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); (); if (!()) { (); } if (!(strAddr)) { // Check whether the Bluetooth address is valid ("mylog", "devAdd un effient!"); } BluetoothDevice device = (strAddr); if (() != BluetoothDevice.BOND_BONDED) { try { ("mylog", "NOT BOND_BONDED"); ((), device, strPsw); // Pair the phone and Bluetooth collector ((), device); // remoteDevice = device; // After pairing, pass this device object to the global remoteDevice result = true; } catch (Exception e) { // TODO Auto-generated catch block ("mylog", "setPiN failed!"); (); } // } else { ("mylog", "HAS BOND_BONDED"); try { ((), device); ((), device, strPsw); // Pair the phone and Bluetooth collector ((), device); // remoteDevice = device; // If the binding is successful, pass this device object directly to the global remoteDevice result = true; } catch (Exception e) { // TODO Auto-generated catch block ("mylog", "setPiN failed!"); (); } } return result; } } //================================================================================================================================
There are some activities
//================================================================================================================================ package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class Demo_ad_escActivity extends Activity { //--------------------------------------------------- public static String ErrorMessage; Button btnSearch, btnDis, btnExit; ToggleButton tbtnSwitch; ListView lvBTDevices; ArrayAdapter<String> adtDevices; List<String> lstDevices = new ArrayList<String>(); BluetoothAdapter btAdapt; public static BluetoothSocket btSocket; //--------------------------------------------------- public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); // if(!ListBluetoothDevice())finish(); Button Button1 = (Button) findViewById(.button1); ErrorMessage = ""; //--------------------------------------------------- btnSearch = (Button) (); (new ClickEvent()); btnExit = (Button) (); (new ClickEvent()); btnDis = (Button) (); (new ClickEvent()); // ToogleButton settings tbtnSwitch = (ToggleButton) (.toggleButton1); (new ClickEvent()); // ListView and its data source adapter lvBTDevices = (ListView) (.listView1); adtDevices = new ArrayAdapter<String>(this, .simple_list_item_1, lstDevices); (adtDevices); (new ItemClickEvent()); btAdapt = ();// Initialize the Bluetooth function of the machine if (()) (false); else (true); // Register Receiver to get results related to Bluetooth devices String ACTION_PAIRING_REQUEST = ".PAIRING_REQUEST"; IntentFilter intent = new IntentFilter(); (BluetoothDevice.ACTION_FOUND);// Use BroadcastReceiver to obtain search results (BluetoothDevice.ACTION_BOND_STATE_CHANGED); (ACTION_PAIRING_REQUEST); (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); (BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(searchDevices, intent); (new () { public void onClick(View arg0) { // Print1(SelectedBDAddress); } }); } //--------------------------------------------------- private BroadcastReceiver searchDevices = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = (); Bundle b = (); Object[] lstName = ().toArray(); // Show all received messages and their details for (int i = 0; i < ; i++) { String keyName = lstName[i].toString(); (keyName, ((keyName))); } BluetoothDevice device = null; // When searching for the device, obtain the MAC address of the device if (BluetoothDevice.ACTION_FOUND.equals(action)) { device = (BluetoothDevice.EXTRA_DEVICE); if (() == BluetoothDevice.BOND_NONE) { String str = "Unpaired|" + () + "|" + (); if ((str) == -1)// Prevent repeated additions (str); // Get the device name and mac address (); try { ((),device,"0000"); } catch (Exception e) { // TODO Auto-generated catch block (); } try { ((), device); } catch (Exception e) { // TODO Auto-generated catch block (); } } }else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){ device = (BluetoothDevice.EXTRA_DEVICE); switch (()) { case BluetoothDevice.BOND_BONDING: ("BlueToothTestActivity", "Pairing..."); break; case BluetoothDevice.BOND_BONDED: ("BlueToothTestActivity", "Complete pairing"); connect(device);//Connect the device break; case BluetoothDevice.BOND_NONE: ("BlueToothTestActivity", "Cancel pairing"); default: break; } } if (().equals(".PAIRING_REQUEST")) { ("tag11111111111111111111111", "ddd"); BluetoothDevice btDevice = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // byte[] pinBytes = ("1234"); // (pinBytes); try { ((), btDevice, "0000"); // Pair the phone and Bluetooth collector ((), btDevice); ((), btDevice); } catch (Exception e) { // TODO Auto-generated catch block (); } } } }; class ItemClickEvent implements { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(())(); String str = (arg2); String[] values = ("\\|"); String address = values[2]; ("address", values[2]); BluetoothDevice btDev = (address); try { Boolean returnValue = false; if (() == BluetoothDevice.BOND_NONE) { //Call with reflection method (BluetoothDevice remoteDevice);// Method createBondMethod = ("createBond"); // ("BlueToothTestActivity", "Start Pairing");// returnValue = (Boolean) (btDev); (address, "0000"); showMessage("here"); }else if(() == BluetoothDevice.BOND_BONDED){ connect(btDev); } } catch (Exception e) { (); } } } private void connect(BluetoothDevice btDev) { final UUID SPP_UUID = ("00001101-0000-1000-8000-00805F9B34FB"); UUID uuid = SPP_UUID; try { btSocket = (uuid); ("BlueToothTestActivity", "Start Connect..."); (); } catch (IOException e) { // TODO Auto-generated catch block (); } } class ClickEvent implements { @Override public void onClick(View v) { if (v == btnSearch)// Search for Bluetooth devices and display the results in BroadcastReceiver { if (() == BluetoothAdapter.STATE_OFF) {// If Bluetooth is not turned on (Demo_ad_escActivity.this, "Please turn on Bluetooth first", 1000).show(); return; } if (()) (); (); Object[] lstDevice = ().toArray(); for (int i = 0; i < ; i++) { BluetoothDevice device = (BluetoothDevice) lstDevice[i]; String str = "Paired|" + () + "|" + (); (str); // Get the device name and mac address (); } setTitle("Native Bluetooth address:" + ()); (); } else if (v == tbtnSwitch) {//Native Bluetooth start/off if (() == false) (); else if (() == true) (); } else if (v == btnDis)// This machine can be searched { Intent discoverableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); ( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } else if (v == btnExit) { try { if (btSocket != null) (); } catch (IOException e) { (); } Demo_ad_escActivity.(); } } } @Override protected void onDestroy() { (searchDevices); (); (()); } public void showMessage(String str) { (this,str, Toast.LENGTH_LONG).show(); } }
The above is what the editor introduced to you. The Android cancels the Bluetooth pairing box to achieve automatic pairing function. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!