Bluetooth a2dp in Android

Source: Internet
Author: User

Abstract: Bluetooth is an open global standard for low-cost wireless data and digital communication.
Android is the name of an open-source mobile phone operating system based on the Linux platform announced by Google in November 5, 2007. The platform consists of an operating system, middleware, user interface, and application software, it is claimed to be the first truly open and complete mobile software for mobile terminals. By studying the bluetooth wireless communication protocol stack, this article designs and implements the function of listening to high fidelity music (that is, the a2dp Application Framework) on the Android mobile phone platform ).
  
Key words: Bluetooth; a2dp; android
  
  1. Introduction
The Bluetooth Technical Specifications are developed by the Bluetooth Special Interest Group (SIG) to develop interactive services and applications based on the use of universal wireless transmission modules and data communication protocols, and are mostly used for portable communication devices.
The Bluetooth specification includes two files: Core Protocol and Application Framework (profiles. The Protocol Specification section defines the communication protocols at all layers of Bluetooth. the application framework only shows how to use these protocols to implement specific application products [1].
The Bluetooth protocol specification complies with the Open System Interconnection/referencedmodel, OSI/RM, and defines the layers of the Bluetooth protocol stack from low to high.
The following protocols of Logical Link Control and adaptation protocols are the core protocols of Bluetooth wireless communication, providing connectionless and connection-oriented data channels for Bluetooth communication. The serial port simulation protocol can easily transfer a serial communication application model with cables to the wireless serial communication field. Service Discovery Protocol (SDP) is an important part of the Bluetooth Technical Framework. It is the basis of all application models. The implementation of any Bluetooth application model is based on the results of some services.
The basic motivation for networking devices is to make these devices communicate with each other and get services from each other. Other protocols are Bluetooth Application protocols. This article describes how to implement a2dp Based on avdtp (Audio/Video Publishing and transmission protocol) [2].
  
  2. Development Platform and Android system
The text hardware platform is based on mavell's Tavor platform. The Tavor platform includes two parts: Application subsystem and communication subsystem. Xscalecpu (624 MHz) is used in the application subsystem. armcpu (34.67 MHz to 208 MHz) and MSA core (52 mhzto 312 MHz) are used together to process the communication module subsystem, when the mobile phone is in STANDBY state, xscalecpu is in sleep state to save power. The two CPUs use their own DDR memory and communicate with each other through the MSL serial bus. The Bluetooth chip is connected to XScale through a serial port.
  
2.1 audio system
The Bluetooth chip (WLAN/ipvth88w8688, with a serial connection up to 3.6 Mbps, 64-bytestmos) serves as a peripheral with two data paths. One is the ssp3 serial interface used for receiving and transmitting PCM Data for voice communication and is used to create SCO links. The other is the connection with the serial controller for receiving and receiving ACL data groups and other applications based on data groups. I2C bus uses and controls audio decoding chips for routing operations (where sound comes from, speakers, wired headphones, or Bluetooth devices ). The ssp2 interface is the channel for transmitting audio data.
  
2.2 Android system
Android is a software platform designed for mobile devices, including operating systems, middleware, and some key applications.
Currently, the released Android SDK provides required tools and APIs for application development. Java is used for Android development.
Developers can call the same API used by core applications when developing their own applications. This application architecture is designed to simplify Component Reuse. Any application can declare its functions, and any other application can use these functions. The same mechanism allows users to replace components.
Android includes a set of C/C ++ libraries, which are provided to developers through the Android Application Framework.
Each Android Application runs in its own process and has its own Dalvik Virtual Machine instance. Dalvik has been rewritten so that one device can run multiple VMS effectively. The Dalvik virtual machine runs its own unique executable file format-. Dex, Which is optimized and uses as little memory as possible. The Dalvik Virtual Machine relies on linuxkernel to provide underlying functions, such as thread and underlying memory management.
Android relies on Linux kernel 2.6 to provide core system services, such as security, memory management, process management, network, and hardware drivers. Bluetooth drivers are also included. Here, the Linux kernel acts as an abstract layer between the hardware layer and other layers of the system.
  
  3. a2dp implementation
A2dpprofile defines the protocol and process for high-quality audio data transmission, including stereo and single channel data transmission. Here, high-quality audio refers to mono and sterco audio, which are different from common voice transmitted on the Bluetooth SCO link. A typical application of a2dp is to send audio data from a music player to a headset or speaker.
Due to the narrow bandwidth provided by bluetooth, audio data may need to be effectively compressed to ensure real-time playback at the receiving end.
Currently, a2dp only defines point-to-point audio transmission, but does not define broadcast-based audio transmission, probably because of the speed.
A2dp is based on the avdtp transmission protocol. avdtp specifies how the link is established. After the connection is established, the audio data can be sent and received after being compressed. Audio Data is bidirectional [4].
This application framework defines two audio device roles: audio source and sink point.
Source (SRC)-When a device acts as a digital audio stream transmitted to the micro-network SNK, the device is called a data volume.
Collection point (SNK)-When a device acts as a digital audio stream transmitted from a data volume on the same micro-network, the device is called a data stream collection.
  
3.1 signaling process
First, use the stream_end_point_discover command to find the stream endpoint in ACP. The main task is to query the SEP that can be provided by the remote Bluetooth device (which requires the address of the remote Bluetooth device, which is unique, each Sep can provide some services. The remote Bluetooth device will return a respond, which contains some information, which is least important to the number of seids and media service types. The service type can be obtained through get_capablities.
Select the stream endpoint to be created. After the stream_end_point_discover command is sent, multiple seids are obtained. Then, the get_capabilites command is sent to obtain the services that each Sep can provide. (A Seid represents a SEP) for specific service types and field detail, see avdtp manual. We are interested in whether Bluetooth devices (such as Bluetooth headsets) have SBC decoding capabilities. a2dp stipulates that Bluetooth stereo headphones must support SBC decoding and other decoding methods are optional, for details about SBC, see a2dp protocol.
Use the set_configuration command to configure the stream endpoint. You can use get_capabilites to know the services supported by SEP, and then perform specific configuration based on the service type. You need to configure audio channels, sampling rates, and other information. Of course, the Seid parameter must be provided during configuration to specify which Sep to configure.
The last step is to start stream_start. If a SEP has audiomedia services, open a stream and provide the Seid parameter. Stream corresponds to a specific Sep. After enabling stream, you can use the Write System Call to write audio data to the socket. You do not need to specify the specific Seid when writing.
After the link is established, data transmission and chain building are performed. The content of the avdtp protocol must comply with a2dp. Of course, a2dp and avdtp are inseparable.
  
3.2 Audio Encoding
PCM code streams require a lot of bandwidth, that is, inefficient and expensive, and are not suitable for wireless transmission. Therefore, encoding and compression are required before transmission.
A2dp requires that SRC and SNK must support at least the lower than the complexity built-in codec (SBC) standard. MPEG-1 audio, Mpeg-2Audio, MPEG-2, 4 Advanced Audio Encoding (advancedaudio coding, ACC) and adaptivetransform acousticcoding, ATRAC) These audio encoding standards are optional. Other encoding standards are called "non a2dp encoding (Non-A2DPCodec) [5].
The low-complexity built-in codec (SBC) is specially designed for Bluetooth audio and video. It can obtain high audio quality at a moderate bit rate and has low computing complexity.
In the process of signaling interaction, You need to discover the SEP of the bluetooth device and obtain its service capability. If SEP has SBC decoding capability, You need to configure relevant parameters of SBC as follows:
Sample frequency (48 k, 44.1 K, 32 K, 16 k) Mono, dualchannel, stereo) block length (,) sub-band) allocation Method (SNR, loudnes) bitpool (2 ~ 250) the encoding/decoding information that determines the bit rate is included in the SBC data frame header and is continuously sent to the SNK along with the audio data stream.
  
