如何以编程方式判断蓝牙设备是否已连接?

问题描述 投票:0回答:8

我了解如何获取已配对设备的列表,但如何判断它们是否已连接?

这一定是可能的,因为我看到它们列在我手机的蓝牙设备列表中,并且它说明了它们的连接状态。

android android-bluetooth
8个回答
175
投票

将蓝牙权限添加到您的AndroidManifest中,

<uses-permission android:name="android.permission.BLUETOOTH" />

然后使用意图过滤器收听

ACTION_ACL_CONNECTED
ACTION_ACL_DISCONNECT_REQUESTED
ACTION_ACL_DISCONNECTED
广播:

public void onCreate() {
    ...
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter);
}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           ... //Device found
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
           ... //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
           ... //Done searching
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           ... //Device is about to disconnect
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
           ... //Device has disconnected
        }
    }
};

一些注意事项:

  • 无法在应用程序启动时检索已连接设备的列表。蓝牙 API 不允许您查询,而是允许您监听更改
  • 解决上述问题的一个诡异的解决方法是检索所有已知/配对设备的列表...然后尝试连接到每个设备(以确定您是否已连接)。
  • 或者,您可以让后台服务监视蓝牙 API 并将设备状态写入磁盘以供您的应用程序稍后使用。

44
投票

在我的用例中,我只想查看是否为 VoIP 应用程序连接了蓝牙耳机。以下解决方案对我有用。

科特林:

fun isBluetoothHeadsetConnected(): Boolean {
    val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    return (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled
        && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED)
}

Java:

public static boolean isBluetoothHeadsetConnected() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
            && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
} 

当然你需要蓝牙权限:

<uses-permission android:name="android.permission.BLUETOOTH" />


18
投票

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java中的BluetoothDevice系统API中有一个isConnected函数。

如果您想知道当前是否已连接有界(配对)设备,以下功能对我来说效果很好:

public static boolean isConnected(BluetoothDevice device) {
    try {
        Method m = device.getClass().getMethod("isConnected", (Class[]) null);
        boolean connected = (boolean) m.invoke(device, (Object[]) null);
        return connected;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

12
投票

由于某种原因,Android Studio 无法解析BluetoothAdapter.ACTION_ACL_CONNECTED。也许它在 Android 4.2.2 中已被弃用?

这里是 Skylarsutton 代码的修改(非常感谢 Skylarsutton 的回答。)。注册码相同;接收器代码略有不同。我在一项服务中使用它来更新应用程序其他部分引用的蓝牙连接标志。

    public void onCreate() {
        //...
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        this.registerReceiver(BTReceiver, filter);
    }

    //The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            //Do something if connected
            Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_SHORT).show();
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            //Do something if disconnected
            Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
        }
        //else if...
    }
};

1
投票

此代码适用于耳机配置文件,可能也适用于其他配置文件。

首先您需要提供一个配置文件侦听器(Kotlin 代码):

private val mProfileListener = object : BluetoothProfile.ServiceListener {
    override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
        if (profile == BluetoothProfile.HEADSET)
            mBluetoothHeadset = proxy as BluetoothHeadset
    }

    override fun onServiceDisconnected(profile: Int) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = null
        }
    }
}

然后在检查蓝牙时:

mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET)
if (!mBluetoothAdapter.isEnabled) {
    return Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
}

调用 onSeviceConnected 需要一些时间。之后,您可以从以下位置获取已连接耳机设备的列表:

mBluetoothHeadset!!.connectedDevices

1
投票

BluetoothAdapter.getDefaultAdapter().isEnabled
-> 当蓝牙打开时返回 true。

val audioManager = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager

audioManager.isBluetoothScoOn
-> 当设备连接时返回 true


0
投票

我真的在寻找一种获取设备连接状态的方法,而不是监听连接事件。这对我有用:

BluetoothManager bm = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> devices = bm.getConnectedDevices(BluetoothProfile.GATT);
int status = -1;

for (BluetoothDevice device : devices) {
  status = bm.getConnectionState(device, BLuetoothGatt.GATT);
  // compare status to:
  //   BluetoothProfile.STATE_CONNECTED
  //   BluetoothProfile.STATE_CONNECTING
  //   BluetoothProfile.STATE_DISCONNECTED
  //   BluetoothProfile.STATE_DISCONNECTING
}

0
投票

kotlin
扩展功能:

fun BluetoothDevice.isAlreadyConnected(): Boolean {
    return try {
        javaClass.getMethod("isConnected").invoke(this) as? Boolean? ?: false

    } catch (e: Throwable) {
        false
    }
}

用途:

if (device.isAlreadyConnected()) {
     // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.