如何在Android上获取连接的蓝牙设备的名称 [英] How to get name of the connected Bluetooth device on android
问题描述
我正在尝试获取连接到运行android Oreo的Android手机的设备的名称.
I'm trying to get the name of the device that is connected to the Android phone running android Oreo.
过去两天我一直在寻找答案,但没有一个有效.建议大多返回 ioexception-read-failed-socket-might-closed-bluetooth
错误
I was searching for an answer for the past two days, and none of them worked. suggestions mostly returning ioexception-read-failed-socket-might-closed-bluetooth
error
问题是,有什么方法可以使查询返回已连接的蓝牙设备?
The question is, is there any way to make Query that returns the connected Bluetooth device?
这些是不起作用的链接和建议:
These are the links and suggestion which not working:
我可以获得有关先前配对并尝试建立连接的设备或尝试与该设备配对的设备的信息.我想要的是当前已配对和已连接设备的名称或连接状态.
I can get information about the device that is previously paired and trying to make a connection or a device trying to pair to the device. what I want is the name or the connection state of the currently paired and connected device.
推荐答案
这是答案:
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上获取连接的蓝牙设备的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!