android - 列出连接的蓝牙设备?

标签 android bluetooth

如何在安卓上列出所有连接的蓝牙设备?

谢谢!

最佳答案

public void checkConnected()
{
  // true == headset connected && connected headset is support hands free
  int state = BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(BluetoothProfile.HEADSET);
  if (state != BluetoothProfile.STATE_CONNECTED)
    return;

  try
  {
    BluetoothAdapter.getDefaultAdapter().getProfileProxy(_context, serviceListener, BluetoothProfile.HEADSET);
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
}

private ServiceListener serviceListener = new ServiceListener()
{
  @Override
  public void onServiceDisconnected(int profile)
  {

  }

  @Override
  public void onServiceConnected(int profile, BluetoothProfile proxy)
  {
    for (BluetoothDevice device : proxy.getConnectedDevices())
    {
      Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
          + BluetoothProfile.STATE_CONNECTED + ")");
    }

    BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
  }
};

关于android - 列出连接的蓝牙设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3932228/

相关文章:

java - 通过 Smack android api 添加新用户到 ejabberd

android-layout - Android选项菜单 - 单行中的多个项目

java - 在 RxJava2 中使用背压将 Observable 转换为 Flowable

java - 如何通过蓝牙在其他设备上显示相机预览?

android - 如何检测 Android 手机是否在电脑附近

ios - 向 Apple Watch 扩展程序发送通知

android - com.android.phone 进程意外停止。请再试一次

php - 从android上传大视频文件到服务器

android - 通过蓝牙检测附近的另一个安卓设备

swift - AppDelegate中applicationDidEnterBackground时调用ViewController