總結關於藍牙A2DP 操作的一些相關方法和接口:
1. 設置獲取A2dp對象
/** * 服務監聽器,通過綁定服務獲取BluetoothA2dp對象 */ private BluetoothA2dp mBluetoothA2dp; public class MyServiceListener implements BluetoothProfile.ServiceListener { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { LoggerUtils.d(TAG + "onServiceConnected()"); /** * use reflect method to get the Hide method "connectToDevice" in BluetoothA2DP */ if (profile == BluetoothProfile.A2DP) { mBluetoothA2dp = (BluetoothA2dp) proxy; } } @Override public void onServiceDisconnected(int profile) { LoggerUtils.d(TAG + "onServiceDisconnected " + profile); if (profile == BluetoothProfile.A2DP) { mBluetoothA2dp = null; } } sBluetoothAdapter.getProfileProxy(context, new MyServiceListener(), BluetoothProfile.A2DP);
2. 判斷A2DP link 是否已建立
if (sBluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP) == BluetoothProfile.STATE_CONNECTED) { return true; }
3. 獲取連接的A2DP 設備
public BluetoothDevice getConnectedA2dp(BluetoothA2dp mBluetoothA2dp) { LoggerUtils.d(TAG + "getconnectedA2dp()"); List<BluetoothDevice> connectedDevices = mBluetoothA2dp.getConnectedDevices(); if (connectedDevices != null && connectedDevices.size() > 0) { BluetoothDevice device = connectedDevices.get(0); return device; } return null; }
4. 藍牙設備掃描
BluetoothAdapter sBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 獲取藍牙適配器
sBluetoothAdapter.isEnabled() // 判斷手機藍牙是否開啟
sBluetoothAdapter.enable(); // 嘗試開機手機藍牙
sBluetoothAdapter.startDiscovery(); // 開啟掃描藍牙設備
sBluetoothAdapter.cancelDiscovery(); // 停止掃描藍牙設備
5. 注冊監聽藍牙狀態改變的一些通知
public void register() { LoggerUtils.d(TAG + "register()"); IntentFilter filter = new IntentFilter(); //掃描action filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothDevice.ACTION_FOUND); //配對監聽 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); // filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);//A2dp狀態改變 MyApp.getContext().registerReceiver(MYBroadcast, filter); }
6. 監聽回調
BroadcastReceiver MYBroadcast = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); /** * 開始掃描 */ if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { if (mDiscoveryListener == null) return; bluetoothDevices.clear(); LoggerUtils.d(TAG + "ACTION_DISCOVERY_STARTED"); mDiscoveryListener.onScanStart(); /** * 掃描結束 */ } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { if (mDiscoveryListener == null) return; mDiscoveryListener.onScanFinish(); discoveryTimes--; if (bluetoothDevices.size() == 0 && discoveryTimes > 0) { LoggerUtils.d(TAG + "=================="); startDiscovery(true); } else { discoveryTimes = 3; } LoggerUtils.d(TAG + "ACTION_DISCOVERY_FINISHED"); /** * 發現設備 */ } else if (action.equals(BluetoothDevice.ACTION_FOUND)) { if (mDiscoveryListener == null) return; BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); mDiscoveryListener.onDeviceFound(device); bluetoothDevices.add(device); LoggerUtils.d(TAG + "ACTION_FOUND: " + device.getAddress() + "\n" + device.getName()); /** * 綁定狀態改變 */ } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {//綁定狀態監聽 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { connectToDevice(); } /** * 連接狀態改變 */ } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { LoggerUtils.d(TAG + "ACTION_CONNECTION_STATE_CHANGED"); /** * spp連接 */ } else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) { LoggerUtils.d(TAG + "ACTION_ACL_CONNECTED"); /** * spp斷開連接 */ } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) { LoggerUtils.d(TAG + "ACTION_ACL_DISCONNECTED"); /** * 斷開請求 */ } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED)) { LoggerUtils.d(TAG + "ACTION_ACL_DISCONNECT_REQUESTED"); /** * A2dp */ } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, 0); /** * A2dp連接成功 */ if (state == BluetoothA2dp.STATE_CONNECTED) { if (mBluetoothA2dp == null) return; mLastBluetoothDevice = sBluetoothDevice; sBluetoothDevice = getConnectedA2dpDevice(mBluetoothA2dp); LoggerUtils.d(TAG + "onReceive: " + "A2dp的連接成功"); notifyConnectSuccess(); // if(sJIElIManager != null && sJIElIManager.getFilePathItemList()) /** * A2dp斷開 */ } else if (state == BluetoothA2dp.STATE_DISCONNECTED) { LoggerUtils.d(TAG + "onReceive: " + "A2dp斷開"); notifyDisconnect(); } } } };
7. 連接藍牙
public void connectToDevice() { LoggerUtils.d(TAG + "connectToDevice()"); if (sBluetoothDevice == null) return; if (getBluetoothType() == 2) { LoggerUtils.d(TAG + "傑里藍牙連接"); switch (sBluetoothDevice.getBondState()) { case BluetoothDevice.BOND_NONE: boolean returnValue = createBond();//配對 if (returnValue) { LoggerUtils.d(TAG + "配對成功"); } else { LoggerUtils.d(TAG + "配對失敗"); } break; case BluetoothDevice.BOND_BONDED: try { BluetoothSocket socket = device.createRfcommSocketToServiceRecord(DUUID); Method method_connect = clazz.getMethod("connect", BluetoothDevice.class); Boolean BooleanValue = (Boolean) method_connect.invoke(mBluetoothA2dp, mBluetoothDevice); } } catch (Exception e) { e.printStackTrace(); LoggerUtils.d(TAG + "A2dp連接異常,reason = " + e.getCause()); } break; } } }
8. 綁定設備
public boolean createBond() { try { Method createBondMethod = BluetoothDevice.class.getMethod("createBond"); Boolean returnValue = (Boolean) createBondMethod.invoke(sBluetoothDevice); return returnValue; } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return false; }
9, 斷開藍牙
try { Method method_disconnect = clazz.getMethod("disconnect", BluetoothDevice.class); Boolean BooleanValue = (Boolean) method_disconnect.invoke(mBluetoothA2dp, mBluetoothDevice); } catch (Exception e) { e.printStackTrace(); LoggerUtils.d(TAG + "斷開A2dp連接異常,reason = " + e.getMessage()); } if (socket != null) { try { socket.close(); socket = null; LoggerUtils.d(TAG + "斷開spp socket成功"); } catch (IOException e) { e.printStackTrace(); LoggerUtils.d(TAG + "斷開spp socket異常,reason = " + e.getCause()); } }
10. 解綁定設備
public boolean removeBond() { try { Method removeBondMethod = BluetoothDevice.class.getMethod("removeBond"); Boolean returnValue = (Boolean) removeBondMethod.invoke(sBluetoothDevice); return returnValue.booleanValue(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return false; }
參考鏈接: