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

Hos*_*ani 2 android bluetooth

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

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

问题是,有没有办法让 Query 返回连接的蓝牙设备?

这些是不起作用的链接和建议:

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

Hos*_*ani 5

这是答案:

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);
    }
};
Run Code Online (Sandbox Code Playgroud)