什么做網(wǎng)站/學生網(wǎng)頁制作成品
#Android9 查看連接多個藍牙耳機查看使用中的藍牙耳機
文章目錄
- 一、主要api:
- 二、BluetoothA2dp 對象的獲取
- 三、獲取 BluetoothDevice 對象,
- 四、其他:
Android 9.0之后,支持一臺手機可以同時連接多個藍牙設(shè)備。
但是判斷那個藍牙設(shè)備是使用中,需要經(jīng)過一些復雜判斷才知道!
一、主要api:
boolean a2dpPlaying = BluetoothA2dp.isA2dpPlaying(BluetoothDevice);
看起來不難,但是 BluetoothA2dp 和 BluetoothDevice 如何獲取到?
BluetoothA2dp 獲取需要連接服務(wù),BluetoothDevice 遍歷所有藍牙即可。
二、BluetoothA2dp 對象的獲取
BluetoothA2dp mA2dp;public void initA2dpAdapter() {BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (mBluetoothAdapter.isEnabled()) { //判斷藍牙是否開啟//獲取A2DP代理對象if (mA2dp == null) { //不能重復連接服務(wù),否則會報錯mBluetoothAdapter.getProfileProxy(getContext(), mBluetoothProfileListener, BluetoothProfile.A2DP);}} else {LogUtil.error("getA2dpAdapter error. bluetooth is not Enabled");}}//getProfileProxy并不會直接返回A2DP代理對象,而是通過mListener中回調(diào)獲取。private BluetoothProfile.ServiceListener mBluetoothProfileListener = new BluetoothProfile.ServiceListener() {@Overridepublic void onServiceDisconnected(int profile) {if (profile == BluetoothProfile.A2DP) {LogUtil.error("A2dp onServiceDisconnected ");mA2dp = null;}}// getProfileProxy 到 連接成功一般需要幾十毫秒!@Overridepublic void onServiceConnected(int profile, BluetoothProfile proxy) {if (profile == BluetoothProfile.A2DP) {LogUtil.error("A2dp onServiceConnected ");mA2dp = (BluetoothA2dp) proxy; //轉(zhuǎn)換 ,這個就是 BluetoothA2dp 對象//不顯示藍牙界面連接耳機會出現(xiàn)多個連接狀態(tài)的藍牙耳機,斷開非使用中的藍牙耳機judgeIsConnectManyBluetooth();}}};
三、獲取 BluetoothDevice 對象,
//判斷所有的耳機設(shè)備是否存在多個連接狀態(tài)的private boolean isConnectManyBluetooth() {//未從已連接設(shè)備列表中找到斷開的api,從所有的設(shè)備列表中進行判斷連接狀態(tài)后斷開LocalBluetoothManager mBluetoothManager = LocalBluetoothManager.getInstance(context, mOnInitCallback);Collection<CachedBluetoothDevice> cachedDevices = mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();LogUtil.debug("cachedDevices size = " + cachedDevices.size());//更新藍牙列表UIint connectDevices = 0;for (CachedBluetoothDevice cachedDevice : cachedDevices) {String name = cachedDevice.getDevice().getName();if (cachedDevice.isConnected()) {LogUtil.debug("cachedDevices name = " + name);connectDevices++;}if (connectDevices > 1) {return true;}}return false;}//判斷是否多個藍牙連接狀態(tài),如果是就斷開非使用中的藍牙private void judgeIsConnectManyBluetooth() {if (isConnectManyBluetooth()) {//獲取當前使用中的藍牙耳機對象,斷開其他耳機對象LocalBluetoothManager mBluetoothManager = LocalBluetoothManager.getInstance(context, mOnInitCallback);Collection<CachedBluetoothDevice> cachedDevices = mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();LogUtil.debug("cachedDevices size = " + cachedDevices.size());//更新藍牙列表UIfor (CachedBluetoothDevice cachedDevice : cachedDevices) {String name = cachedDevice.getDevice().getName();if (mA2dp != null && cachedDevice.getDevice() != null) {boolean a2dpPlaying = mA2dp.isA2dpPlaying(cachedDevice.getDevice()); //判斷藍牙是否使用中!if (a2dpPlaying) {LogUtil.debug("cachedDevices a2dpPlaying name = " + name);disconnectOtherExceptOne(name, cachedDevice.getDevice().getBluetoothClass().getMajorDeviceClass());}}}}}
LocalBluetoothManager 這個類是SettingLib 里面的類,如果不是系統(tǒng)源碼或者系統(tǒng)應用是調(diào)用不到的!可以用反射!
上面是實際項目中的一段代碼,其實具體獲取 BluetoothDevice api只有個兩句:
CachedBluetoothDevice cachedDevice;//單個藍牙對象
BluetoothDevice bluetoothDevice = cachedDevice.getDevice();
四、其他:
//已連接/綁定設(shè)備列表Set<BluetoothDevice> bondedDevices = mBluetoothManager.getBluetoothAdapter().getBondedDevices();
BluetoothDevice 對象沒有啥連接和斷開操作的方法!只有一下配置信息。