3.3 implementation in Android
In the Android system, the app can only see the audiosystem interface. audioflinger is an implementation class, mainly used for audio selection and PCM package mixing and re-sampling, and transmits PCM data packets to the underlying layer. Audioflinger has only one entity in all processes. If other processes need to access it, they need to communicate with each other through the binder process.
Audiohardware is a hardware abstraction layer that is mainly responsible for receiving, transmitting, and controlling audio data. Audioflinger regards him as the actual hardware. When the audio path is set to a2dp, you can use the bluetooth module to send and receive data and use the stereo Bluetooth headset to hear high fidelity music.
Audioaccessory is used to listen to events and send these events to the audioflinger module to reset the audio path. After the Bluetooth stereo headset matches, audioaccessory obtains the relevant information from the kernel (listens to events at the HCI Layer) and then configures the audio path to a2dp mode, in this way, the audio data will only be sent to the Bluetooth headset. If necessary, you can also set the audio decoding chip of the speaker to go to sleep state.
When audio data is played, audiohardware sends 4096 bytes each time because the SBC audio compression open source code can only compress data of bytes, divide the 4096 bytes into N smaller pieces of bytes and put them in a queue. After being compressed by SBC, it is converted to 113 bytes. Another thread encapsulates the SBC compressed package into a standard a2dp packet, and then moves the a2dp packet to the sending queue.
Finally, set the software timer and send back an a2dp package at intervals. This interval is the recommended value returned during SBC encoding.
  
  4. Results and Performance Evaluation
The headset used for testing is Motorola's S9 Bluetooth headset. After the headset is paired, select an MP3 music for playing. The initial program is the a2dp open-source testing program, the audio files that play PCM data streams in linuxshell can be played normally, but integration into the Android system produces a great delay. After the cache queue and timer were introduced, the effect was significantly improved.
  
  5. Summary
This article studies the bluetooth wireless communication protocol stack, analyzes the Bluetooth audio and video distribution transmission protocol, and briefly introduces the features of the Android system, based on the open-source test program, the audio file playing effect is improved accordingly. The Android mobile phone platform has completely designed and implemented the Bluetooth stereo headset to listen to high fidelity music function (that is, the a2dp Application Framework ).
  

Kyoto famous teachers' thesis center, which has been an academic institution for ten years, provides you with professional title papers, graduation papers, Master Thesis, medical papers, educational papers and other papers.
References
[1] Jin Chunlin Jin Chao Wanbao red. Bluetooth protocol and its source code analysis. National Defense Industry Press, 2006
[2] Miller. Core Bluetooth Technology: authoritative guide to the global open standards for wireless communication. China Machinery Industry Press, 2001
[3] Bluetooth Sig. Generic audio/video distribution profile, 2007
[4] Bluetooth Sig. Core Specification of the Bluetooth 2.0 + EDR, 2004
[5] Bluetooth Sig. Advanced Audio distribution profile specification, 2007

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.