Android check if Bluetooth connected

时光毁灭记忆、已成空白 提交于 2021-01-29 04:25:17

问题


I am searching for this on the internet for quite a while, but I can't find what I am looking for.

How can I find out with my app, if my device is already connected to a Bluetooth device (/ was before I start my app).

I was hoping there was something like bool BluetoothAdapter.isPaired();


回答1:


If you are only interested if a connection to an arbitrary bluetooth device is established you can use the BluetoothAdapter.getProfileConnectionState(profile):

    adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null && adapter.isEnabled()) {
        int[] profiles = {BluetoothProfile.A2DP, BluetoothProfile.HEADSET, BluetoothProfile.HEALTH};
        boolean connectionExists = false;
        for (int profileId : profiles) {
            if (BluetoothAdapter.getProfileConnectionState(profileId) == 
                                    BluetoothProfile.STATE_CONNECTED) {
                connectionExists = true;
                break;
            }
        }
    }



回答2:


There is no way to retrieve a list of connected devices at application startup. The Bluetooth API does not allow you to QUERY, instead it allows you to listen to CHANGES.

(s. This question)



来源:https://stackoverflow.com/questions/38050396/android-check-if-bluetooth-connected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!