BluetoothDevice#getName() 在 Android 14 上返回 null
尝试按照以下步骤连接时,获取BluetoothDevice#getName()的空值
注意:特定于 Android 14 的问题。在 Android 13 之前工作正常。
GATT 服务器将通过发送广告数据包来广播其服务。 由于设备名称在 AdvertiseData 中是可选的,因此您可能需要在代码中包含如下所示的控件。 参考[bignerdranch.com](https://bignerdranch.com/blog/bluetooth-low-energy-on-android-part-1/ 和https://bignerdranch.com/blog/bluetooth-low-energy-on-android-part-2/) 作者:安德鲁·伦斯福德
override fun onScanResult(callbackType: Int, result: ScanResult) {
val device: BluetoothDevice = result.getDevice()
// ...do whatever you want with this found device
}
fun getDeviceName () : String? {
return device.name
}
if (getDeviceName() != null)
{ deviceName = getDeviceName()
// do whatever you want with this device name
}
else
{
// code for the case of device.name is null
}