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