Android 13th exploring Bluetooth hiding APIs

Source: Internet
Author: User

The last time I explained the basic usage of Android Bluetooth, I went into depth and discussed the hidden APIs of Bluetooth. Anyone who has used Android settings (Setting) knows that pairing and unpairing can be established after Bluetooth search, but these two functions are not provided in the SDK, how can we use these two functions? This article uses the JAVA reflection mechanism to call the functions corresponding to these two functions: createBond and removeBond. The specific steps are as follows:

1. Use the Git tool to download platform/packages/apps/Settings. git, find the APIs for pairing up and unpairing in the Setting source code, and know the hosts of these two APIs );

2. Use the reflection mechanism to enumerate all of the methods and constants of thdevice to see if they exist:

View plaincopy to clipboardprint?
Static public void printAllInform (Class clsShow ){
Try {
// Obtain all methods
Method [] hideMethod = clsShow. getMethods ();
Int I = 0;
For (; I Log. e ("method name", hideMethod [I]. getName ());
}
// Obtain all constants
Field [] 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 block
E. printStackTrace ();
}
}
Static public void printAllInform (Class clsShow ){
Try {
// Obtain all methods
Method [] hideMethod = clsShow. getMethods ();
Int I = 0;
For (; I Log. e ("method name", hideMethod [I]. getName ());
}
// Obtain all constants
Field [] 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 block
E. printStackTrace ();
}
}

The result is as follows:

11-29 09:19:12. 012: method name (452): cancelBondProcess
11-29 09:19:12. 020: method name (452): cancelPairingUserInput
11-29 09:19:12. 020: method name (452): createBond
11-29 09:19:12. 020: method name (452): createInsecureRfcommSocket
11-29 09:19:12. 027: method name (452): createRfcommSocket
11-29 09:19:12. 027: method name (452): createRfcommSocketToServiceRecord
11-29 09:19:12. 027: method name (452): createScoSocket
11-29 09:19:12. 027: method name (452): describeContents
11-29 09:19:12. 035: method name (452): equals
11-29 09:19:12. 035: method name (452): fetchUuidsWithSdp
11-29 09:19:12. 035: method name (452): getAddress
11-29 09:19:12. 035: method name (452): getdomainthclass
11-29 09:19:12. 043: method name (452): getBondState
11-29 09:19:12. 043: method name (452): getName
11-29 09:19:12. 043: method name (452): getServiceChannel
11-29 09:19:12. 043: method name (452): getTrustState
11-29 09:19:12. 043: method name (452): getUuids
11-29 09:19:12. 043: method name (452): hashCode
11-29 09:19:12. 043: method name (452): isw.thdock
11-29 09:19:12. 043: method name (452): removeBond
11-29 09:19:12. 043: method name (452): setPairingConfirmation
11-29 09:19:12. 043: method name (452): setPasskey
11-29 09:19:12. 043: method name (452): setPin
11-29 09:19:12. 043: method name (452): setTrust
11-29 09:19:12. 043: method name (452): toString
11-29 09:19:12. 043: method name (452): writeToParcel
11-29 09:19:12. 043: method name (452): convertPinToBytes
11-29 09:19:12. 043: method name (452): getClass
11-29 09:19:12. 043: method name (452): Policy
11-29 09:19:12. 043: method name (452): policyall
11-29 09:19:12. 043: method name (452): wait
11-29 09:19:12. 051: method name (452): wait
11-29 09:19:12. 051: method name (452): wait

3. If the enumeration finds that the API exists (the SDK is hidden), you can call the API by yourself:

View plaincopy to clipboardprint?
/**
* Source Code for Device pairing: platform/packages/apps/Settings. git
* Settingssrccomandroidsettingsluw.thcached=thdevice. java
*/
Static public boolean createBond (Class btClass, effecthdevice btDevice) throws Exception {
Method createBondMethod = btClass. getMethod ("createBond ");
Boolean returnValue = (Boolean) createBondMethod. invoke (btDevice );
Return returnValue. booleanValue ();
}

/**
* Source Code for unpairing with devices: platform/packages/apps/Settings. git
* Settingssrccomandroidsettingsluw.thcached=thdevice. java
*/
Static public boolean removeBond (Class btClass, effecthdevice btDevice) throws Exception {
Method removeBondMethod = btClass. getMethod ("removeBond ");
Boolean returnValue = (Boolean) removeBondMethod. invoke (btDevice );
Return returnValue. booleanValue ();
}
/**
* Source Code for Device pairing: platform/packages/apps/Settings. git
* Settingssrccomandroidsettingsluw.thcached=thdevice. java
*/
Static public boolean createBond (Class btClass, effecthdevice btDevice) throws Exception {
Method createBondMethod = btClass. getMethod ("createBond ");
Boolean returnValue = (Boolean) createBondMethod. invoke (btDevice );
Return returnValue. booleanValue ();
}

/**
* Source Code for unpairing with devices: platform/packages/apps/Settings. git
* Settingssrccomandroidsettingsluw.thcached=thdevice. java
*/
Static public boolean removeBond (Class btClass, effecthdevice btDevice) throws Exception {
Method removeBondMethod = btClass. getMethod ("removeBond ");
Boolean returnValue = (Boolean) removeBondMethod. invoke (btDevice );
Return returnValue. booleanValue ();
}

PS: the reason why the SDK does not provide hidden APIs may be due to security or later version compatibility, therefore, it cannot be ensured that the hidden API can run well on all Android platforms...

The program running effect in this article is as follows:

Related Article

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.