Reprint Please specify source: http://blog.csdn.net/vnanyesheshou/article/details/71713786
This article has authorized the public number Fanfan program Yuan exclusive release scan the bottom of the article QR code or in the search Fanfan program Yuan can pay attention to
This article is mainly Android as the audio source side, A2DP basic operation: including connection, disconnect, set priority, get priority, get A2DP connection status, get A2DP connected device list and other functions.
The full name of the A2DP is the Advanced Audio distribution profile, a protocol for high-quality data transfer, which defines protocols and procedures for transmitting high-quality audio, such as mono or stereo, to the general voice that is transmitted on the Bluetooth SCO link. The typical application of the A2DP is to send audio data from the music player to headphones or speakers.
A2DP defines two types of roles:
Audio source audio is encoded on the input to the sink side.
Audio Sink (audio receiver) after the audio data is received, the decoding operation is restored.
To manipulate A2DP correlation, first to obtain the A2DP proxy object, the method to obtain the proxy object is relatively simple, as follows:
mBtAdapter = BluetoothAdapter.getDefaultAdapter();if(!mBtAdapter.isEnabled()){ //弹出对话框提示用户是后打开 Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 1);}//获取A2DP代理对象mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);
Getprofileproxy does not return directly to the A2DP proxy object, but is obtained through a callback in Mlistener.
private servicelistener mlistener = new Servicelistener () { @Override public void onservicedisconnected ( Span class= "Hljs-keyword" >int profile) {if (profile = = Bluetoothprofile.a2 DP) {MA2DP = null ; }} @Override public void onserviceconnected (int Profile, Bluetoothprofile proxies) {if (profile = = BLUETOOTHPROFILE.A2DP) { MA2DP = (BLUETOOTHA2DP) proxy; //conversion } }};
Success will call back the onserviceconnected function in Mlistener, determine if Proflie is BLUETOOTHPROFILE.A2DP, and convert to BLUETOOTHA2DP object. The A2DP can be manipulated by proxy objects.
A2DP connection first needs to pair with the Bluetooth headset, how to pair here will not elaborate.
I am here to connect to one of the previously paired devices. The device name is:
privatefinal"QCY-QY7";
Get the device, first get the paired Bluetooth device, then traverse these Bluetooth devices, find the Bluetooth name meets the condition of the device, is to operate the device,
//获取配对的蓝牙设备Set<BluetoothDevice>= mBtAdapter.getBondedDevices();for(BluetoothDevice device:bondDevice){ //获取指定名称的设备 if(BT_NAME.equals(device.getName())){ = device; }}
Mconnectdevice is the device to be operated on.
1 A2DP Connection
privatevoidconnectA2dp(BluetoothDevice device){ 100//设置priority try { //通过反射获取BluetoothA2dp中connect方法(hide的),进行连接。 Method connectMethod =BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class); connectMethod.invoke(mA2dp, device); catch (Exception e) { e.printStackTrace(); } }
The Connect method in BLUETOOTHA2DP is hide, cannot be accessed directly, and needs to be obtained through a reflection mechanism to get the method connected. After the connection is successful, the phone can play music and the sound will come out from the Bluetooth headset.
2 disconnecting
privatevoiddisConnectA2dp(BluetoothDevice device){ 0); try { //通过反射获取BluetoothA2dp中connect方法(hide的),断开连接。 Method connectMethod =BluetoothA2dp.class.getMethod("disconnect", BluetoothDevice.class); connectMethod.invoke(mA2dp, device); catch (Exception e) { e.printStackTrace(); }}
The Disconnect method in BLUETOOTHA2DP is also hide, similar to connect.
3 Setting the priority level
| variables |
value |
| Priority_off |
0 |
| priority_on |
100 |
| Priority_auto_connect |
1000 |
| priority_undefined |
-1 |
Setting the priority is necessary, or it may cause problems such as connection or disconnection failure.
publicvoidint priority) { ifnullreturn; try {//通过反射获取BluetoothA2dp中setPriority方法(hide的),设置优先级 Method connectMethod =BluetoothA2dp.class.getMethod("setPriority", BluetoothDevice.class,int.class); connectMethod.invoke(mA2dp, device, priority); catch (Exception e) { e.printStackTrace(); }}
4 Getting the priority level
publicintgetPriority(BluetoothDevice device) { int0; ifnullreturn priority; try {//通过反射获取BluetoothA2dp中getPriority方法(hide的),获取优先级 Method connectMethod =BluetoothA2dp.class.getMethod("getPriority", BluetoothDevice.class); priority = (Integer) connectMethod.invoke(mA2dp, device); catch (Exception e) { e.printStackTrace(); } return priority;}
5 getting A2DP connection status with a device
mA2dp.getConnectionState(device);
6 getting a list of connected devices
//返回值类型List<BluetoothDevice>mA2dp.getConnectedDevices();
7 A2DP Whether the audio stream is being sent
//返回值类型boolean,表示设备是否在通过A2DP发送音频流。mA2dp.isA2dpPlaying(device);
The broadcast receiver listens for changes in the A2DP connection status and A2DP the playback status.
privatevoidinitReceiver(){ //注册广播接收者监听状态改变 new IntentFilter(BluetoothA2dp. ACTION_CONNECTION_STATE_CHANGED); filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); registerReceiver(mReceiver, filter);}
The broadcast recipient obtains the status value through intent.
PrivateBroadcastreceiver Mreceiver= NewBroadcastreceiver () {@Override Public voidOnReceive (context context, Intent Intent) {StringAction=Intent.Getaction ();Log.ITAG,"OnReceive action="+Action);//A2DP Connection Status Change if(Action.equals(BLUETOOTHA2DP.action_connection_state_changed)) {int state=Intent.Getintextra (BLUETOOTHA2DP.Extra_state, BLUETOOTHA2DP.state_disconnected);Log.ITAG,"Connect state="+State); }Else if(Action.equals(BLUETOOTHA2DP.action_playing_state_changed)) {//A2DP Playback status Changeint state=Intent.Getintextra (BLUETOOTHA2DP.Extra_state, BLUETOOTHA2DP.state_not_playing);Log.ITAG,"Play state="+State); } }};
A question is welcome to the exchange.
Connecting Small demo:http://download.csdn.net/detail/vnanyesheshou/9841491
Welcome to follow my public number, regularly push high-quality technical articles:
Android Bluetooth Development (ix) A2DP basic functions