Bluetooth development environment construction

Car machine development and mobile phone bluetooth bluetooth development role in the process of the big difference is that the connection, and adapt as a mobile phone as the main target of the Android source code while provides the bluetooth protocol stack used as guest end, but the native SDK does not directly provide us, this is the car machine bluetooth development faced tough questions in the first place.

@hide

Those of you who have read the Android source code will be familiar with comments in the following format:

/ * ** XXXXXXXXXXX
* @hide
*/Copy the code

@hide is often found in comments for classes, methods, and even variables, and is often referred to as a hidden API that is not directly available during normal development. You don’t even need to Google, just do a random Search, and hundreds of articles will show you how to use hidden classes, methods, and variables. More common are the following:

  1. Called by reflection. The upside is that you only care about your business, the downside is that the calls are complex, unreadable, and inefficient, and it is recommended to use this method when you only need to use a small number of hidden apis.
  2. Modify the source code, remove @hide. Advantage obviously, from then on don’t helpless pain using reflection of xi xi, more obvious disadvantages, first, you need a source and the source code to compile environment, then need to know about the Android source code to compile, need to familiar with the source structure, there is a large learning cost, again is to modify the source code is likely to cause a chain reaction, leading other API is affected, Increases the cost of testing, outweighs the cost.
  3. Use the Android source compilation feature to get the full.jar. Car bluetooth module is currently using this method, the advantage is consistent with the current car running Android system, no intrusion on the source code, and the use of method 2 is also convenient, etc., of course, the disadvantage is high learning cost. This approach is highlighted below.

Get the full framework.jar

Three bits of background

  1. Android source code compilation is divided into ROM and SDK two kinds, ROM refers to the brush package we often say, and SDK is in the process of Android development using classes, methods, variables, resources, tools and so on. In the development process, we use SDK to write programs with the resources provided by us, and use a series of tools provided by SDK to compile and package the installation package files of.apk type, and then we can deploy the program to the mobile phone. At this time, we will find that.apk files can be as few as several meters or as many as dozens of meters. It is obvious that the Android.jar provided by the SDK for our development is not included in.apk, which means that our Android.jar is only used in the compilation process, and the API we call during the running process is provided by the system on the phone. When we use reflection to call the hidden API, if there is no crash during the program running, it indicates the existence of the API in the phone system, from which we can conclude that the hidden API is not deleted, but excluded from the API docs.
  2. Android source code compilation is divided into several stages, whether ROM or SDK compilation, first will be the code tree of each sub-project to compile a separate.jar, even we can compile a separate sub-project, interested students can learn. After all subprojects are compiled, the.jars are processed differently depending on the target. If the target is SDK, the compiler will assemble several.jars including frameworks. Jar into the familiar Android.jar. The assembly process generates THE API docs, which determines which apis we can use and which we can’t, and this is where the @hide logo comes into play, removing identified apis from the API Docs.
  3. The IDE used for Android APP development is Android Studio (AS for short). AS uses Gradle AS the compilation tool. Gradle references other class libraries into two cases, compile and Provided. Compile means referring to the library compilation project and compiling the library into the final.apk file, while provided means referring to the library compilation project but not packing it into an.apk file.

Specific steps

Based on the above three backgrounds, we can derive a method to get the full amount of frameworks. Jar.

  1. ROM compile car machine is completed to ANDROID_SOURCE/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/path, The classes. Jar is the full set of frameworks. Copy it out and rename it frameworks_all.jar.
  2. willframeworks_all.jarAdd the build.gradle file to the libs directory of the related module in the project.
    dependencies {
       provided files('libs/frameworks_all.jar')}Copy the code
  3. Modify the.iml configuration file in the module directory:
    <orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
    <orderEntry type="library" exported="" name="frameworks_all" level="project" />Copy the code

    To:

    <orderEntry type="library" exported="" name="frameworks_all" level="project" />
    <orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />Copy the code

    After refreshing the project, we found that the hidden API was included in the IDE auto-prompt, and the development work could begin. The.iml file is automatically generated by the IDE and is regenerated every time Gradle is updated, so it may be modified several times during development.

  4. Step 3 ensures that our development works normally, but compilation errors occur. The reason is that Gradle builds using the API in Android.jar in preference. Some of the full frameworks. Jar API we use is not included in Android.jar, so we need to give Gradle priority to compile our project using the full frameworks. Modify the build.gradle file in the root directory of the project:

    buildscript {
       repositories {
          jcenter()
       }
       dependencies {
          classpath 'com. Android. Tools. Build: gradle: 2.2.0'}}allprojects {
       gradle.projectsEvaluated {
          tasks.withType(JavaCompile) {
             options.compilerArgs.add('-Xbootclasspath/p:chjbluetoothconnection/libs/frameworks_all.jar')}}}Copy the code

    -xbootCLASspath /p:$PATH tells the Java VM to load the.class under PATH first. Gradle runs -xbootCLASspath /p:$PATH. The reason for this error is unknown, but it does not appear in Gradle.

The full reference to frameworks. Jar is now complete.

Get bluetooth Pbap stack support.jar

To facilitate development, Android provides several Bluetooth enabled protocol stacks, which we call Profiles. The profiles we need for the development of vehicle-phone Bluetooth mainly include:

Profile Description
A2DP Sink Audio related
HFP Client The phone related
Avrcp Controller Audio control correlation
Pbap Client Address book and call history

Except Pbap Client, the code is contained in ANDROID_SOURCE/frameworks/base/core/directory, which will be compiled with the compilation of subject of frameworks, would be included in our whole quantity inside the bag.

The Pbap Client is contained in the ANDROID_SOURCE/frameworks/opt/ path, which contains the optional functionality of frameworks and requires manual compilation or modification of the Android source compiler script. We choose manual compilation.

Specific steps

  1. After compiling the ROM, run source build/evnsetup.sh in the source code root directory to initialize the compilation tool
  2. Enter ANDROID_SOURCE/frameworks/opt/bluetooth/mm path implementation, code compiled separately on the path, Generated. The jar in ANDROID_SOURCE/out/target/common/obj/JAVA_LIBRARIES/android bluetooth. Client. Pbap_intermediates/path, Copy it out and rename it bluetooth_pbap_client.jar.
  3. Enter the ANDROID_SOURCE/frameworks/base/obex perform the path/mm, code compiled separately on the path, Generated. The jar in ANDROID_SOURCE/out/target/common/obj/JAVA_LIBRARIES/javax.mail obex_intermediates/path, Copy it out and rename it javax_obex.jar.
  4. Add the two generated.jar files to the module libs directory and modify the build.gradle file in the Module directory:
    dependencies {
       compile files('libs/bluetooth_pbap_client.jar')
       compile files('libs/javax_obex.jar')}Copy the code

    Here we use the compile form reference.jar, because this part of the code does not exist in the system, the project needs to provide relevant classes to ensure the normal operation of the program.

At this point the bluetooth Pbap stack support.jar reference is complete.