android - 如何在android上获取连接的蓝牙设备的名称

标签 android bluetooth

我正在尝试获取连接到运行 android Oreo 的 Android 手机的设备的名称。

这两天我一直在寻找答案,但没有一个有效。建议主要返回 ioexception-read-failed-socket-might-closed-bluetooth 错误

问题是,有什么方法可以让Query返回连接的蓝牙设备吗?

这些是无效的链接和建议:

我可以获得有关之前配对并尝试建立连接的设备或尝试与该设备配对的设备的信息。我想要的是当前配对和连接的设备的名称或连接状态。

最佳答案

这里是答案:

String name;
String address;
String threadName;
public void checkConnected()
{

       BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, serviceListener, BluetoothProfile.HEADSET);
}

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

    }

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy)
    {

        for (BluetoothDevice device : proxy.getConnectedDevices())
        {
            name = device.getName();
            address = device.getAddress();
            threadName = Thread.currentThread().getName();
            Toast.makeText(MainActivity.this, name+" " + address+ threadName,  Toast.LENGTH_SHORT).show();
            txtName.setText(name + " " + address);
            Log.i("onServiceConnected", "|" + device.getName() + " | " + device.getAddress() + " | " + proxy.getConnectionState(device) + "(connected = "
                    + BluetoothProfile.STATE_CONNECTED + ")");
        }

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

关于android - 如何在android上获取连接的蓝牙设备的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225547/

相关文章:

Android,只显示自定义键盘

java - 了解 Android Visualizer 类

java - Android studio 应用程序未将数据保存到数据库并发送确认电子邮件

ios - 从 iPhone 应用程序中的 iWatch 触发 Action 以控制蓝牙设备

android - Android 上的 Yahoo OAuth - 401 错误

java - 如何在 android studio 中使用弹出窗口按钮打开新 Activity ?

iphone - iPhone 应用程序可以通过 A2DP 配置文件连接到蓝牙发射器(非 iOs 设备)吗?

java - BlueCove 无法在 Java 中与蓝牙设备关联

android - 如何通过点击请求Android用户启用蓝牙?

java - 如何获取蓝牙配对设备的设备名称?