Android-bluetooth® Bluetooth (a) Getting Started

Source: Internet
Author: User

Some time ago on the Internet to read some articles about Android Bluetooth, found that most are based on the old version (4.1 including 4.1) source code, although it is not a hindrance to understand the fundamentals of Bluetooth and workflow, but the 4.2.2 code seems to be a bit regrettable. So for 4.2.2 version of the Code to organize the relevant knowledge, of course, the backbone of Bluetooth work flow is not changed, power on, load drive these actions, but the implementation of these functions of the code location has changed a lot. Hope this article can let everyone to android4.2 Bluetooth part of the code has a preliminary understanding.

Before the text begins, make a clear code version: Android Jellybean 4.2.2, followed by Bluetooth related articles.

Another recommended source of online reading URL http://androidxref.com/, already know the children's shoes ignore this line.

Start a new module or application, of course, first of all to know what it has, and its related code is there, so first look at the Bluetooth code distribution.

1. Code Distribution:

packages/apps/bluetooth/

Look at this path is definitely the Bluetooth application code, mainly about the Bluetooth application protocol performance code, including OPP, HFP, HDP, A2DP, Pan, and so on, these nouns are explained later.

frameworks/base/core/java/android/server/

4.2 After this directory although there are, but the code has been transferred to the application layer, is the previous directory, so 4.2.2 on the Bluetooth here can be ignored.

Framework/base/core/java/android/bluetooth

The code in this directory is more like a bridge, there are some classes for the Java layer, there are corresponding aidl files to contact C, C + + part of the code, or very important.

Kernel\drivers\bluetoothbluetooth

Specific protocol implementations. including HCI,HID,RFCOMM,SCO,SDP and other agreements

Kernel\net\bluetooth Linux Kernel

Bluetoothdevice drivers for various interfaces. For example: USB interface, serial port, etc., above kernel these two directories may not see, but certainly will have.

External\bluetooth\bluedroid BlueZ (Application Space Protocol), the official Bluetooth protocol stack.

The System\bluetoothbluetooth adaptation layer code, similar to that of the framework, is a tool for concatenating the framework and BlueZ.

The approximate code distribution is these, after the initial view let us look at the overall structure of Bluetooth.

2. Overall structure:

This part directly, looking at the intuitive. In the figure, although the JNI part is in the packages layer in the current 4.2 code, it is still drawn in the framework layer, indicating the hope

Don't arouse misunderstanding of understanding. From the picture can feel the overall process and the previous change is not big, so the flow of articles to see 4.1 or earlier should not be a problem.

PS: about the Bluetooth protocol stack is wrong, 4.2 is no longer bluez, in some corrections, of course, this part of the stack is still to be, the new protocol stack to see the following English:

Android 4.2 introduces a new Bluetooth stack optimized for use with Android devices. The new Bluetooth stack developed in

Collaboration between Google and Broadcom replaces the stack based on BlueZ and provides improved compatibility and RELIAB Ility.

Google and Broadcom have developed a new Bluetooth protocol stack, and the compatibility of older versions is unavoidable. In this thank netizens andger032 reminder.

3. Common class and noun explanation:

Under the \packages\apps\settings\src\com\android\settings\bluetooth directory

Bluetoothenabler.java interface Bluetooth on, off the switch is it,

Bluetoothsettings.java main interface for managing pairing and connecting devices

Localbluetoothmanager.java provides a simple calling interface on the Bluetooth API, which is just the beginning.

Cachedbluetoothdevice.java description of the Bluetooth device class, re-encapsulation of the Bluetoothdevice

Bluetoothpairingdialog.java the dialog box for the pairing prompt

/packages/apps/phone/src/com/android/phone/

Bluetoothphoneservice.java in the phone directory must be related to the phone, Bluetooth answer hung up calls will use this

/packages/apps/bluetooth/src/com/android/bluetooth/btservice/

        Adapterservice.java    4.2 After the code, Bluetooth on, off, scan, Pairing will come here, in fact, it is more accurate to say that it replaces the Bluetoothservice.java before 4.1, the original work by this class to complete. When it comes to the 4.2 Bluetooth directory has changed, in 4.1 and previous code packages layer of code only the OPP protocol related application code, that is, the file transfer part, and 4.2 Code application layer of the code is rich in many, according to the specific Bluetooth application protocol to differentiate, divided into the following folder (here are some of the Bluetooth terminology For a simple explanation):

       A2DP     Bluetooth stereo, and Bluetooth headset listen to songs about those, there is also a avrcp--audio/ Video remote control profile, is used to listen to songs when paused, up and down songs selected.
       btservice   The description of the front Adapterservice.java you should be able to guess some, about the basic operation of Bluetooth directory, everything started.
       HDP       Bluetooth for medical applications   Bluetooth Health Device profile
       HFP     & nbsp Phone-related, Bluetooth answer, hang up phone  hands-free profile
       hid       Man-machine interface, Bluetooth mouse keyboard What's this?
      OPP     Not much explanation, there were before.
      pan       describes two or more Bluetooth How the device forms an instant network, and the network-related serial port function (SPP), Dial-Up Networking function (DUN)

PBAP Phone Book Access Protocol (Phonebook access profile)

The Android 4.2 Bluetooth Application Layer section code is richer, although some directories do not have specific code, but it is not to say which version of the update has, as the 4.0 added HDP medical that part of the same.

In addition the JNI code that was originally in the framework was moved to Packages/apps/bluetooth.

Under the/frameworks/base/core/java/android/bluetooth/directory

function Realization of Bluetootha2dp.java A2DP
Bluetoothadapter.java definition of Bluetooth action, virtual device properties, and how to do it
Bluetoothaudiogateway.java bluetooth Voice gateway
Bluetoothclass.java definition of Bluetooth device type
Bluetoothdevice.java Bluetooth Device Properties
Bluetoothdevicepicker.java defines the features of a remote Bluetooth device, such as authentication required, device type
Bluetoothheadset.java defining the properties and interfaces of the Bluetooth headset feature
Bluetoothinputstream.java implementation of Bluetooth streaming interface (input stream)
Bluetoothoutputstream.java implementation of Bluetooth streaming interface (output stream)
Bluetoothserversocket.java Bluetooth socket service side of the method
Bluetoothsocket.java bluetooth Socket Encapsulation
bluetoothuuid.java definition of Bluetooth UUID and analysis of UUID

The above Java files in the use of specific features will be used, and now only a brief description, as for the specific use of the subsequent article is given. And that's what the code says.

For C, C + + part of the code on the one hand did not see so much, on the other hand according to the android JNI naming habits, we find it is easy.

4. Follow-up analysis:

Front from the overall description of the basic knowledge of Bluetooth, implementation in the specific code analysis, we press a few main functions to go, Bluetooth switch, search pairing, Bluetooth headset and telephone and file transfer,

These are some of the most common features of Bluetooth, so in subsequent articles you will follow them in this order to follow the code invocation process. Hope that you can quickly understand Bluetooth, of course, if there is a mistake

Write the wrong place, welcome feedback, Thank you.

Android-bluetooth® bluetooth (i) Getting Started

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.