如何找出如果蓝牙连接? [英] How to find out if bluetooth is connected?

查看:173
本文介绍了如何找出如果蓝牙连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能教教我怎么可以找出蓝牙连接到其他设备(手机,耳机等)

Can someone teach me how can I find out if bluetooth is connected to other device (mobile, headset, etc.)

推荐答案

我不知道有什么办法让当前连接的设备的清单,但你可以听使用ACL_CONNECTED意图新的连接:
<一href=\"http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED\" rel=\"nofollow\">http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED

I don't know of any way to get a list of currently connected devices, but you can listen for new connections using the ACL_CONNECTED intent: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED

这意图包括与远程设备的连接是一个额外的领域。

This intent includes an extra field with the remote device that the connection is with.

在Android上,所有蓝牙连接ACL连接,因此注册这个意图将让你所有新连接。

On Android, all Bluetooth connections are ACL connections, so registering for this intent will get you all new connections.

所以,你的接收器将是这个样子:

So, your receiver would look something like this:

public class ReceiverBlue extends BroadcastReceiver {
  public final static String CTAG = "ReceiverBlue";
  public Set<BluetoothDevice> connectedDevices = new HashSet<BluetoothDevice>();

  public void onReceive(Context ctx, Intent intent) {

    final BluetoothDevice device = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equalsIgnoreCase( action ) )   {
      Log.v(CTAG, "We are now connected to " + device.getName() );
      if (!connectedDevices.contains(device))
        connectedDevices.add(device);
    }

    if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equalsIgnoreCase( action ) )    {
      Log.v(CTAG, "We have just disconnected from " + device.getName() );
      connectedDevices.remove(device);
    }
  }
}

这篇关于如何找出如果蓝牙连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