我正在为 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 级别 8 找到了此问题的解决方案。我确实意识到在 API 11 中存在一些新类,例如 BluetoothProfile和 BluetoothHeadset可能可以用 1 行代码来完成此操作,但我再次尝试在 API 级别 8 中执行此操作
此时我愿意尝试任何事情。
提前致谢
-H
最佳答案
以下代码适用于我。首先手动将您的 BT 设备与 Android 连接。
AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
//bt device is connected with phone
}
else{
// no device is connected with phone
}
关于Android 2.2 以编程方式判断我的蓝牙耳机是否在应用程序启动时连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7097781/