Android:如何检测蓝牙连接状态

Android:如何检测蓝牙连接状态,android,bluetooth,Android,Bluetooth,我想检测配对蓝牙耳机的连接状态 去接电话。 在Android 3.0 API 11级蓝牙耳机类中 isAudioConnected方法 我不知道如何创建和初始化BluetoothHeadset对象。 看来我需要使用 getProfileProxy,但我需要一个示例代码来了解我如何需要它 创建并传递参数。 谢谢, Hos您需要实施BluetoothProfile.ServiceListener: BluetoothProfile.ServiceListener b = new BlueToothL

我想检测配对蓝牙耳机的连接状态 去接电话。 在Android 3.0 API 11级蓝牙耳机类中 isAudioConnected方法

我不知道如何创建和初始化BluetoothHeadset对象。 看来我需要使用 getProfileProxy,但我需要一个示例代码来了解我如何需要它 创建并传递参数。 谢谢,
Hos

您需要实施BluetoothProfile.ServiceListener:

BluetoothProfile.ServiceListener b = new BlueToothListener();
            boolean profileProxy = BluetoothAdapter.getDefaultAdapter()
                    .getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET);


public class BlueToothListener implements ServiceListener {
        public static BluetoothHeadset headset;
        public static BluetoothDevice bluetoothDevice;
    @Override
    public void onServiceDisconnected(int profile) {// dont care
        headset = null;
    }

    @Override
    public void onServiceConnected(int profile,
            BluetoothProfile proxy) {// dont care
        try {
            Debugger.test("BluetoothProfile onServiceConnected "+proxy);
            if (proxy instanceof BluetoothHeadset)
                headset = ((BluetoothHeadset) proxy);
            else// getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET); 
                return;// ^^ => NEVER

            List<BluetoothDevice> connectedDevices = proxy
                    .getConnectedDevices();
            for (BluetoothDevice device : connectedDevices) {
                Debugger.log("BluetoothDevice found :" + device);
                bluetoothDevice = device;
                int connectionState = headset.getConnectionState(bluetoothDevice);
                Debugger.log("BluetoothHeadset connectionState "+connectionState);//2 == OK
                boolean startVoiceRecognition = headset
                        .startVoiceRecognition(device);
                if (startVoiceRecognition) {
                    Debugger
                            .log("BluetoothHeadset init Listener OK");
                    return;
                }
                else 
                    Notify.popup("Bluetooth headset can't start speech recognition");

            }
        } catch (Exception e) {
            // }
        }
    }
}

`

“onServiceConnected”从未在蓝牙设备连接/断开时被调用。@VineeSTP找到了解决方案吗?