Use of Android Bluetooth A2DP profile

Source: Internet
Author: User

A2DP profile is a Bluetooth situational mode supported by Android, typically used for Bluetooth stereo headphones, which is the output of Bluetooth audio

In the Android app layer, the use of A2DP is not very open, the API provides only a very small number of operating interfaces, even the basic connection can only use reflection to invoke the underlying method. The use of A2DP is through BLUETOOTHA2DP this proxy class to control the A2DP service, here is a simple example of how to use:

Private Bluetoothadapter Mbluetooth = Bluetoothadapter.getdefaultadapter (); Mbluetooth.getprofileproxy (This, new Profilelistener (), BLUETOOTHPROFILE.A2DP);p Ublic class Profilelistener implements Servicelistener {@Overridepublic void onserviceconnected (int profile, Bluetoothprofile proxy) {//TODO auto-generated method stublog.i (TAG, " Onserviceconnected () "); mbluetoothprofile = Proxy;mhandler.sendemptymessage (message_a2dp_proxy_changed);} @Overridepublic void onservicedisconnected (int profile) {//TODO auto-generated method stublog.i (TAG, " Onservicedisconnected () ");}    }


The profile's connection and disconnection methods are defined in the Android source code, so the interface can be obtained through reflection:

public void A2dpconnect (Bluetoothdevice device) {BLUETOOTHA2DP A2DP = (BLUETOOTHA2DP) mbluetoothprofile; A2dp.isa2dpplaying (device); class<? Extends bluetootha2dp> clazz = A2dp.getclass (); Method m2;try {log.i (TAG, "use reflect to connect A2DP"); m2 = Clazz.getmethod ("Connect", Bluetoothdevice.class); M2.invoke (A2DP, device);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); LOG.E (TAG, "error:" + e.tostring ());}}


It is also important to note that due to the use of reflection, not all versions of the Android system, 4.2. More than 2 of the success rate of the connection is still high, and in the acquisition of the state of the connection, not all systems can or get the correct state, Some system or hardware platform does not support the operation of this profile, so in actual use, to adapt to local conditions, depending on the machine.

Use of Android Bluetooth A2DP profile

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.