Learning notes Bluetooth Auto Pairing

Source: Internet
Author: User
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;       }     

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.