Before doing the Bluetooth auto pairing, now tidy up a bit. Lest forget.
Home must pay attention to the problem of permissions
[HTML]View plain copy <uses-permission android:name= "Android.permission.BLUETOOTH"/>//permission to use Bluetooth <uses-permissi On android:name= "Android.permission.BLUETOOTH_ADMIN"/>//Manage Bluetooth permissions
Also note that a little simulator cannot simulate a Bluetooth device.
To automatically pair your phone's Bluetooth device with a remote Bluetooth device.
The steps are:
1. Get the phone bluetooth adapter [Java] view plain copy Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter ();
2. Check that your phone's Bluetooth device is turned on, and force the Bluetooth device on if it is not turned on
[Java] view plain copy <span style= "color: #000000;" >public static void Openbluetooth () {if (adapter! = NULL) {if (!adapter.isenabled ()) { Adapter.enable ();//forced to turn on Bluetooth}}}</span>
3. Get a remote Bluetooth device for a given address
[Java] view plain copy bluetoothdevice device = Adapter.getremotedevice (straddress);
4. Check if the remote Bluetooth device is paired and pair it without pairing
[Java] View Plain copy if (device.getbondstate () != bluetoothdevice.bond_bonded) {// Determine if the device under the given address is paired try{ clsutils.autobond ( Device.getclass (), device, strpin);//Set pin value clsutils.createbond (Device.getclass (), device); remoteDevice = device; } catch (exception e) { // TODO: handle exception &nBsp; system.out.println ("Pairing not successful"); } } else { remoteDevice = device; } //Auto Pairing set pin value static public boolean autobond (Class btclass,bluetoothdevice device , String strpin) throws Exception { method autobondmethod = btclass.getmethod ("Setpin", New class[]{byte[].class}); Boolean result = (Boolean) Autobondmethod.invoke (Device,new object[]{strpin.getbytes ()); return result; }