2

我正在为 android 2.2 API Level 8 编程,并且我试图确定当我的应用程序最初启动时当前是否连接了蓝牙耳机。我似乎无法找到解决此问题的方法。

一旦我的应用程序启动并运行,我就可以侦听 BluetoothDevice.ACTION_ACL_CONNECTED 和 BluetoothDevice.ACTION_ACL_DISCONNECTED,如此处所示这对你很有帮助。但我不知道如何在广播接收器打开之前判断当前是否连接了蓝牙耳机。

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

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 级别 8 解决了这个问题。我确实意识到在 API 11 中有一些新类,例如 BluetoothProfileBluetoothHeadset可能可以在 1 行代码中完成此操作,但我再次我正在尝试在 API 级别 8 中执行此操作

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

提前致谢

-H

4

2 回答 2

4

以下代码对我有用。首先手动将您的 BT 设备与 android 连接。

AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
    //bt device is connected with phone
}
else{
   // no device is connected with phone
}
于 2014-05-22T10:34:45.670 回答
1

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

于 2011-08-17T18:51:19.457 回答