Android 2.2 以编程方式判断我的蓝牙耳机是否在应用程序启动时连接

发布于 2024-11-30 06:01:59 字数 1454 浏览 2 评论 0原文

我正在为 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 中存在一些新类,例如 蓝牙配置文件BluetoothHeadset 可能可以用 1 行代码完成此操作,但还是这样我正在尝试在 API Level 8 中做到这一点,

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

提前致谢

-H

I am programming for android 2.2 API Level 8 and I am trying to find out if there is currently a bluetooth headset connected when my application starts initially. I can not seem to find a solution to this issue.

Once my application is up and running I can listen for BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED as denoted here which works great mind you. But I can't figure out how to tell before the BroadcastReceiver is on if there is currently a bluetooth headset connected.

I have also figured out how to see if the device has any paired devices via the following.

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");
    }
}

I am hoping that someone out there already has a solution to this issue for API Level 8. I do realize that in API 11 there are some new classes such as
BluetoothProfile and
BluetoothHeadset that could probably do this in 1 line of code, but again I am trying to do this in API Level 8

I am willing to try anything at this point.

Thanks in advance

-H

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2024-12-07 06:01:59

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

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

Following code works for me. First connect your BT device with android manually.

AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
    //bt device is connected with phone
}
else{
   // no device is connected with phone
}
燕归巢 2024-12-07 06:01:59

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

Unfortunately there is really no way to do this in Java on an unrooted device. If you have a device with root access and you use the NDK you can drop into the BlueZ programming layer and you can do it there, but with the publicly available Java API there is no way to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文