通过蓝牙连接车机和手机,实现打电话功能实现

前言:目前的汽车中控系统几乎都是清一色使用Android系统平台,来实现多媒体娱乐相关功能。当然也有苹果的CarPlay,谷歌的Android Auto。更多的都是厂家自己深度定制的Android,来达到自己想要的功能

去年一直在开发车载平台的Android应用,基于语音识别的定制应用。有一个功能几乎是标配的,通过语音控制实现打电话功能,那么Android手机设备通过蓝牙连接上汽车中控系统后,如何实现打电话功能的?有两种方式实现

1.通过硬件厂商提供的SDK来实现,这部分直接调用提供的API即可,实现简单

2.通过标准的Android 蓝牙协议实现,需要自己来实现,网上这方面的文章也不是很多

通过标准的Android蓝牙如何实现打电话的?
android.bluetooth.BluetoothHeadsetClient是一个系统隐藏类,实现蓝牙电话就是通过这个类完成。不能直接调用,就只能反射调用了

public class BluetoothManager {
    private final static String DIAL_CLASS_NAME = "android.bluetooth.BluetoothHeadsetClient";  //拨打电话类名
    private final static String DIAL_METHOD_NAME = "dial";  //拨打电话方法名
    private final static String BLUE_TOOTH_PROFILE_FIELD = "HEADSET_CLIENT";  //蓝牙适配器远程设备类型

    private static BluetoothManager instance;
    private OnCallResultListener onCallResultListener;

    public interface OnCallResultListener {
        //蓝牙未打开
        void onBluetoothIsClosed();

        //蓝牙未连接
        void onDeviceIsEmpty();

        //无效的电话号码
        void onPhoneIsInValid();

        void onError(String errorMsg);
    }

    public static BluetoothManager getInstance() {
        if (instance == null) {
            instance = new BluetoothManager();
        }
        return instance;
    }

    //拨打电话的实际调用
    public void dial(final String number, final OnCallResultListener onCallResultListener) {
        this.onCallResultListener = onCallResultListener;
        if (StringUtils.isEmpty(number)) {
            if (onCallResultListener != null) {
                onCallResultListener.onPhoneIsInValid();
            }
        }
        getConnectStatus(new BluetoothProfile.ServiceListener() {
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                List mDevices = proxy.getConnectedDevices();
                if (!Utils.isListEmpty(mDevices)) {
                    for (BluetoothDevice device : mDevices) {
                        dial(proxy, device, number);
                        break;
                    }
                } else {
                    if (onCallResultListener != null) {
                        onCallResultListener.onDeviceIsEmpty();
                    }
                }
            }

            @Override
            public void onServiceDisconnected(int profile) {
                if (onCallResultListener != null) {
                    onCallResultListener.onDeviceIsEmpty();
                }
            }
        });
    }

    //获取蓝牙的连接状态
    private void getConnectStatus(BluetoothProfile.ServiceListener serviceListener) {
        final int bluetooth_Profile_HEADSET_CLIENT;
        try {
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if (!adapter.isEnabled()) {
                if (onCallResultListener != null) {
                    onCallResultListener.onBluetoothIsClosed();
                    return;
                }
            }

            bluetooth_Profile_HEADSET_CLIENT = (int) BluetoothProfile.class.getField(BLUE_TOOTH_PROFILE_FIELD).get(null);
            //获取到蓝牙电话的连接状态
            int isConnected = adapter.getProfileConnectionState(bluetooth_Profile_HEADSET_CLIENT);
            if (isConnected == BluetoothProfile.STATE_DISCONNECTED) {
                if (onCallResultListener != null) {
                    onCallResultListener.onDeviceIsEmpty();
                    return;
                }
            }
            adapter.getProfileProxy(CommonApp.getCommonApp().getApplication(), serviceListener, bluetooth_Profile_HEADSET_CLIENT);

        } catch (Exception e) {
            if (onCallResultListener != null) {
                onCallResultListener.onError(e.getMessage());
            }
        }
    }

    /***
     * 拨号
     * @param proxy
     * @param bluetoothDevice
     * @param number
     */
    private void dial(BluetoothProfile proxy, BluetoothDevice bluetoothDevice, String number) {
        try {
            if (proxy == null || bluetoothDevice == null || (StringUtils.isEmpty(number))) {
                return;
            }
            Class BluetoothHeadsetClient = Class.forName(DIAL_CLASS_NAME);
            Method methodMain = BluetoothHeadsetClient.getMethod(DIAL_METHOD_NAME, BluetoothDevice.class, String.class);
            if (methodMain != null) {
                //蓝牙电话调用的具体执行
                methodMain.invoke(proxy, bluetoothDevice, number);
            }
        } catch (Exception e) {
            if (onCallResultListener != null) {
                onCallResultListener.onError(e.getMessage());
            }
        }
    }
}

这是一个模板代码,标准实现可参考此种方式。。。

你可能感兴趣的:(通过蓝牙连接车机和手机,实现打电话功能实现)