本文介绍了Android的:如何检测蓝牙连接状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲检测配对的蓝牙耳机的连接状态到手机上。在的Andr​​oid 3.0(API级别11)BluetoothHeadset类有isAudioConnected()方法。

I want to detect the connection status of a paired Bluetooth headsetto the phone.In Android 3.0 (API level 11) "BluetoothHeadset" class has"isAudioConnected()" method.

  • 在我不知道如何创建(初始化)BluetoothHeadset对象。看来,我需要使用getProfileProxy(),但我需要一个样品code,找出我需要怎么创建和传递的参数。
  • I don't know how to create (initialize) a "BluetoothHeadset" object.It seems that I need to use"getProfileProxy ()" but I need a sample code to find out how I needto create and pass the parameters.

谢谢,何

推荐答案

您需要实现BluetoothProfile.ServiceListener:

You need to implement 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) {
            // }
        }
    }
}

`

这篇关于Android的:如何检测蓝牙连接状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:56