Android 断开已连接蓝牙的连接(媒体和通话音频)【完美解决】

Android 断开已连接蓝牙的连接

通过配对连接蓝牙设备后发现有些设备包含两种属性媒体音频和通话音频;
蓝牙设备连接后的设置页
通过反射调用断开连接的方法后判断连接状态还是已连接。
因为媒体音频是属于BluetoothProfile.A2DP代理的
因为通话音频是属于BluetoothProfile.HEADSET代理的
关闭时需要把他们全部关闭后获取的连接状态才是正确的。

断开对音频和通话的连接

  • 方法调用
disconnect(BluetoothDevice);//要断开的蓝牙连接对象
     * 需要连接/断开的蓝牙设备
     */
    private BluetoothDevice currentBluetoothDevice;
    
    
    private BluetoothAdapter mBluetoothAdapter; = BluetoothAdapter.getDefaultAdapter();
    /**
     * 断开蓝牙设备连接
     *
     * @param bluetoothDevice BluetoothDevice
     */
    public void disconnect(BluetoothDevice bluetoothDevice) {
        currentBluetoothDevice = bluetoothDevice;
        //获取A2DP代理对象
        mBluetoothAdapter.getProfileProxy(this, disconnectProfileServiceListener, BluetoothProfile.HEADSET);
        //获取HEADSET代理对象
        mBluetoothAdapter.getProfileProxy(this, disconnectProfileServiceListener, BluetoothProfile.A2DP);
    }
    
     private final BluetoothProfile.ServiceListener disconnectProfileServiceListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceDisconnected(int profile) {

        }

        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            try {
                if (profile == BluetoothProfile.HEADSET){
                    //使用HEADSET的协议断开蓝牙设备(使用了反射技术调用断开的方法)
                    BluetoothHeadset bluetoothHeadset = (BluetoothHeadset) proxy;
                    boolean isDisConnect = false;
                    try {
                        Method connect = bluetoothHeadset.getClass().getDeclaredMethod("disconnect", BluetoothDevice.class);
                        connect.setAccessible(true);
                        isDisConnect = (boolean) connect.invoke(bluetoothHeadset, currentBluetoothDevice);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.d(MAINACTIVITY, "isDisConnect:" + (isDisConnect ? "断开通话成功" : "断开通话失败") + currentBluetoothDevice.getName());
                }
                if (profile == BluetoothProfile.A2DP) {
                    //使用A2DP的协议断开蓝牙设备(使用了反射技术调用断开的方法)
                    BluetoothA2dp bluetoothA2dp = (BluetoothA2dp) proxy;
                    boolean isDisConnect = false;
                    try {
                        Method connect = bluetoothA2dp.getClass().getDeclaredMethod("disconnect", BluetoothDevice.class);
                        connect.setAccessible(true);
                        isDisConnect = (boolean) connect.invoke(bluetoothA2dp, currentBluetoothDevice);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.d(MAINACTIVITY, "isDisConnect:" + (isDisConnect ? "断开音频成功" : "断开音频失败") + currentBluetoothDevice.getName());

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_44232136/article/details/123238728