Android 2.2编程告诉我的蓝牙耳机是否在应用程序启动时连接

时间:2011-08-17 18:41:39

标签: android bluetooth android-2.2-froyo headset

我正在为Android 2.2 API Level 8编程,我试图找出当我的应用程序最初启动时是否连接了蓝牙耳机。我似乎找不到解决这个问题的方法。

一旦我的应用程序启动并运行,我就可以收听BluetoothDevice.ACTION_ACL_CONNECTED和BluetoothDevice.ACTION_ACL_DISCONNECTED,如here所示。但是如果当前连接了蓝牙耳机,我无法弄清楚如何在BroadcastReceiver开启之前判断。

我还想出了如何通过以下方式查看设备是否有任何配对设备。

BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null)
{
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() != 0)
    {
        // the device has paired devices
        for (BluetoothDevice device : pairedDevices)
        {
            Log.i(TAG, "device name: " + device.getName());
            Log.i(TAG, "device address: " + device.getAddress());
        }
    }
    else
    {
        // no paired devices
        Log.i(TAG, "no paired devices");
    }
}

我希望那里的人已经有针对API Level 8的这个问题的解决方案。我确实意识到在API 11中有一些新的类,比如 BluetoothProfileBluetoothHeadset可能在一行代码中执行此操作,但我再次尝试在API级别8中执行此操作

我愿意在这一点上尝试任何事情。

提前致谢

-H

2 个答案:

答案 0 :(得分:4)

以下代码适合我。首先用手动连接你的BT设备。

AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
    //bt device is connected with phone
}
else{
   // no device is connected with phone
}

答案 1 :(得分:1)

不幸的是,在无根设备上用Java实际上没有办法做到这一点。如果你有一个具有root访问权限的设备并且你使用NDK就可以进入BlueZ编程层而你可以在那里进行,但是使用公开的Java API就无法做到这一点。