Eight matching of Bluetooth development examples

Source: Internet
Author: User

The app you wrote matches the Bluetooth device and does not need to be connected via the system settings.

Matching and communication are two different things.

People who have used Android system settings (Setting) know that Bluetooth search can establish pairing and de-pairing , but the functions of these two functions are not given in the SDK. But it can be obtained by reflection.

Method[] HideMethod2 =bluetoothdevice. class . GetMethods ();   int k = 0;    for (k < hidemethod2.length; k++) {  log.e ("Bluetoothdevice method Name", Hidemethod2[k].getname ( ));  }

Know the host of these two APIs (Bluetoothdevice):

/*** Pairing with Device reference Source: Platform/packages/apps/settings.git */settings/src/com/android/settings/bluetooth/ Cachedbluetoothdevice.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/ Cachedbluetoothdevice.java*/  Static  Public BooleanRemovebond (Class btclass,bluetoothdevice btdevice)throwsException {Method Removebondmethod= Btclass.getmethod ("Removebond"); Boolean returnvalue=(Boolean) Removebondmethod.invoke (Btdevice); returnReturnvalue.booleanvalue (); }

In one of the Bluetooth development examples, we have acquired a scanned Bluetooth device, at which point we need to match.

First Step: Custom Classes

ImportJava.lang.reflect.Method;ImportAndroid.bluetooth.BluetoothDevice;/*** This class is used to get Bluetooth matching and contact matching methods, using the principle of Java reflection to obtain, because the two methods are hidden by Android, so this way to get. *  * @authorTHINK **/ Public classBtbondutils {/*** 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"); //The Invoke () method is primarily for class reflection, so that you can invoke a method of a class based on the configured string without knowing the specific class.        Very useful in flexible programming. //Many framework codes are implemented in this way. But general programming, you do not need to do this, because the classes are written by yourself, how to call, how to generate is clear. 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 (); }}

Step Two: Call method

btbondutils. Createbond (Device.getclass (), device);

PS. There will be hints matching the pop-up window.

Step three: Set pin value, no matching pop-up---> This step is to be verified

    // auto Pairing set pin value      staticpublicbooleanthrows  Exception {           = Btclass.getmethod ("Setpin",new class[]{byte[].  Class});           = (Boolean) autobondmethod.invoke (device,new  object[]{strpin.getbytes ()});           return result;      }  

Call Method:

Autobond (Device.getclass (), device, Strpin); // Set pin value  

In the source code there is an automatic pairing method, that is, the PIN value is automatically set to "0000";

Eight matching of Bluetooth development examples

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.