我正在尝试获取连接到运行 android Oreo 的 Android 手机的设备的名称。
这两天我一直在寻找答案,但没有一个有效。建议主要返回 ioexception-read-failed-socket-might-closed-bluetooth
错误
问题是,有什么方法可以让Query返回连接的蓝牙设备吗?
这些是无效的链接和建议:
- IOException: read failed, socket might closed - Bluetooth on Android 4.3
- In Android, how to get the profile of a connected bluetooth device?
- list connected bluetooth devices?
我可以获得有关之前配对并尝试建立连接的设备或尝试与该设备配对的设备的信息。我想要的是当前配对和连接的设备的名称或连接状态。
最佳答案
这里是答案:
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/